Guest User

USB detection and removal ERROR

a guest
May 19th, 2012
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 10.57 KB | None | 0 0
  1. # define WINVER 0x501
  2. #include <stdio.h>
  3. #include <windows.h>
  4. #include <dbt.h>
  5. //#include <wtsapi32.h>
  6. #include <tchar.h>
  7. //#include <winsvc.h>
  8. //#include <Winuser.h>
  9.  
  10. #define WTS_CURRENT_SERVER ((HANDLE)NULL)
  11. #define WTS_CURRENT_SERVER_HANDLE ((HANDLE)NULL)
  12. #define WTS_CURRENT_SERVER_NAME (NULL)
  13. #define WTS_CURRENT_SESSION ((DWORD)-1)
  14. #define SLEEP_TIME   5000
  15.  
  16.  
  17. typedef BOOL ( *fptr1 ) ( HANDLE ,DWORD , LPTSTR, DWORD , LPTSTR , DWORD , DWORD , DWORD , DWORD* , BOOL );
  18. typedef DWORD ( *fptr2 ) ( void );
  19. void start();
  20. DWORD ControlHandler(DWORD, DWORD, LPVOID, LPVOID);
  21.  
  22. // Declarations for popup message at stop request
  23. fptr1 WTSSendMessage;
  24. DWORD res;
  25. fptr2 WTSGetActiveConsoleSessionId;
  26. //LPTSTR lpszTitle = L"Time Change";
  27. //LPTSTR lpszText = L"Sample";
  28. //const WTS_CURRENT_SERVER_HANDLE = 0;
  29.  
  30.         TCHAR* serviceName = TEXT ( "Beeper Service" );
  31.         SERVICE_STATUS serviceStatus;
  32.         SERVICE_STATUS_HANDLE serviceStatusHandle = 0;
  33.         HANDLE stopServiceEvent = 0;
  34.         HDEVNOTIFY hDeviceNotify;
  35.         FILE* Log;
  36.  
  37. DWORD ControlHandler( DWORD dwControl, DWORD dwEventType,
  38.                                  LPVOID lpEventData, LPVOID lpContext )
  39. {
  40.  
  41.             Log = fopen("C:\\mss.log", "a");
  42.             fprintf(Log, "ControlHandler call with %d:%d\n", dwControl, dwEventType);
  43.  
  44.             HINSTANCE hinstLib = LoadLibrary( "C:\\MinGW\\msys\\1.0\\home\\user\\Sample\\NewF\\wtsapi32.dll" );
  45.             //WTSGetActiveConsoleSessionId = (fptr2)GetProcAddress(hinstLib, "WTSGetActiveConsoleSessionIdW");
  46.             //DWORD dwSession = WTSGetActiveConsoleSessionId();
  47.     switch ( dwControl )
  48.     {
  49.         case SERVICE_CONTROL_INTERROGATE:
  50.             break;
  51.  
  52.         case SERVICE_CONTROL_SHUTDOWN:
  53.         case SERVICE_CONTROL_STOP:
  54.  
  55.                         WTSSendMessage = (fptr1)GetProcAddress(hinstLib, "WTSSendMessageW");
  56.                         BOOL ret = WTSSendMessage(WTS_CURRENT_SERVER_HANDLE,WTS_CURRENT_SESSION, (LPTSTR)L"WARNING", 8*2 ,(LPTSTR)L"USB'S WONT DETECT AUTOMATICALLY, PLEASE START THE SERVICE AGAIN", 74*2 ,MB_ICONERROR|MB_TOPMOST|MB_SETFOREGROUND, 0, &res, FALSE);
  57.             //BOOL ret = WTSSendMessage(WTS_CURRENT_SERVER_HANDLE,WTS_CURRENT_SESSION, (LPWSTR)L"WARNING", 8*2 ,(LPWSTR)L"USB'S WONT DETECT AUTOMATICALLY, PLEASE START THE SERVICE AGAIN", 74*2 ,MB_ICONERROR|MB_TOPMOST|MB_SETFOREGROUND, 0, &res, FALSE);
  58.             /*if ( ret == 0  ) {
  59.                           printf ( "ERROR\n" );
  60.                         }*/
  61.             //system ( "C:\\MinGW\\msys\\1.0\\home\\mkumar\\Sample\\WIN.exe" );
  62.  
  63.  
  64.             serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
  65.             SetServiceStatus( serviceStatusHandle, &serviceStatus );
  66.                         SetEvent( stopServiceEvent );
  67.             return;
  68.  
  69.         case SERVICE_CONTROL_PAUSE:
  70.             break;
  71.  
  72.         case SERVICE_CONTROL_CONTINUE:
  73.             break;
  74.         case SERVICE_CONTROL_DEVICEEVENT:
  75.              SetServiceStatus( serviceStatusHandle, &serviceStatus );
  76.                      fprintf(Log, "SERVICE_CONTROL_DEVICEEVENT! ");
  77.                       switch(dwEventType)
  78.                                {
  79.                                 case DBT_DEVICEARRIVAL:
  80.                                   // Handle
  81.  
  82.                                   fprintf(Log, "DBT_DEVICEARRIVAL\n");
  83.                                   break;
  84.                                 case DBT_DEVICEREMOVECOMPLETE:
  85.                                   fprintf(Log, "DBT_DEVICEREMOVECOMPLETE\n");
  86.                                   // Handle
  87.                                   break;
  88.                                }
  89.  
  90.         default:
  91.                 fprintf(Log, "Unknown dwControl: %d\n", dwControl);
  92.             if ( dwControl >= 128 && dwControl <= 255 )
  93.                 // user defined control code
  94.                 break;
  95.             else
  96.                 // unrecognised control code
  97.                 break;
  98.     }
  99.  
  100.     SetServiceStatus( serviceStatusHandle, &serviceStatus );
  101. }
  102.  
  103. void WINAPI ServiceMain( DWORD argc, LPTSTR *argv)
  104. {
  105.     // initialise service status
  106.     Log = fopen("C:\\mss.log", "a");
  107.     fprintf(Log, "Entering ServiceMain function...\n");
  108.     serviceStatus.dwServiceType = SERVICE_WIN32;
  109.     serviceStatus.dwCurrentState = SERVICE_STOPPED;
  110.     serviceStatus.dwControlsAccepted = 0;
  111.     serviceStatus.dwWin32ExitCode = NO_ERROR;
  112.     serviceStatus.dwServiceSpecificExitCode = NO_ERROR;
  113.     serviceStatus.dwCheckPoint = 0;
  114.     serviceStatus.dwWaitHint = 0;
  115.  
  116.     serviceStatusHandle = RegisterServiceCtrlHandlerEx( serviceName,(LPHANDLER_FUNCTION_EX)ControlHandler,0 );
  117.  
  118.     if ( serviceStatusHandle )
  119.     {
  120.         // service is starting
  121.         serviceStatus.dwCurrentState = SERVICE_START_PENDING;
  122.         SetServiceStatus( serviceStatusHandle, &serviceStatus );
  123.  
  124.         // Some initialization
  125.                fprintf(Log, "Registering device notification...\n");
  126.                DEV_BROADCAST_DEVICEINTERFACE NotificationFilter;
  127.                hDeviceNotify = NULL;
  128.  
  129.                static const GUID GuidForMYDevice =
  130.                       { 0x4D36E978, 0xE325, 0x11CE, {0xBF, 0xC1, 0x08, 0x00, 0x2B, 0xE1, 0x03, 0x18 } };
  131.  
  132.                       ZeroMemory(&NotificationFilter, sizeof(NotificationFilter));
  133.  
  134.                       NotificationFilter.dbcc_size = sizeof(DEV_BROADCAST_DEVICEINTERFACE);
  135.                       NotificationFilter.dbcc_devicetype = DBT_DEVTYP_DEVICEINTERFACE;
  136.                       NotificationFilter.dbcc_classguid = GuidForMYDevice;
  137.  
  138.                       hDeviceNotify = RegisterDeviceNotification( (HANDLE)serviceStatusHandle, &NotificationFilter, DEVICE_NOTIFY_SERVICE_HANDLE | DEVICE_NOTIFY_ALL_INTERFACE_CLASSES );
  139.  
  140.                       fprintf(Log, "RegisterDeviceNotification returned %p\n", hDeviceNotify);
  141.  
  142.  
  143.         // do initialisation here
  144.         stopServiceEvent = CreateEvent( 0, FALSE, FALSE, 0 );
  145.  
  146.         // running
  147.         serviceStatus.dwControlsAccepted |= (SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
  148.         serviceStatus.dwCurrentState = SERVICE_RUNNING;
  149.         SetServiceStatus( serviceStatusHandle, &serviceStatus );
  150.         fprintf(Log, "Running...\n");
  151.                 /*while(serviceStatus.dwCurrentState == SERVICE_RUNNING)
  152.                  {
  153.                    Sleep(SLEEP_TIME);
  154.                   }*/
  155.  
  156.         do
  157.         {
  158.             Beep( 50, 10 );
  159.             //system ( "C:\\MinGW\\msys\\1.0\\home\\mkumar\\Sample\\WIN.exe" );
  160.             //printf ( "HI\n" );
  161.         }
  162.         while ( WaitForSingleObject( stopServiceEvent, 5000 ) == WAIT_TIMEOUT );
  163.                  //system ( "C:\\MinGW\\msys\\1.0\\home\\mkumar\\Sample\\WIN.exe" );
  164.         // service was stopped
  165.         serviceStatus.dwCurrentState = SERVICE_STOP_PENDING;
  166.         SetServiceStatus( serviceStatusHandle, &serviceStatus );
  167.  
  168.         // do cleanup here
  169.         CloseHandle( stopServiceEvent );
  170.         stopServiceEvent = 0;
  171.  
  172.         // service is now stopped
  173.         serviceStatus.dwControlsAccepted &= ~(SERVICE_ACCEPT_STOP | SERVICE_ACCEPT_SHUTDOWN);
  174.         serviceStatus.dwCurrentState = SERVICE_STOPPED;
  175.         SetServiceStatus( serviceStatusHandle, &serviceStatus );
  176.     }
  177. }
  178.  
  179. void RunService()
  180. {      
  181.         fprintf(Log, "In Run service...\n");
  182.         SERVICE_TABLE_ENTRY serviceTable[] =
  183.     {
  184.         { serviceName, ServiceMain },
  185.         { 0, 0 }
  186.     };
  187.  
  188.     //BOOL success;
  189.    // Register the service with the Service Control Manager
  190.         fprintf(Log, "Invoking StartServiceCtrlDispatcher...\n");
  191.         StartServiceCtrlDispatcher(serviceTable);
  192.  
  193. }
  194.  
  195.  
  196. void InstallService()
  197. {
  198.         printf ( "In install service\n" );
  199.     SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CREATE_SERVICE );
  200.         printf ( "after sc handel\n" );
  201.     if ( serviceControlManager )
  202.     {
  203.                 //Log = fopen("C:\\mss.log", "a");
  204.                 //printf ( "In if\n" );
  205.                 fprintf(Log, "Installing Service...\n");
  206.                  printf ( "after fprintf\n" );
  207.         TCHAR path[ _MAX_PATH + 1 ];
  208.         if ( GetModuleFileName( 0, path, sizeof(path)/sizeof(path[0]) ) > 0 )
  209.         {
  210.             SC_HANDLE service = CreateService( serviceControlManager,
  211.                             serviceName, serviceName,
  212.                             SERVICE_ALL_ACCESS, SERVICE_WIN32_OWN_PROCESS,
  213.                             SERVICE_AUTO_START, SERVICE_ERROR_IGNORE, path,
  214.                             0, 0, 0, 0, 0 );
  215.             if ( service )
  216.                 CloseServiceHandle( service );
  217.         }
  218.  
  219.         CloseServiceHandle( serviceControlManager );
  220.     }
  221. }
  222.  
  223. void UninstallService()
  224. {
  225.     SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CONNECT );
  226.  
  227.     if ( serviceControlManager )
  228.     {      
  229.                 //Log = fopen("C:\\mss.log", "a");
  230.                 fprintf(Log, "UnInstalling Service...\n");
  231.         SC_HANDLE service = OpenService( serviceControlManager,
  232.             serviceName, SERVICE_QUERY_STATUS | DELETE );
  233.         if ( service )
  234.         {
  235.             SERVICE_STATUS serviceStatus;
  236.             if ( QueryServiceStatus( service, &serviceStatus ) )
  237.             {
  238.                 if ( serviceStatus.dwCurrentState == SERVICE_STOPPED )
  239.                     DeleteService( service );
  240.             }
  241.  
  242.             CloseServiceHandle( service );
  243.         }
  244.  
  245.         CloseServiceHandle( serviceControlManager );
  246.     }
  247. }
  248.  
  249. void start ( ) {
  250.  
  251.  
  252.   SC_HANDLE serviceControlManager = OpenSCManager( 0, 0, SC_MANAGER_CONNECT );
  253.  
  254.     if ( serviceControlManager )
  255.     {
  256.                 //Log = fopen("C:\\mss.log", "a");
  257.                 fprintf(Log, "Start Service...\n");
  258.         SC_HANDLE service = OpenService( serviceControlManager,
  259.             serviceName, SERVICE_ALL_ACCESS );
  260.         if ( service )
  261.         {
  262.                     SERVICE_STATUS serviceStatus;
  263.             if ( QueryServiceStatus( service, &serviceStatus ) )
  264.             {
  265.                 if ( serviceStatus.dwCurrentState == SERVICE_STOPPED )
  266.                 {
  267.                                             //StartService( service, 0, NULL );
  268.                                        if (!StartService(service, 0, NULL) )
  269.                                               {
  270.                                                 printf("StartService failed (%d)\n", GetLastError());
  271.                                               } else {
  272.                                                     serviceStatus.dwCurrentState = SERVICE_RUNNING;
  273.                                                  }      
  274.                                 }
  275.  
  276.                 CloseServiceHandle( service );
  277.                 }
  278.  
  279.         CloseServiceHandle( serviceControlManager );
  280.             }
  281.        }
  282. }
  283.  
  284.  
  285.  
  286. int _tmain( int argc, TCHAR* argv[] )
  287.  
  288. {
  289.         Log = fopen("C:\\mss.log", "a");
  290.         //fprintf(Log, "Logging started...\n");
  291.  
  292.         //fprintf(Log, "Service executable started...\n");
  293.  
  294.  
  295.     if ( argc > 1 && lstrcmpi( argv[1], TEXT ( "install" ) ) == 0 )
  296.  
  297.     {
  298.                   printf ( "In install\n");
  299.         InstallService();
  300.     }
  301.     else if ( argc > 1 && lstrcmpi( argv[1], TEXT ( "uninstall" ) ) == 0 )
  302.     {         printf ( "In uninstall\n");
  303.         UninstallService();
  304.     }
  305.     else if ( argc > 1 && lstrcmpi( argv[1], TEXT ( "start" ) ) == 0 )
  306.     {
  307.           printf ( "In uninstall\n");
  308.           start();
  309.         }
  310.  
  311.     else
  312.     {
  313.           RunService();
  314.         }
  315.  
  316.         //fprintf(Log, "Logging finished!\n");
  317.     return 0;
  318. }
Advertisement
Add Comment
Please, Sign In to add comment