home *** CD-ROM | disk | FTP | other *** search
/ PCNET 2006 September - Disc 1 / PCNET_CD_2006_09.iso / surpriz / MSRMesh-VirtualWIFI.MSI / ServiceInstaller.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2005-06-24  |  6.9 KB  |  292 lines

  1. /*
  2.  * Author   : Ranveer Chandra
  3.  * Directory: VirtualWiFi_Root\install
  4.  * File Name: ServiceInstaller.cpp  
  5.  * Purpose  : Defines the entry point for the VirtualWiFi Service.
  6.  */
  7.  
  8. #include <stdio.h>
  9. #include <Windows.h>
  10. #include "precomp.h"
  11.  
  12. extern bool Verbose;
  13.  
  14. void PrintError(DWORD code)
  15. {
  16.      LPTSTR serr;
  17.  
  18.      if(::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
  19.             FORMAT_MESSAGE_FROM_SYSTEM,
  20.             NULL,
  21.             code,
  22.             MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
  23.             (LPTSTR)&serr,
  24.             0,
  25.             NULL) == 0)
  26.     {
  27.         printf("Unknown Error\n");
  28.     }
  29.     else
  30.     {
  31.         printf("Error : %s\n", serr);
  32.         LocalFree(serr);
  33.     }
  34. }
  35.  
  36.  
  37. void ImpersonateUser()
  38. {
  39.   //prepare to access remote system
  40.  
  41.   DWORD id = GetCurrentProcessId();
  42.   HANDLE hp = OpenProcess(PROCESS_ALL_ACCESS, FALSE, id);
  43.  
  44.   HANDLE t;
  45.  
  46.   BOOL b = OpenProcessToken( hp,
  47.                              TOKEN_QUERY | TOKEN_DUPLICATE ,
  48.                              &t);
  49.  
  50.   if(!ImpersonateLoggedOnUser(t))
  51.   {
  52.     PrintError(GetLastError());
  53.     return;
  54.   }
  55. }
  56.  
  57. void PrintStatus(SERVICE_STATUS sc)
  58. {
  59.     switch(sc.dwCurrentState)
  60.     {
  61.         case SERVICE_STOPPED :            printf("The service is not running.\n");
  62.             break;
  63.         case SERVICE_START_PENDING :    printf("The service is starting.\n");
  64.             break;
  65.         case SERVICE_STOP_PENDING:        printf("The service is stopping. \n");
  66.             break;
  67.         case SERVICE_RUNNING:            printf("The service is running. \n");
  68.             break;
  69.         case SERVICE_CONTINUE_PENDING:    printf("The service continue is pending.\n");
  70.             break;
  71.         case SERVICE_PAUSE_PENDING:        printf("The service pause is pending. \n");
  72.             break;
  73.         case SERVICE_PAUSED:            printf("The service is paused.\n");
  74.             break;
  75.         default:                        printf("Status Unknown\n");
  76.             break;
  77.     }
  78. }
  79.  
  80. void QuerySvc(SC_HANDLE hSCM, char *szSvcName)
  81. {
  82.     //get service handle
  83.     SC_HANDLE hService = ::OpenService(hSCM, szSvcName, SERVICE_QUERY_STATUS);
  84.  
  85.     if (hService == NULL)
  86.     {
  87.         printf("ERROR: COULDN'T OPEN SERVICE\n");
  88.         return;
  89.     }
  90.  
  91.     SERVICE_STATUS status;
  92.  
  93.     //query status
  94.     if(!QueryServiceStatus(hService,&status))
  95.     {
  96.         ::CloseServiceHandle(hSCM);
  97.         PrintError(GetLastError());
  98.     }
  99.  
  100.     PrintStatus(status);
  101.  
  102.     ::CloseServiceHandle(hService);
  103. }
  104.  
  105. int Install(SC_HANDLE hSCM, char *szSvcName, char *szFilePath1)
  106. {
  107.     // Service installation
  108.     char currentDirectory[100];
  109.     char szFilePath[200];
  110.   //if ( GetModuleFileName( NULL, szFilePath, 512 ) == 0 )
  111.   if ( GetCurrentDirectory( 512, currentDirectory ) == 0 )
  112.    {
  113.       PrintError(GetLastError());
  114.       return 0;
  115.    }
  116.  
  117.    sprintf(szFilePath, "%s\\%s", currentDirectory, SZAPPNAME);
  118.    if(Verbose)
  119.         printf("szFilePath %s\n", szFilePath);
  120.  
  121.    SC_HANDLE hService = ::CreateService(
  122.                                 hSCM,               // SCManager database
  123.                                 SZSERVICENAME,        // name of service
  124.                                 SZSERVICEDISPLAYNAME, // name to display
  125.                                 SERVICE_ALL_ACCESS,         // desired access
  126.                                 SERVICE_WIN32_OWN_PROCESS,  // service type
  127.                                 SERVICE_AUTO_START,       // start type
  128.                                 //SERVICE_DEMAND_START,       // start type
  129.                                 SERVICE_ERROR_IGNORE,       // error control type
  130.                                 szFilePath,                     // service's binary
  131.                                 //SZFILEPATH,
  132.                                 NULL,                       // no load ordering group
  133.                                 NULL,                       // no tag identifier
  134.                                 SZDEPENDENCIES,       // dependencies
  135.                                 NULL,                       // LocalSystem account
  136.                                 NULL);                      // no password
  137.  
  138.      if (hService == NULL)
  139.     {
  140.         printf("ERROR: COULDN'T CREATE SERVICE\n");
  141.         PrintError(GetLastError());
  142.         return 0;
  143.     }
  144.  
  145.     ::CloseServiceHandle(hService);
  146.     return 1;
  147. }
  148.  
  149.  
  150. int Uninstall(SC_HANDLE hSCM, char *szSvcName)
  151. {
  152.     //get a handle to the service
  153.     SC_HANDLE hService = ::OpenService(hSCM, szSvcName, SERVICE_STOP | DELETE);
  154.  
  155.     if (hService == NULL)
  156.     {
  157.         printf("ERROR: COULDN'T OPEN SERVICE\n");
  158.         return 0;
  159.     }
  160.  
  161.     // let us first stop the service
  162.     SERVICE_STATUS status;
  163.     ::ControlService(hService, SERVICE_CONTROL_STOP, &status);
  164.  
  165.     //Print the status of the service
  166.     PrintStatus(status);
  167.  
  168.    // now uninstall the service
  169.     BOOL bDelete = ::DeleteService(hService);
  170.     ::CloseServiceHandle(hService);
  171.  
  172.     if (!bDelete) {
  173.         printf("WARNING: SERVICE COULD NOT BE DELETED\n");
  174.     return 0;
  175.     }
  176.     return 1;
  177. }
  178.  
  179.  
  180. int StartSvc(SC_HANDLE hSCM, char *szSvcName)
  181. {
  182.     SC_HANDLE hService = ::OpenService(hSCM, szSvcName, SERVICE_START);
  183.  
  184.     if (hService == NULL)
  185.     {
  186.         printf("ERROR: COULDN'T ACCESS SERVICE\n");
  187.         return 0;
  188.     }
  189.  
  190.     //no arguments
  191.     if(::StartService(hService, 0, NULL)==0)
  192.     {
  193.         PrintError(GetLastError());
  194.         return 0;
  195.     }
  196.  
  197.     ::CloseServiceHandle(hService);
  198.  
  199.      QuerySvc(hSCM, szSvcName);
  200.      return 1;
  201. }
  202.  
  203.  
  204. int StopSvc(SC_HANDLE hSCM, char *szSvcName)
  205. {
  206.     SC_HANDLE hService = ::OpenService(hSCM, szSvcName, SERVICE_STOP);
  207.  
  208.     if (hService == NULL)
  209.     {
  210.         printf("ERROR: COULDN'T OPEN SERVICE\n");
  211.         return 0;
  212.     }
  213.  
  214.     SERVICE_STATUS status;
  215.     if(!::ControlService(hService, SERVICE_CONTROL_STOP, &status) && Verbose)
  216.     {
  217.         printf("ERROR: COULDN'T STOP SERVICE\n");
  218.         return 0;
  219.     }
  220.  
  221.  
  222.     ::CloseServiceHandle(hService);
  223.  
  224.     QuerySvc(hSCM, szSvcName);
  225.     return 1;
  226. }
  227.  
  228.  
  229.  
  230. extern 
  231. int ServiceInstaller(char *action, char *pSvcPath)
  232. {
  233.     char *pSysName = NULL;
  234.     int retVal = 1;
  235.  
  236.     /*
  237.     if(argc == 4)
  238.     {
  239.         pSysName = new char[50]; //if remote system
  240.         strcpy(pSysName, argv[3]);
  241.  
  242.         ImpersonateUser();
  243.     }
  244.     */
  245.  
  246.     //access service control manager
  247.     SC_HANDLE hSCM = ::OpenSCManager(pSysName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
  248.  
  249.     if (hSCM == 0)
  250.     {
  251.             printf("ERROR: UNABLE TO OPEN SERVICE MANAGER\n");
  252.             PrintError(GetLastError());
  253.             return 0;
  254.     }
  255.  
  256.  
  257.     if(!strcmp(action,"install"))
  258.     {
  259.         //pSvcPath = strcat(pSvcPath, "\\simple.exe");
  260.         if(Verbose)
  261.             printf("The path of the service is %s\n", pSvcPath);
  262.         retVal = Install(hSCM, "VirtualWiFiService", pSvcPath);
  263.         //delete[] pSvcPath;
  264.     }
  265.     else if(!strcmp(action,"uninstall"))
  266.     {
  267.         retVal = Uninstall(hSCM, "VirtualWiFiService");
  268.     }
  269.     else if(!strcmp(action,"start"))
  270.     {
  271.         retVal = StartSvc(hSCM, "VirtualWiFiService");
  272.     }
  273.     else if(!strcmp(action,"stop"))
  274.     {
  275.         retVal = StopSvc(hSCM, "VirtualWiFiService");
  276.     }
  277.     else if(!strcmp(action,"status"))
  278.     {
  279.         QuerySvc(hSCM, "VirtualWiFiService");
  280.     }
  281.     else
  282.     {
  283.         printf("WARNING: Bad command\n");
  284.     }
  285.  
  286.     ::CloseServiceHandle(hSCM);
  287.  
  288.     delete[] pSysName;
  289.  
  290.     return retVal;
  291. }
  292.