Advertisement
xSpeTz-

Untitled

Nov 25th, 2016
290
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 16.14 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <windows.h>
  3.  
  4. void PrintUsage();
  5.  
  6. SC_HANDLE schSCManager;
  7. SC_HANDLE schService;
  8. char * szSvcName;
  9.  
  10. void DoCreateSvc()
  11. {
  12.     SC_HANDLE schSCManager;
  13.     SC_HANDLE schService;
  14. //    TCHAR szPath[MAX_PATH];
  15.  
  16. //    if( !GetModuleFileName( "", szPath, MAX_PATH ) )
  17. //    {
  18. //        printf("Cannot install service (%d)\n", (unsigned int)GetLastError());
  19. //        return;
  20. //    }
  21.  
  22.     // Get a handle to the SCM database.
  23.  
  24.     schSCManager = OpenSCManager(
  25.                        NULL,                    // local computer
  26.                        NULL,                    // ServicesActive database
  27.                        SC_MANAGER_ALL_ACCESS);  // full access rights
  28.  
  29.     if (NULL == schSCManager)
  30.     {
  31.         printf("OpenSCManager failed (%d)\n", (unsigned int)GetLastError());
  32.         return;
  33.     }
  34.  
  35.     // Create the service
  36.  
  37.     char aCurrentDirectory[515];
  38.     char aPath[1024];
  39.     GetCurrentDirectory( 512, aCurrentDirectory);
  40.     _snprintf(aPath, 1022, "%s\\%s", aCurrentDirectory, szSvcName);
  41.  
  42.     // Create the service
  43.     schService = CreateService(schSCManager,
  44.                                szSvcName,
  45.                                szSvcName,
  46.                                SERVICE_ALL_ACCESS,
  47.                                SERVICE_KERNEL_DRIVER,
  48.                                SERVICE_DEMAND_START,
  49.                                SERVICE_ERROR_NORMAL,
  50.                                aPath,
  51.                                NULL,
  52.                                NULL,
  53.                                NULL,
  54.                                NULL,
  55.                                NULL);
  56.  
  57. //    schService = CreateService(
  58. //        schSCManager,              // SCM database
  59. //        SVCNAME,                   // name of service
  60. //        SVCNAME,                   // service name to display
  61. //        SERVICE_ALL_ACCESS,        // desired access
  62. //        SERVICE_WIN32_OWN_PROCESS, // service type
  63. //        SERVICE_DEMAND_START,      // start type
  64. //        SERVICE_ERROR_NORMAL,      // error control type
  65. //        szPath,                    // path to service's binary
  66. //        NULL,                      // no load ordering group
  67. //        NULL,                      // no tag identifier
  68. //        NULL,                      // no dependencies
  69. //        NULL,                      // LocalSystem account
  70. //        NULL);                     // no password
  71.  
  72.     if (schService == NULL)
  73.     {
  74.         printf("CreateService failed (%d)\n", (unsigned int)GetLastError());
  75.         CloseServiceHandle(schSCManager);
  76.         return;
  77.     }
  78.     else printf("Service installed successfully\n");
  79.  
  80.     CloseServiceHandle(schService);
  81.     CloseServiceHandle(schSCManager);
  82. }
  83.  
  84. void __stdcall DoStartSvc()
  85. {
  86.     SERVICE_STATUS_PROCESS ssStatus;
  87.     DWORD dwOldCheckPoint;
  88.     DWORD dwStartTickCount;
  89.     DWORD dwWaitTime;
  90.     DWORD dwBytesNeeded;
  91.  
  92.     // Get a handle to the SCM database.
  93.  
  94.     schSCManager = OpenSCManager(
  95.                        NULL,                    // local computer
  96.                        NULL,                    // servicesActive database
  97.                        SC_MANAGER_ALL_ACCESS);  // full access rights
  98.  
  99.     if (NULL == schSCManager)
  100.     {
  101.         printf("OpenSCManager failed (%d)\n", (unsigned int)GetLastError());
  102.         return;
  103.     }
  104.  
  105.     // Get a handle to the service.
  106.  
  107.     schService = OpenService(
  108.                      schSCManager,         // SCM database
  109.                      szSvcName,            // name of service
  110.                      SERVICE_ALL_ACCESS);  // full access
  111.  
  112.     if (schService == NULL)
  113.     {
  114.         printf("OpenService failed (%d)\n", (unsigned int)GetLastError());
  115.         CloseServiceHandle(schSCManager);
  116.         return;
  117.     }
  118.  
  119.     // Check the status in case the service is not stopped.
  120.  
  121.     if (!QueryServiceStatusEx(
  122.                 schService,                     // handle to service
  123.                 SC_STATUS_PROCESS_INFO,         // information level
  124.                 (LPBYTE) &ssStatus,             // address of structure
  125.                 sizeof(SERVICE_STATUS_PROCESS), // size of structure
  126.                 &dwBytesNeeded ) )              // size needed if buffer is too small
  127.     {
  128.         printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
  129.         CloseServiceHandle(schService);
  130.         CloseServiceHandle(schSCManager);
  131.         return;
  132.     }
  133.  
  134.     // Check if the service is already running. It would be possible
  135.     // to stop the service here, but for simplicity this example just returns.
  136.  
  137.     if(ssStatus.dwCurrentState != SERVICE_STOPPED && ssStatus.dwCurrentState != SERVICE_STOP_PENDING)
  138.     {
  139.         printf("Cannot start the service because it is already running\n");
  140.         CloseServiceHandle(schService);
  141.         CloseServiceHandle(schSCManager);
  142.         return;
  143.     }
  144.  
  145.     // Save the tick count and initial checkpoint.
  146.  
  147.     dwStartTickCount = GetTickCount();
  148.     dwOldCheckPoint = ssStatus.dwCheckPoint;
  149.  
  150.     // Wait for the service to stop before attempting to start it.
  151.  
  152.     while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
  153.     {
  154.         // Do not wait longer than the wait hint. A good interval is
  155.         // one-tenth of the wait hint but not less than 1 second
  156.         // and not more than 10 seconds.
  157.  
  158.         dwWaitTime = ssStatus.dwWaitHint / 10;
  159.  
  160.         if( dwWaitTime < 1000 )
  161.             dwWaitTime = 1000;
  162.         else if ( dwWaitTime > 10000 )
  163.             dwWaitTime = 10000;
  164.  
  165.         Sleep( dwWaitTime );
  166.  
  167.         // Check the status until the service is no longer stop pending.
  168.  
  169.         if (!QueryServiceStatusEx(
  170.                     schService,                     // handle to service
  171.                     SC_STATUS_PROCESS_INFO,         // information level
  172.                     (LPBYTE) &ssStatus,             // address of structure
  173.                     sizeof(SERVICE_STATUS_PROCESS), // size of structure
  174.                     &dwBytesNeeded ) )              // size needed if buffer is too small
  175.         {
  176.             printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
  177.             CloseServiceHandle(schService);
  178.             CloseServiceHandle(schSCManager);
  179.             return;
  180.         }
  181.  
  182.         if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
  183.         {
  184.             // Continue to wait and check.
  185.  
  186.             dwStartTickCount = GetTickCount();
  187.             dwOldCheckPoint = ssStatus.dwCheckPoint;
  188.         }
  189.         else
  190.         {
  191.             if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
  192.             {
  193.                 printf("Timeout waiting for service to stop\n");
  194.                 CloseServiceHandle(schService);
  195.                 CloseServiceHandle(schSCManager);
  196.                 return;
  197.             }
  198.         }
  199.     }
  200.  
  201.     // Attempt to start the service.
  202.  
  203.     if (!StartService(
  204.                 schService,  // handle to service
  205.                 0,           // number of arguments
  206.                 NULL) )      // no arguments
  207.     {
  208.         printf("StartService failed (%d)\n", (unsigned int)GetLastError());
  209.         CloseServiceHandle(schService);
  210.         CloseServiceHandle(schSCManager);
  211.         return;
  212.     }
  213.     else printf("Service start pending...\n");
  214.  
  215.     // Check the status until the service is no longer start pending.
  216.  
  217.     if (!QueryServiceStatusEx(
  218.                 schService,                     // handle to service
  219.                 SC_STATUS_PROCESS_INFO,         // info level
  220.                 (LPBYTE) &ssStatus,             // address of structure
  221.                 sizeof(SERVICE_STATUS_PROCESS), // size of structure
  222.                 &dwBytesNeeded ) )              // if buffer too small
  223.     {
  224.         printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
  225.         CloseServiceHandle(schService);
  226.         CloseServiceHandle(schSCManager);
  227.         return;
  228.     }
  229.  
  230.     // Save the tick count and initial checkpoint.
  231.  
  232.     dwStartTickCount = GetTickCount();
  233.     dwOldCheckPoint = ssStatus.dwCheckPoint;
  234.  
  235.     while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
  236.     {
  237.         // Do not wait longer than the wait hint. A good interval is
  238.         // one-tenth the wait hint, but no less than 1 second and no
  239.         // more than 10 seconds.
  240.  
  241.         dwWaitTime = ssStatus.dwWaitHint / 10;
  242.  
  243.         if( dwWaitTime < 1000 )
  244.             dwWaitTime = 1000;
  245.         else if ( dwWaitTime > 10000 )
  246.             dwWaitTime = 10000;
  247.  
  248.         Sleep( dwWaitTime );
  249.  
  250.         // Check the status again.
  251.  
  252.         if (!QueryServiceStatusEx(
  253.                     schService,             // handle to service
  254.                     SC_STATUS_PROCESS_INFO, // info level
  255.                     (LPBYTE) &ssStatus,             // address of structure
  256.                     sizeof(SERVICE_STATUS_PROCESS), // size of structure
  257.                     &dwBytesNeeded ) )              // if buffer too small
  258.         {
  259.             printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
  260.             break;
  261.         }
  262.  
  263.         if ( ssStatus.dwCheckPoint > dwOldCheckPoint )
  264.         {
  265.             // Continue to wait and check.
  266.  
  267.             dwStartTickCount = GetTickCount();
  268.             dwOldCheckPoint = ssStatus.dwCheckPoint;
  269.         }
  270.         else
  271.         {
  272.             if(GetTickCount()-dwStartTickCount > ssStatus.dwWaitHint)
  273.             {
  274.                 // No progress made within the wait hint.
  275.                 break;
  276.             }
  277.         }
  278.     }
  279.  
  280.     // Determine whether the service is running.
  281.  
  282.     if (ssStatus.dwCurrentState == SERVICE_RUNNING)
  283.     {
  284.         printf("Service started successfully.\n");
  285.     }
  286.     else
  287.     {
  288.         printf("Service not started. \n");
  289.         printf("  Current State: %d\n", (unsigned int)ssStatus.dwCurrentState);
  290.         printf("  Exit Code: %d\n", (unsigned int)ssStatus.dwWin32ExitCode);
  291.         printf("  Check Point: %d\n", (unsigned int)ssStatus.dwCheckPoint);
  292.         printf("  Wait Hint: %d\n", (unsigned int)ssStatus.dwWaitHint);
  293.     }
  294.  
  295.     CloseServiceHandle(schService);
  296.     CloseServiceHandle(schSCManager);
  297. }
  298.  
  299. VOID __stdcall DoStopSvc()
  300. {
  301.     SERVICE_STATUS_PROCESS ssp;
  302.     DWORD dwStartTime = GetTickCount();
  303.     DWORD dwBytesNeeded;
  304.     DWORD dwTimeout = 30000; // 30-second time-out
  305.     DWORD dwWaitTime;
  306.  
  307.     // Get a handle to the SCM database.
  308.  
  309.     schSCManager = OpenSCManager(
  310.                        NULL,                    // local computer
  311.                        NULL,                    // ServicesActive database
  312.                        SC_MANAGER_ALL_ACCESS);  // full access rights
  313.  
  314.     if (NULL == schSCManager)
  315.     {
  316.         printf("OpenSCManager failed (%d)\n", (unsigned int)GetLastError());
  317.         return;
  318.     }
  319.  
  320.     // Get a handle to the service.
  321.  
  322.     schService = OpenService(
  323.                      schSCManager,         // SCM database
  324.                      szSvcName,            // name of service
  325.                      SERVICE_STOP |
  326.                      SERVICE_QUERY_STATUS |
  327.                      SERVICE_ENUMERATE_DEPENDENTS);
  328.  
  329.     if (schService == NULL)
  330.     {
  331.         printf("OpenService failed (%d)\n", (unsigned int)GetLastError());
  332.         CloseServiceHandle(schSCManager);
  333.         return;
  334.     }
  335.  
  336.     // Make sure the service is not already stopped.
  337.  
  338.     if ( !QueryServiceStatusEx(
  339.                 schService,
  340.                 SC_STATUS_PROCESS_INFO,
  341.                 (LPBYTE)&ssp,
  342.                 sizeof(SERVICE_STATUS_PROCESS),
  343.                 &dwBytesNeeded ) )
  344.     {
  345.         printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
  346.         goto stop_cleanup;
  347.     }
  348.  
  349.     if ( ssp.dwCurrentState == SERVICE_STOPPED )
  350.     {
  351.         printf("Service is already stopped.\n");
  352.         goto stop_cleanup;
  353.     }
  354.  
  355.     // If a stop is pending, wait for it.
  356.  
  357.     while ( ssp.dwCurrentState == SERVICE_STOP_PENDING )
  358.     {
  359.         printf("Service stop pending...\n");
  360.  
  361.         // Do not wait longer than the wait hint. A good interval is
  362.         // one-tenth of the wait hint but not less than 1 second
  363.         // and not more than 10 seconds.
  364.  
  365.         dwWaitTime = ssp.dwWaitHint / 10;
  366.  
  367.         if( dwWaitTime < 1000 )
  368.             dwWaitTime = 1000;
  369.         else if ( dwWaitTime > 10000 )
  370.             dwWaitTime = 10000;
  371.  
  372.         Sleep( dwWaitTime );
  373.  
  374.         if ( !QueryServiceStatusEx(
  375.                     schService,
  376.                     SC_STATUS_PROCESS_INFO,
  377.                     (LPBYTE)&ssp,
  378.                     sizeof(SERVICE_STATUS_PROCESS),
  379.                     &dwBytesNeeded ) )
  380.         {
  381.             printf("QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError());
  382.             goto stop_cleanup;
  383.         }
  384.  
  385.         if ( ssp.dwCurrentState == SERVICE_STOPPED )
  386.         {
  387.             printf("Service stopped successfully.\n");
  388.             goto stop_cleanup;
  389.         }
  390.  
  391.         if ( GetTickCount() - dwStartTime > dwTimeout )
  392.         {
  393.             printf("Service stop timed out.\n");
  394.             goto stop_cleanup;
  395.         }
  396.     }
  397.  
  398.     // If the service is running, dependencies must be stopped first.
  399.  
  400. //    StopDependentServices();
  401.  
  402.     // Send a stop code to the service.
  403.  
  404.     if ( !ControlService(
  405.                 schService,
  406.                 SERVICE_CONTROL_STOP,
  407.                 (LPSERVICE_STATUS) &ssp ) )
  408.     {
  409.         printf( "ControlService failed (%d)\n", (unsigned int)GetLastError() );
  410.         goto stop_cleanup;
  411.     }
  412.  
  413.     // Wait for the service to stop.
  414.  
  415.     while ( ssp.dwCurrentState != SERVICE_STOPPED )
  416.     {
  417.         Sleep( ssp.dwWaitHint );
  418.         if ( !QueryServiceStatusEx(
  419.                     schService,
  420.                     SC_STATUS_PROCESS_INFO,
  421.                     (LPBYTE)&ssp,
  422.                     sizeof(SERVICE_STATUS_PROCESS),
  423.                     &dwBytesNeeded ) )
  424.         {
  425.             printf( "QueryServiceStatusEx failed (%d)\n", (unsigned int)GetLastError() );
  426.             goto stop_cleanup;
  427.         }
  428.  
  429.         if ( ssp.dwCurrentState == SERVICE_STOPPED )
  430.             break;
  431.  
  432.         if ( GetTickCount() - dwStartTime > dwTimeout )
  433.         {
  434.             printf( "Wait timed out\n" );
  435.             goto stop_cleanup;
  436.         }
  437.     }
  438.     printf("Service stopped successfully\n");
  439.  
  440. stop_cleanup:
  441.     CloseServiceHandle(schService);
  442.     CloseServiceHandle(schSCManager);
  443. }
  444.  
  445. void __stdcall DoDeleteSvc()
  446. {
  447.     SC_HANDLE schSCManager;
  448.     SC_HANDLE schService;
  449.     //SERVICE_STATUS ssStatus;
  450.  
  451.     // Get a handle to the SCM database.
  452.  
  453.     schSCManager = OpenSCManager(
  454.                        NULL,                    // local computer
  455.                        NULL,                    // ServicesActive database
  456.                        SC_MANAGER_ALL_ACCESS);  // full access rights
  457.  
  458.     if (NULL == schSCManager)
  459.     {
  460.         printf("OpenSCManager failed (%d)\n", (unsigned int)GetLastError());
  461.         return;
  462.     }
  463.  
  464.     // Get a handle to the service.
  465.  
  466.     schService = OpenService(
  467.                      schSCManager,       // SCM database
  468.                      szSvcName,          // name of service
  469.                      DELETE);            // need delete access
  470.  
  471.     if (schService == NULL)
  472.     {
  473.         printf("OpenService failed (%d)\n", (unsigned int)GetLastError());
  474.         CloseServiceHandle(schSCManager);
  475.         return;
  476.     }
  477.  
  478.     // Delete the service.
  479.  
  480.     if (! DeleteService(schService) )
  481.     {
  482.         printf("DeleteService failed (%d)\n", (unsigned int)GetLastError());
  483.     }
  484.     else printf("Service deleted successfully\n");
  485.  
  486.     CloseServiceHandle(schService);
  487.     CloseServiceHandle(schSCManager);
  488. }
  489.  
  490. void LoadService()
  491. {
  492.     DoCreateSvc();
  493.     DoStartSvc();
  494. }
  495.  
  496. void UnloadService()
  497. {
  498.     DoStopSvc();
  499.     DoDeleteSvc();
  500. }
  501.  
  502. int main(int argc, char *argv[])
  503. {
  504.     if(argc != 3)
  505.     {
  506.         PrintUsage(argv[0]);
  507.         return 1;
  508.     }
  509.  
  510.     szSvcName = argv[2];
  511.  
  512.     if(!strcmp(argv[1], "load"))
  513.     {
  514.         LoadService();
  515.     }
  516.     else if (!strcmp(argv[1], "unload"))
  517.     {
  518.         UnloadService();
  519.     }
  520.     else
  521.     {
  522.         PrintUsage(argv[0]);
  523.         return 2;
  524.     }
  525.  
  526.  
  527.     return 0;
  528. }
  529.  
  530. void PrintUsage(char *program)
  531. {
  532.     printf("Usage: %s < load | unload > < driver path >\n", program);
  533. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement