PowerManager.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. #include "stdafx.h"
  2. #include "PowerManager.h"
  3. #include "Misc.h"
  4. static HWND s_notifyHwnd;
  5. static ULONG PowerChanged(PVOID Context, ULONG Type, PVOID Setting);
  6. ULONG PowerChanged(PVOID Context, ULONG Type, PVOID Setting)
  7. {
  8. CString cs;
  9. cs.Format(_T("PowerChanged Type %d"), Type);
  10. Log(cs);
  11. if(Type == PBT_APMRESUMEAUTOMATIC)
  12. {
  13. //had reports of the main window not showing clips after resuming (report was from a vmware vm), catch the resuming callback from windows
  14. //and close and reopen the database
  15. Log(_T("windows is RESUMING, sending message to main window to close and reopen the database/qpastewnd"));
  16. ::PostMessage(s_notifyHwnd, WM_REOPEN_DATABASE, 0, 0);
  17. }
  18. return 0;
  19. }
  20. CPowerManager::CPowerManager()
  21. {
  22. m_registrationHandle = 0;
  23. }
  24. CPowerManager::~CPowerManager(void)
  25. {
  26. }
  27. void CPowerManager::Start(HWND hWnd)
  28. {
  29. s_notifyHwnd = hWnd;
  30. HMODULE powrprof = LoadLibrary( _T("powrprof.dll") );
  31. if( powrprof != NULL )
  32. {
  33. DWORD (_stdcall*PowerRegisterSuspendResumeNotification)(_In_ DWORD,_In_ HANDLE,_Out_ PHPOWERNOTIFY);
  34. PowerRegisterSuspendResumeNotification = (DWORD(_stdcall*)(_In_ DWORD,_In_ HANDLE,_Out_ PHPOWERNOTIFY))GetProcAddress(powrprof, "PowerRegisterSuspendResumeNotification");
  35. if(PowerRegisterSuspendResumeNotification)
  36. {
  37. static _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS testCallback = {PowerChanged, nullptr};
  38. PowerRegisterSuspendResumeNotification(DEVICE_NOTIFY_CALLBACK, &testCallback, &m_registrationHandle);
  39. }
  40. ::FreeLibrary(powrprof);
  41. }
  42. }
  43. void CPowerManager::Close()
  44. {
  45. if(m_registrationHandle != 0)
  46. {
  47. HMODULE powrprof = LoadLibrary( _T("powrprof.dll") );
  48. if( powrprof != NULL )
  49. {
  50. DWORD (_stdcall*PowerUnregisterSuspendResumeNotification)(_Inout_ PHPOWERNOTIFY);
  51. PowerUnregisterSuspendResumeNotification = (DWORD(_stdcall*)(_Inout_ PHPOWERNOTIFY))GetProcAddress(powrprof, "PowerUnregisterSuspendResumeNotification");
  52. if(PowerUnregisterSuspendResumeNotification)
  53. {
  54. PowerUnregisterSuspendResumeNotification(&m_registrationHandle);
  55. }
  56. ::FreeLibrary(powrprof);
  57. }
  58. }
  59. }