HANDLE hBacklight = SetPowerRequirement(L"BKL1:", D0, POWER_NAME, NULL, 0);
ReleasePowerRequirement(hBacklight)
const static LPCWSTR s_szGWE_REG_PATH = L"SYSTEM\\GWE";
const static LPCWSTR s_szActivityKey = L"ActivityEvent";
DWORD EventName(LPWSTR& lpszName)
{
HKEY hKey = NULL;
DWORD nResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, s_szGWE_REG_PATH, 0, 0, &hKey);
if (nResult != ERROR_SUCCESS)
return 0;
DWORD nNumBytes = 0;
DWORD nType;
nResult = RegQueryValueEx(hKey, s_szActivityKey, NULL, &nType, NULL, &nNumBytes);a
if (nResult == ERROR_SUCCESS)
{
nResult = 0;
if (nNumBytes > 0)
{
lpszName = (LPWSTR)malloc(nNumBytes + 2);
if (lpszName != NULL)
{
ZeroMemory(lpszName, nNumBytes + 2);
nResult = RegQueryValueEx(hKey, s_szActivityKey, NULL, &nType, (LPBYTE)lpszName, &nNumBytes);
nResult = wcslen(lpszName);
}
}
RegCloseKey(hKey);
}
return nResult;
};
#pragma once
class CPowerEvent
{
enum enumHandles
{
HANDLE_THREAD = 0,
HANDLE_POWER_EVENT,
HANDLE_STOP,
HANDLE_START,
HANDLE_EXIT,
HANDLES
};
enum enumTime
{
TIME_EXIT_THREAD = 1000,
TIME_IDLE = 5000
};
LPWSTR m_lpszName;
HANDLE m_arrHandles[HANDLES];
DWORD m_nIdle;
static DWORD WINAPI Run(LPVOID pParam);
public:
CPowerEvent();
virtual ~CPowerEvent();
static DWORD EventName(LPWSTR& lpszName);
BOOL Start();
BOOL Stop();
BOOL SetIdle(DWORD nTime);
inline BOOL IsActive()
{ return (m_arrHandles[HANDLE_THREAD] != NULL); };
void Clear();
};
#include "StdAfx.h"
#include "PowerEvent.h"
const static LPCWSTR s_szGWE_REG_PATH = L"SYSTEM\\GWE";
const static LPCWSTR s_szActivityKey = L"ActivityEvent";
CPowerEvent::CPowerEvent()
{
m_lpszName = NULL;
m_nIdle = TIME_IDLE;
ZeroMemory(m_arrHandles, HANDLES * sizeof(HANDLE));
}
CPowerEvent::~CPowerEvent()
{
Clear();
}
void CPowerEvent::Clear()
{
if (m_arrHandles[HANDLE_THREAD] != NULL)
{
SetEvent(m_arrHandles[HANDLE_EXIT]);
DWORD nWait = WaitForSingleObject(m_arrHandles[HANDLE_THREAD], TIME_EXIT_THREAD);
if (nWait != WAIT_OBJECT_0)
TerminateThread(m_arrHandles[HANDLE_THREAD], 2);
}
int nCnt = 0;
do
{
if (m_arrHandles[nCnt] != NULL)
{
CloseHandle(m_arrHandles[nCnt]);
m_arrHandles[nCnt] = NULL;
}
nCnt++;
} while(nCnt < HANDLES);
m_nIdle = TIME_IDLE;
ZeroMemory(m_arrHandles, HANDLES * sizeof(HANDLE));
if (m_lpszName != NULL)
{
free(m_lpszName);
m_lpszName = NULL;
}
}
DWORD CPowerEvent::EventName(LPWSTR& lpszName)
{
HKEY hKey = NULL;
DWORD nResult = RegOpenKeyEx(HKEY_LOCAL_MACHINE, s_szGWE_REG_PATH, 0, 0, &hKey);
if (nResult != ERROR_SUCCESS)
return 0;
DWORD nNumBytes = 0;
DWORD nType;
nResult = RegQueryValueEx(hKey, s_szActivityKey, NULL, &nType, NULL, &nNumBytes);
if (nResult == ERROR_SUCCESS)
{
nResult = 0;
if (nNumBytes > 0)
{
lpszName = (LPWSTR)malloc(nNumBytes + 2);
if (lpszName != NULL)
{
ZeroMemory(lpszName, nNumBytes + 2);
nResult = RegQueryValueEx(hKey, s_szActivityKey, NULL, &nType, (LPBYTE)lpszName, &nNumBytes);
nResult = wcslen(lpszName);
}
}
RegCloseKey(hKey);
}
return nResult;
}
BOOL CPowerEvent::Start()
{
if (m_arrHandles[HANDLE_THREAD] != NULL)
{
SetEvent(m_arrHandles[HANDLE_START]);
return TRUE;
}
if (m_lpszName == NULL)
{
DWORD nSize = EventName(m_lpszName);
if (nSize == 0 || m_lpszName == NULL)
return FALSE;
}
m_arrHandles[HANDLE_POWER_EVENT] = CreateEvent(NULL, FALSE, FALSE, m_lpszName);
if (m_arrHandles[HANDLE_POWER_EVENT] == NULL)
return FALSE;
int nCnt = HANDLE_STOP;
while (nCnt < HANDLES)
{
m_arrHandles[nCnt] = CreateEvent(NULL, FALSE, FALSE, NULL);
nCnt++;
}
m_arrHandles[HANDLE_THREAD] = CreateThread(NULL, 0, Run, (LPVOID)this, 0, NULL);
if (m_arrHandles[HANDLE_THREAD] == NULL)
{
Clear();
return FALSE;
}
return TRUE;
}
BOOL CPowerEvent::Stop()
{
if (m_arrHandles[HANDLE_THREAD] == NULL)
return FALSE;
SetEvent(m_arrHandles[HANDLE_STOP]);
return TRUE;
}
BOOL CPowerEvent::SetIdle(DWORD nTime)
{
if (m_arrHandles[HANDLE_THREAD] != NULL)
return FALSE;
m_nIdle = nTime;
return TRUE;
}
DWORD WINAPI CPowerEvent::Run(LPVOID pParam)
{
CPowerEvent* pSelf = (CPowerEvent*)pParam;
DWORD nIdle = TIME_IDLE;
if (pSelf->m_nIdle > 0 && pSelf->m_nIdle != nIdle)
nIdle = pSelf->m_nIdle;
HANDLE arrWait[2] = { pSelf->m_arrHandles[HANDLE_STOP], pSelf->m_arrHandles[HANDLE_EXIT] };
HANDLE arrPause[2] = { pSelf->m_arrHandles[HANDLE_START], pSelf->m_arrHandles[HANDLE_EXIT] };
DWORD nWait = WAIT_OBJECT_0;
BOOL bExit = FALSE;
do
{
SetEvent(pSelf->m_arrHandles[HANDLE_POWER_EVENT]);
nWait = WaitForMultipleObjects(2, arrWait, FALSE, nIdle);
if (nWait == WAIT_OBJECT_0)
{
nWait = WaitForMultipleObjects(2, arrPause, FALSE, INFINITE);
if (nWait == WAIT_OBJECT_0 + 1)
bExit = TRUE;
}
else if (nWait == WAIT_OBJECT_0 + 1)
bExit = TRUE;
} while(!bExit);
return 0;
};
void EmulateKeyboard()
{
EnableHardwareKeyboard(FALSE);
keybd_event(0xE9, 0, KEYEVENTF_SILENT, 0);
keybd_event(0xE9, 0, KEYEVENTF_SILENT | KEYEVENTF_KEYUP, 0);
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (1)
Commented:
Articles when first submittted are regarded as "unverified" in so much as none of the Editors have seen it yet.
As an article is worked on it should swap status between Editor Review and Author Review.
As aikimark has already pointed out, it really was a message for him. Sorry for any confusion.