C++--------检测电脑是否休眠过

前一阵子,产测上需要这么一种功能———检测电脑是否睡眠过,现在记录一下主要的实现代码。

  1. // TODO: 在此添加额外的初始化代码
  2. CString strCommandLine = GetCommandLine();
  3. strCommandLine.TrimLeft().TrimRight();
  4. CString strTimeout;
  5. int idx = strCommandLine.Find(_T(" "));
  6. if (idx != -1)
  7. {
  8. strTimeout = strCommandLine.Mid(idx+1);
  9. strTimeout.TrimLeft().TrimRight();
  10. //m_nTimeout = _ttoi(strTimeout);
  11. //======================================================//
  12. m_nTimeout = 50;
  13. }
  14. TRACE(_T("m_nTimeout = %d\r\n"),m_nTimeout);
  15. SetTimer(TIMER1,TIMER1_INTERVAL,NULL);
  16. LRESULT CPowerMonitorDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  17. {
  18. // TODO: 在此添加专用代码和/或调用基类
  19. //TRACE(_T("%04x\r\n"),message);
  20. switch (message)
  21. {
  22. case WM_POWERBROADCAST:
  23. {
  24. TRACE(_T("%04x\r\n"),wParam);
  25. //系统睡眠了
  26. if(wParam == PBT_APMSUSPEND)
  27. {
  28. m_bAPMSUSPEND = TRUE;
  29. }
  30. //系统唤醒了
  31. else if (wParam == PBT_APMRESUMESUSPEND)
  32. {
  33. m_bAPMRESUMESUSPEND = TRUE;
  34. }
  35. }
  36. break;
  37. default:
  38. break;
  39. }
  40. return CDialogEx::WindowProc(message, wParam, lParam);
  41. }
  42. void CPowerMonitorDlg::OnTimer(UINT_PTR nIDEvent)
  43. {
  44. // TODO: 在此添加消息处理程序代码和/或调用默认值
  45. switch (nIDEvent)
  46. {
  47. case TIMER1:
  48. {
  49. m_dwCnts++;
  50. TRACE(_T("m_dwCnts=%d\r\n"),m_dwCnts);
  51. CString strSeconds;
  52. strSeconds.Format(_T("%ds"),m_nTimeout-m_dwCnts);
  53. //strSeconds.Format(_T("%ds"), 40);
  54. GetDlgItem(IDC_STATIC_TIMER)->SetWindowText(strSeconds);
  55. //3秒钟后执行
  56. //if (m_dwCnts==3)
  57. //{
  58. // system("shutdown -h");
  59. // //SetSystemPowerState(false, false);
  60. // //Sleep(1000*6);
  61. //}
  62. CString strTemp;
  63. CFile mFile;
  64. DWORD dwAttr = GetFileAttributes(_T("C://test"));
  65. //若文件夹不存在,创建文件夹
  66. if (dwAttr == 0xFFFFFFFF){
  67. CreateDirectory(_T("C://test"), NULL);
  68. }
  69. else{
  70. mFile.Open(_T("C://test//log.txt "), CFile::modeCreate | CFile::modeNoTruncate | CFile::modeWrite);
  71. }
  72. WORD unicode = 0xFEFF; //这句重要
  73. mFile.SeekToBegin();
  74. mFile.Write(&unicode, 2); //这句重要
  75. CTime time = CTime::GetCurrentTime(); ///构造CTime对象
  76. CString m_strTime = time.Format("%Y-%m-%d %H:%M:%S");
  77. if (m_bAPMSUSPEND && m_bAPMRESUMESUSPEND)
  78. {
  79. SetTimer(TIMER2,TIMER2_INTERVAL,NULL);
  80. m_bResult = TRUE;
  81. KillTimer(TIMER1);
  82. mFile.SeekToEnd();
  83. mFile.Write(_T("\r\n"), sizeof(_T("\r\n")));
  84. strTemp = _T("TestTime:") + m_strTime + _T(" result:pass ");
  85. mFile.Write(strTemp, wcslen(strTemp)*sizeof(wchar_t));
  86. GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("测试通过!"));
  87. }
  88. else if(m_dwCnts >= static_cast<DWORD>(m_nTimeout))
  89. {
  90. SetTimer(TIMER2,TIMER2_INTERVAL,NULL);
  91. m_bResult = FALSE;
  92. KillTimer(TIMER1);
  93. mFile.SeekToEnd();
  94. mFile.Write(_T("\r\n"), sizeof(_T("\r\n")));
  95. strTemp = _T("TestTime:") + m_strTime + _T(" result:fail ");
  96. mFile.Write(strTemp, wcslen(strTemp)*sizeof(wchar_t));
  97. GetDlgItem(IDC_STATIC_INFO)->SetWindowText(_T("测试失败!"));
  98. }
  99. mFile.Flush();
  100. mFile.Close();
  101. }
  102. break;
  103. case TIMER2:
  104. {
  105. KillTimer(TIMER2);
  106. if (m_bResult)
  107. {
  108. exit(0);
  109. }
  110. else
  111. {
  112. exit(1);
  113. }
  114. }
  115. default:
  116. break;
  117. }
  118. CDialogEx::OnTimer(nIDEvent);
  119. }

发表评论

表情:
评论列表 (有 0 条评论,76人围观)

还没有评论,来说两句吧...

相关阅读