2012-06-17 19 views
0

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

回答

6

我不明白你爲什麼要運行在內核模式下的代碼...是不夠作爲服務運行?

你應該收拾你的程序作爲Windows服務,然後在系統上安裝的服務。沒有必要去內核級別。

+0

我該怎麼辦呢?這是我的功課 – user1386966

+1

看看這個教程的一部分:http://www.devx.com/cplus/Article/9857 – aleroot

+0

謝謝,我會看看我 – user1386966

相關問題