Advertisement
HEX0x29A

Battery Status Informer

Jan 8th, 2015
660
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.98 KB | None | 0 0
  1. #pragma comment(linker,"/MERGE:.rdata=.text")
  2. #pragma comment(linker,"/MERGE:.data=.text")
  3. #pragma comment(linker,"/SECTION:.text,ERW,512")
  4. #pragma comment(linker,"/Od")
  5. #pragma comment(linker,"/NODEFAULTLIB")
  6. #pragma comment(linker,"/CLRUNMANAGEDCODECHECK:NO")
  7. #pragma comment(linker,"/MANIFEST:NO")
  8. #pragma comment(linker,"/MANIFESTUAC:NO")
  9. #pragma comment(linker,"/GS-")
  10. #pragma comment(linker,"/PDBPATH:NONE")
  11. #pragma comment(linker,"/DYNAMICBASE:NO")
  12. #pragma comment(linker,"/ENTRY:WinMain")
  13.  
  14. #include "windows.h"
  15.  
  16. #define Interval       60000
  17. #define MsgCaption     "Информация о батарее"
  18. #define MsgText        "Батарея полностью заряжена. Питание можно отключить.\r\nПродолжить работу программы?"
  19. #define AutorunValue   "BatteryStatusInformer"
  20. #define AutorunKeyName "Software\\Microsoft\\Windows\\CurrentVersion\\Run"
  21. #define MutexStr       "{4406ED85-5FE6-4346-BEF0-88C49FE7A2A2}"
  22.  
  23. UINT   hTimer;
  24. HANDLE Mutex;
  25.  
  26. bool AlreadyRunned();
  27. void InToAutorun();
  28. bool ShowMessage();
  29.  
  30. void __stdcall TimerProc(HWND hWnd, UINT nMsg, UINT nIDEvent, DWORD dwTime)
  31. {
  32.     _SYSTEM_POWER_STATUS sps;
  33.     GetSystemPowerStatus((LPSYSTEM_POWER_STATUS)&sps);
  34.     if ((sps.ACLineStatus == 1) && (sps.BatteryLifePercent = 100))
  35.     {
  36.         KillTimer(NULL, hTimer);
  37.         if (ShowMessage())
  38.             ExitProcess(0);
  39.         hTimer = SetTimer(NULL, 0, Interval, (TIMERPROC)&TimerProc);
  40.     }
  41. }
  42.  
  43. int __stdcall WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdParam, int nCmdShow)
  44. {
  45.     if (AlreadyRunned())
  46.         ExitProcess(0);
  47.     InToAutorun();
  48.     hTimer = SetTimer(NULL, 0, Interval, (TIMERPROC)&TimerProc);
  49.     MSG msg;
  50.     while (GetMessageA(&msg, NULL, 0, 0))
  51.     {
  52.         TranslateMessage(&msg);
  53.         DispatchMessageA(&msg);
  54.     }
  55.     KillTimer(NULL, hTimer);
  56.     ExitProcess(0);
  57.     return 0;
  58. }
  59.  
  60. bool AlreadyRunned()
  61. {
  62.     Mutex = OpenMutexA(MUTEX_ALL_ACCESS, false, MutexStr);
  63.     if (Mutex != 0)
  64.         return true;
  65.     Mutex = CreateMutexA(NULL, false, MutexStr);
  66.     return false;
  67. }
  68.  
  69. void InToAutorun()
  70. {
  71.     DWORD D;
  72.     HKEY  hKey;
  73.     char  Value[2048];
  74.     GetModuleFileNameA(GetModuleHandleA(NULL), (LPSTR)Value, 2048);
  75.     if (RegCreateKeyExA(HKEY_CURRENT_USER, AutorunKeyName, 0, NULL,
  76.         REG_OPTION_NON_VOLATILE, KEY_WRITE, NULL, (PHKEY)&hKey, (LPDWORD)&D) == ERROR_SUCCESS)
  77.     {
  78.         RegSetValueExA(hKey, AutorunValue, 0, REG_SZ, (LPBYTE)Value, sizeof(Value));
  79.         RegCloseKey(hKey);
  80.     }
  81. }
  82.  
  83. bool ShowMessage()
  84. {
  85.     tagMSGBOXPARAMSA params;
  86.     params.cbSize             = sizeof(params);
  87.     params.hwndOwner          = NULL;
  88.     params.hInstance          = GetModuleHandleA(NULL);
  89.     params.lpszText           = MsgText;
  90.     params.lpszCaption        = MsgCaption;
  91.     params.dwStyle            = MB_YESNO | MB_USERICON | MB_TOPMOST;
  92.     params.lpszIcon           = MAKEINTRESOURCEA(101);
  93.     params.dwContextHelpId    = 0;
  94.     params.lpfnMsgBoxCallback = NULL;
  95.     params.dwLanguageId       = LANG_NEUTRAL;
  96.     MessageBeep(MB_ICONINFORMATION);
  97.     return (MessageBoxIndirectA(&params) == IDNO);
  98. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement