我有C应用程序,用于确定我的笔记本电脑何时打开电源。 它仅在我打开此.exe文件时才有效
有没有办法让它在内核模式下工作? 意思是我不想运行.exe但只是打开笔记本电脑并在电量不足时接收有关电源的消息。
这是我的.exe:
#include <windows.h>
const char g_szClassName[] = "myWindowClass";
// Step 4: the Window Procedure
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
short x ;
SYSTEM_POWER_STATUS status;
switch(msg)
{
case WM_CLOSE:
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_POWERBROADCAST:
switch (wParam)
{
case PBT_APMPOWERSTATUSCHANGE :
x = GetSystemPowerStatus(&status);
if (x > 0) // function succeeded
{
if (status.ACLineStatus == 1)
{
printf("power is off");
MessageBox(NULL, "power is on" , "NOTICE" ,MB_OK );
}
else if (status.ACLineStatus == 0)
{
printf("power is on");
MessageBox(NULL, "power is off" , "NOTICE" ,MB_OK );
}
else
{
printf("unknown");
MessageBox(NULL, "unknown status" , "ERROR" ,MB_OK);
}
}
else
{
LPSTR str = "function failed in providing information";
MessageBox(NULL , str, "ERROR", MB_OK);
}
break;
case PBT_POWERSETTINGCHANGE:
x = GetSystemPowerStatus(&status);
if (x > 0) // function succeeded
{
if (status.ACLineStatus == 1)
{
printf("power is off");
MessageBox(NULL, "power is on" , "NOTICE" ,MB_OK );
}
else if (status.ACLineStatus == 0)
{
printf("power is on");
MessageBox(NULL, "power is off" , "NOTICE" ,MB_OK );
}
else
{
printf("unknown");
MessageBox(NULL, "unknown status" , "ERROR" ,MB_OK);
}
}
break;
default:
MessageBox ( NULL , "nothing" , "------" , MB_OK);
}
break;
case WM_MOUSEMOVE:
if (wParam == MK_LBUTTON)
MessageBox(NULL, "mouse was clicked", "check", MB_OK);
break;
default:
return DefWindowProc(hwnd, msg, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
LPSTR lpCmdLine, int nCmdShow)
{
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
HPOWERNOTIFY notification_1;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc; //signing to the updating service of the operating system
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc))
{
MessageBox(NULL, "Window Registration Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"The title of my window",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 240, 120,
NULL, NULL, hInstance, NULL);
if(!hwnd)
{
MessageBox(NULL, "Window Creation Failed!", "Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
notification_1 = RegisterPowerSettingNotification(hwnd, &GUID_ACDC_POWER_SOURCE, DEVICE_NOTIFY_WINDOW_HANDLE);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Step 3: The Message Loop
while(GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}
答案 0 :(得分:6)
我不明白你为什么要在内核模式下运行这段代码......是不是作为服务运行了?
您应该将程序打包为Windows服务,然后在您的系统上安装该服务。没有必要进入内核级别。