home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / s / servic.zip / INSTSRV.C < prev    next >
C/C++ Source or Header  |  1993-02-08  |  3KB  |  97 lines

  1. ///////////////////////////////////////////////////////
  2. //
  3. //  InstSrv.c --
  4. //
  5. //      This program demonstrates the use of the OpenSCManager and
  6. //      CreateService APIs to install the Simple service sample.
  7. //
  8. //  Copyright 1993, Microsoft Corp.  All Rights Reserved
  9. //
  10. //  history:
  11. //
  12. //      who         when            what
  13. //      ---         ----            ----
  14. //      davidbro    2/3/93          creation
  15. //
  16. #include <windows.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20.  
  21. SC_HANDLE   schService;
  22. SC_HANDLE   schSCManager;
  23.  
  24. VOID
  25. InstallService(LPCTSTR serviceName, LPCTSTR serviceExe)
  26. {
  27.     LPCTSTR lpszBinaryPathName = serviceExe;
  28.  
  29.     schService = CreateService(
  30.         schSCManager,               // SCManager database
  31.         serviceName,                // name of service
  32. //      serviceName,                // name to display (new parameter after october beta)
  33.         SERVICE_ALL_ACCESS,         // desired access
  34.         SERVICE_WIN32_OWN_PROCESS,  // service type
  35.         SERVICE_DEMAND_START,       // start type
  36.         SERVICE_ERROR_NORMAL,       // error control type
  37.         lpszBinaryPathName,         // service's binary
  38.         NULL,                       // no load ordering group
  39.         NULL,                       // no tag identifier
  40.         NULL,                       // no dependencies
  41.         NULL,                       // LocalSystem account
  42.         NULL);                      // no password
  43.  
  44.     if (schService == NULL) {
  45.         printf("failure: CreateService (0x%02x)\n", GetLastError());
  46.         return;
  47.     } else
  48.         printf("CreateService SUCCESS\n");
  49.  
  50.     CloseServiceHandle(schService);
  51. }
  52.  
  53. VOID
  54. RemoveService(LPCTSTR serviceName)
  55. {
  56.     BOOL    ret;
  57.  
  58.     schService = OpenService(schSCManager, serviceName, SERVICE_ALL_ACCESS);
  59.  
  60.     if (schService == NULL) {
  61.         printf("failure: OpenService (0x%02x)\n", GetLastError());
  62.         return;
  63.     }
  64.  
  65.     ret = DeleteService(schService);
  66.  
  67.     if (ret)
  68.         printf("DeleteService SUCCESS\n");
  69.     else
  70.         printf("failure: DeleteService (0x%02x)\n", GetLastError());
  71. }
  72.  
  73. VOID
  74. main(int argc, char *argv[])
  75. {
  76.     if (argc != 3) {
  77.         printf("usage: instsrv <service name> <exe location>\n");
  78.         printf("           to install a service, or:\n");
  79.         printf("       instsrv <service name> remove\n");
  80.         printf("           to remove a service\n");
  81.         exit(1);
  82.     }
  83.  
  84.     schSCManager = OpenSCManager(
  85.                         NULL,                   // machine (NULL == local)
  86.                         NULL,                   // database (NULL == default)
  87.                         SC_MANAGER_ALL_ACCESS   // access required
  88.                         );
  89.  
  90.     if (!stricmp(argv[2], "remove"))
  91.         RemoveService(argv[1]);
  92.     else
  93.         InstallService(argv[1], argv[2]);
  94.  
  95.     CloseServiceHandle(schSCManager);
  96. }
  97.