Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 27 additions & 16 deletions src/PowerManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,49 +30,60 @@ ULONG PowerChanged(PVOID Context, ULONG Type, PVOID Setting)
CPowerManager::CPowerManager()
{
m_registrationHandle = 0;
m_hPowrProf = NULL;
}


CPowerManager::~CPowerManager(void)
{
Close();
}


void CPowerManager::Start(HWND hWnd)
{
s_notifyHwnd = hWnd;

HMODULE powrprof = LoadLibrary( _T("powrprof.dll") );
if( powrprof != NULL )
if (m_hPowrProf == NULL)
{
DWORD (_stdcall*PowerRegisterSuspendResumeNotification)(_In_ DWORD,_In_ HANDLE,_Out_ PHPOWERNOTIFY);
PowerRegisterSuspendResumeNotification = (DWORD(_stdcall*)(_In_ DWORD,_In_ HANDLE,_Out_ PHPOWERNOTIFY))GetProcAddress(powrprof, "PowerRegisterSuspendResumeNotification");
if(PowerRegisterSuspendResumeNotification)
m_hPowrProf = LoadLibrary(_T("powrprof.dll"));
}

if (m_hPowrProf != NULL && m_registrationHandle == 0)
{
DWORD (_stdcall*PowerRegisterSuspendResumeNotification)(_In_ DWORD, _In_ HANDLE, _Out_ PHPOWERNOTIFY);
PowerRegisterSuspendResumeNotification = (DWORD(_stdcall*)(_In_ DWORD, _In_ HANDLE, _Out_ PHPOWERNOTIFY))GetProcAddress(m_hPowrProf, "PowerRegisterSuspendResumeNotification");
if (PowerRegisterSuspendResumeNotification)
{
static _DEVICE_NOTIFY_SUBSCRIBE_PARAMETERS testCallback = {PowerChanged, nullptr};

PowerRegisterSuspendResumeNotification(DEVICE_NOTIFY_CALLBACK, &testCallback, &m_registrationHandle);
}

::FreeLibrary(powrprof);
}
}

void CPowerManager::Close()
{
if(m_registrationHandle != 0)
if (m_hPowrProf != NULL)
{
HMODULE powrprof = LoadLibrary( _T("powrprof.dll") );
if( powrprof != NULL )
if (m_registrationHandle != 0)
{
DWORD (_stdcall*PowerUnregisterSuspendResumeNotification)(_Inout_ PHPOWERNOTIFY);
PowerUnregisterSuspendResumeNotification = (DWORD(_stdcall*)(_Inout_ PHPOWERNOTIFY))GetProcAddress(powrprof, "PowerUnregisterSuspendResumeNotification");
if(PowerUnregisterSuspendResumeNotification)
DWORD (_stdcall*PowerUnregisterSuspendResumeNotification)(_Inout_ HPOWERNOTIFY);
PowerUnregisterSuspendResumeNotification = (DWORD(_stdcall*)(_Inout_ HPOWERNOTIFY))GetProcAddress(m_hPowrProf, "PowerUnregisterSuspendResumeNotification");
if (PowerUnregisterSuspendResumeNotification)
{
PowerUnregisterSuspendResumeNotification(&m_registrationHandle);
DWORD ret = PowerUnregisterSuspendResumeNotification(m_registrationHandle);
if (ret == ERROR_SUCCESS)
{
m_registrationHandle = 0;
}
}
}

::FreeLibrary(powrprof);
if (m_registrationHandle == 0)
{
::FreeLibrary(m_hPowrProf);
m_hPowrProf = NULL;
}
}
}
}
3 changes: 2 additions & 1 deletion src/PowerManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@ class CPowerManager
~CPowerManager(void);

void Start(HWND hWnd);
void CPowerManager::Close();
void Close();

protected:
HPOWERNOTIFY m_registrationHandle;
HMODULE m_hPowrProf;
};