Advertisement
Surendil

mss.c

Oct 11th, 2011
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.74 KB | None | 0 0
  1. #define UNICODE
  2. #define WINVER 0x501
  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.    
  65.    
  66.     static const GUID GuidDevInterfaceDisk =
  67.     { 0x53F56307, 0xB6BF, 0x11D0,
  68.         { 0x94, 0xF2, 0x00, 0xA0, 0xC9, 0x1E, 0xFB, 0x8B } };
  69.    
  70.     ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
  71.    
  72.     NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  73.     NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  74.     // NotificationFilter.dbcc_classguid = GuidDevInterfaceDisk;
  75.    
  76.     hDeviceNotify =
  77.         RegisterDeviceNotification( (HANDLE)hStatus,
  78.                                     &NotificationFilter,
  79.                                     DEVICE_NOTIFY_SERVICE_HANDLE |
  80.                                     DEVICE_NOTIFY_ALL_INTERFACE_CLASSES );
  81.    
  82.     fprintf(Log, "RegisterDeviceNotification returned %p\n", hDeviceNotify);
  83.    
  84.     if(hDeviceNotify == NULL)
  85.         ; // Error
  86.    
  87.    
  88.     Status.dwCurrentState = SERVICE_RUNNING;
  89.   SetServiceStatus(hStatus, &Status);
  90.    
  91.     fprintf(Log, "Running...\n");
  92.     while(Status.dwCurrentState == SERVICE_RUNNING)
  93.     {
  94.         Sleep(SLEEP_TIME);
  95.     }
  96. }
  97.  
  98. DWORD ControlHandler( DWORD dwControl, DWORD dwEventType,
  99.                              LPVOID lpEventData, LPVOID lpContext )
  100. {
  101.     fprintf(Log, "ControlHandler call with %d %d\n", dwControl, dwEventType);
  102.     switch(dwControl)
  103.     {
  104.     case SERVICE_CONTROL_STOP:
  105.         UnregisterDeviceNotification(hDeviceNotify);
  106.         Status.dwCurrentState = SERVICE_STOPPED;
  107.         SetServiceStatus(hStatus, &Status);
  108.         return NO_ERROR;
  109.     case SERVICE_CONTROL_SHUTDOWN:
  110.         UnregisterDeviceNotification(hDeviceNotify);
  111.         Status.dwCurrentState = SERVICE_STOPPED;
  112.         SetServiceStatus(hStatus, &Status);
  113.         return NO_ERROR;
  114.     case SERVICE_CONTROL_DEVICEEVENT:
  115.         fprintf(Log, "SERVICE_CONTROL_DEVICEEVENT! ");
  116.         switch(dwEventType)
  117.         {
  118.         case DBT_DEVICEARRIVAL:
  119.             // Handle
  120.             fprintf(Log, "DBT_DEVICEARRIVAL\n");
  121.             break;
  122.         case DBT_DEVICEREMOVECOMPLETE:
  123.             fprintf(Log, "DBT_DEVICEREMOVECOMPLETE\n");
  124.             // Handle
  125.             break;
  126.         }
  127.     default:
  128.         fprintf(Log, "Unknown dwControl: %d\n", dwControl);
  129.         SetServiceStatus(hStatus, &Status);
  130.         break;
  131.     }
  132.     return NO_ERROR;
  133. }
  134.  
  135.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement