Pastebin launched a little side project called VERYVIRAL.com, check it out ;-) Want more features on Pastebin? Sign Up, it's FREE!

mss

By: Surendil on Oct 12th, 2011  |  syntax: C  |  size: 3.86 KB  |  views: 142  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #define UNICODE
  2. #define WINVER 0x502
  3.  
  4. #include <stdio.h>
  5. #include <windows.h>
  6. #include <dbt.h>
  7.  
  8. #define SERVICE_NAME L"MediaSpy Service"
  9. #define SLEEP_TIME   5000
  10.  
  11. SERVICE_STATUS Status;
  12. SERVICE_STATUS_HANDLE hStatus;
  13. HDEVNOTIFY hDeviceNotify;
  14. FILE* Log;
  15.  
  16. void ServiceMain();
  17. DWORD ControlHandler(DWORD, DWORD, LPVOID, LPVOID);
  18.  
  19. void main()
  20. {
  21.   Log = fopen("C:\\mss.log", "w");
  22.   fprintf(Log, "Logging started...\n");
  23.  
  24.   fprintf(Log, "Service executable started...\n");
  25.   SERVICE_TABLE_ENTRY ServiceTable[1];
  26.   ServiceTable[0].lpServiceName = SERVICE_NAME;
  27.   ServiceTable[0].lpServiceProc = (LPSERVICE_MAIN_FUNCTION)ServiceMain;
  28.  
  29.   fprintf(Log, "Invoking StartServiceCtrlDispatcher...\n");
  30.   StartServiceCtrlDispatcher(ServiceTable);
  31.  
  32.   fprintf(Log, "Logging finished!\n");
  33.   fclose(Log);
  34. }
  35.  
  36. void ServiceMain()
  37. {
  38.   fprintf(Log, "Entering ServiceMain function...\n");
  39.   Status.dwServiceType       = SERVICE_WIN32_OWN_PROCESS;
  40.   Status.dwCurrentState      = SERVICE_START_PENDING;
  41.   Status.dwControlsAccepted  = SERVICE_ACCEPT_STOP |
  42.                                       SERVICE_ACCEPT_SHUTDOWN;
  43.   Status.dwWin32ExitCode     = 0;
  44.   Status.dwServiceSpecificExitCode = 0;
  45.   Status.dwCheckPoint = 0;
  46.   Status.dwWaitHint   = 0;
  47.  
  48.   hStatus =
  49.     RegisterServiceCtrlHandlerEx( SERVICE_NAME,
  50.                                   (LPHANDLER_FUNCTION_EX)ControlHandler,
  51.                                   0 );
  52.   fprintf(Log, "RegisterServiceCtrlHandlerEx returned %p\n", hStatus);
  53.  
  54.   if(hStatus == (SERVICE_STATUS_HANDLE)0)
  55.     ; // Error
  56.  
  57.   SetServiceStatus(hStatus, &Status);
  58.  
  59.   // Some initialization
  60.   fprintf(Log, "Registering device notification...\n");
  61.   DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
  62.   hDeviceNotify = NULL;
  63.  
  64.   static const GUID GuidDevInterfaceDisk =
  65.   { 0x53F56307, 0xB6BF, 0x11D0,
  66.     { 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B } };
  67.  
  68.   ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
  69.  
  70.   NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  71.   NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  72.   // NotificationFilter.dbcc_classguid = GuidDevInterfaceDisk;
  73.  
  74.   hDeviceNotify =
  75.     RegisterDeviceNotification( (HANDLE)hStatus,
  76.                                 &NotificationFilter,
  77.                                 DEVICE_NOTIFY_SERVICE_HANDLE |
  78.                                 DEVICE_NOTIFY_ALL_INTERFACE_CLASSES );
  79.  
  80.   fprintf(Log, "RegisterDeviceNotification returned %p\n", hDeviceNotify);
  81.  
  82.   if(hDeviceNotify == NULL)
  83.     ; // Error
  84.  
  85.  
  86.   Status.dwCurrentState = SERVICE_RUNNING;
  87.   SetServiceStatus(hStatus, &Status);
  88.  
  89.   fprintf(Log, "Running...\n");
  90.   while(Status.dwCurrentState == SERVICE_RUNNING)
  91.   {
  92.     Sleep(SLEEP_TIME);
  93.   }
  94. }
  95.  
  96. DWORD ControlHandler( DWORD dwControl, DWORD dwEventType,
  97.                              LPVOID lpEventData, LPVOID lpContext )
  98. {
  99.   fprintf(Log, "ControlHandler call with %d:%d\n", dwControl, dwEventType);
  100.   switch(dwControl)
  101.   {
  102.   case SERVICE_CONTROL_STOP:
  103.     UnregisterDeviceNotification(hDeviceNotify);
  104.     Status.dwCurrentState = SERVICE_STOPPED;
  105.     SetServiceStatus(hStatus, &Status);
  106.     return NO_ERROR;
  107.   case SERVICE_CONTROL_SHUTDOWN:
  108.     UnregisterDeviceNotification(hDeviceNotify);
  109.     Status.dwCurrentState = SERVICE_STOPPED;
  110.     SetServiceStatus(hStatus, &Status);
  111.     return NO_ERROR;
  112.   case SERVICE_CONTROL_DEVICEEVENT:
  113.     fprintf(Log, "SERVICE_CONTROL_DEVICEEVENT! ");
  114.     switch(dwEventType)
  115.     {
  116.     case DBT_DEVICEARRIVAL:
  117.       // Handle
  118.       fprintf(Log, "DBT_DEVICEARRIVAL\n");
  119.       break;
  120.     case DBT_DEVICEREMOVECOMPLETE:
  121.       fprintf(Log, "DBT_DEVICEREMOVECOMPLETE\n");
  122.       // Handle
  123.       break;
  124.     }
  125.   default:
  126.     fprintf(Log, "Unknown dwControl: %d\n", dwControl);
  127.     SetServiceStatus(hStatus, &Status);
  128.     break;
  129.   }
  130.   return NO_ERROR;
  131. }