netripper

netripper

Dec 21st, 2009
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.44 KB | None | 0 0
  1. // Author:
  2. // Martijn Stolk / NetRipper
  3.  
  4. #include "stdafx.h"
  5. #include <windows.h>
  6.  
  7. // From MSDN: http://msdn.microsoft.com/en-us/library/ms838354.aspx
  8. // Stole ExtEscape idea from HaRET :-)
  9. #define QUERYESCSUPPORT 8
  10. #define SETPOWERMANAGEMENT 6147
  11. #define GETPOWERMANAGEMENT 6148
  12.  
  13. typedef struct VIDEOPOWER_MANAGEMENT {
  14.     ULONG Length;
  15.     ULONG DPMSVersion;
  16.     ULONG PowerState;
  17. } VIDEO_POWER_MANAGEMENT, *PVIDEO_POWER_MANAGEMENT;
  18.  
  19. int _tmain(int argc, _TCHAR* argv[])
  20. {
  21.     // Create an event for ourselves, so that we can be turned off as well
  22.     HANDLE appEvent = CreateEvent(NULL, TRUE, FALSE, L"KeypadLedControlApp");
  23.     if (appEvent == NULL)
  24.     {
  25.         MessageBox(NULL, L"Couldn't create an event for ourselves", L"Error", MB_OK);
  26.         return 1;
  27.     }
  28.  
  29.     // If the app is already running, we set the event so that it will close itself.
  30.     if (GetLastError() == ERROR_ALREADY_EXISTS)
  31.     {
  32.         SetEvent(appEvent);
  33.         CloseHandle(appEvent);
  34.         return 1;
  35.     }
  36.  
  37.     // Get the keyped led event
  38.     HANDLE iconEvent = CreateEvent(NULL, TRUE, FALSE, L"IconLedOnEvent");
  39.     if (iconEvent == NULL)
  40.     {
  41.         // Oops, error creating the event?
  42.         MessageBox(NULL, L"Couldn't create the keypad light event", L"Error", MB_OK);
  43.         return 1;
  44.     }
  45.     if (GetLastError() != ERROR_ALREADY_EXISTS)
  46.     {
  47.         // Oops, we created the event, while it should already be there.
  48.         MessageBox(NULL, L"This application does not support this device", L"Error", MB_OK);
  49.         CloseHandle(iconEvent);
  50.         return 1;
  51.     }
  52.  
  53.     // Check if the driver supports power management information
  54.     HDC gdc = GetDC(NULL);
  55.     int iESC = GETPOWERMANAGEMENT;
  56.     if (ExtEscape(gdc, QUERYESCSUPPORT, sizeof(int), (LPCSTR)&iESC, 0, NULL) == 0)
  57.     {
  58.         MessageBox(NULL, L"This device does not support checking for LCD state", L"Error", MB_OK);
  59.         CloseHandle(iconEvent);
  60.         ReleaseDC(NULL, gdc);
  61.         return 1;
  62.     }
  63.  
  64.     VIDEO_POWER_MANAGEMENT pm;
  65.     pm.Length = sizeof(pm);
  66.     int ret;
  67.     while (true)
  68.     {
  69.         // Get current power management information
  70.         ret = ExtEscape(gdc, GETPOWERMANAGEMENT, 0, NULL, pm.Length, (LPSTR)&pm);
  71.  
  72.         // If the LCD is on, set the event so that the lights go on
  73.         if (pm.PowerState == 1)
  74.         {
  75.             SetEvent(iconEvent);
  76.         }
  77.        
  78.         // We'll keep on running this app as long as the event hasn't been set (by starting this app again)
  79.         if (WaitForSingleObject(appEvent, 500) != WAIT_TIMEOUT)
  80.         {
  81.             break;
  82.         }
  83.     }
  84.  
  85.     // Cleanup
  86.     ReleaseDC(NULL, gdc);
  87.     CloseHandle(iconEvent);
  88.     CloseHandle(appEvent);
  89.    
  90.     return 0;
  91. }
  92.  
  93.  
Add Comment
Please, Sign In to add comment