我有一个 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;
}