home *** CD-ROM | disk | FTP | other *** search
- /*
- * Author : Ranveer Chandra
- * Directory: VirtualWiFi_Root\install
- * File Name: ServiceInstaller.cpp
- * Purpose : Defines the entry point for the VirtualWiFi Service.
- */
-
- #include <stdio.h>
- #include <Windows.h>
- #include "precomp.h"
-
- extern bool Verbose;
-
- void PrintError(DWORD code)
- {
- LPTSTR serr;
-
- if(::FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM,
- NULL,
- code,
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
- (LPTSTR)&serr,
- 0,
- NULL) == 0)
- {
- printf("Unknown Error\n");
- }
- else
- {
- printf("Error : %s\n", serr);
- LocalFree(serr);
- }
- }
-
-
- void ImpersonateUser()
- {
- //prepare to access remote system
-
- DWORD id = GetCurrentProcessId();
- HANDLE hp = OpenProcess(PROCESS_ALL_ACCESS, FALSE, id);
-
- HANDLE t;
-
- BOOL b = OpenProcessToken( hp,
- TOKEN_QUERY | TOKEN_DUPLICATE ,
- &t);
-
- if(!ImpersonateLoggedOnUser(t))
- {
- PrintError(GetLastError());
- return;
- }
- }
-
- void PrintStatus(SERVICE_STATUS sc)
- {
- switch(sc.dwCurrentState)
- {
- case SERVICE_STOPPED : printf("The service is not running.\n");
- break;
- case SERVICE_START_PENDING : printf("The service is starting.\n");
- break;
- case SERVICE_STOP_PENDING: printf("The service is stopping. \n");
- break;
- case SERVICE_RUNNING: printf("The service is running. \n");
- break;
- case SERVICE_CONTINUE_PENDING: printf("The service continue is pending.\n");
- break;
- case SERVICE_PAUSE_PENDING: printf("The service pause is pending. \n");
- break;
- case SERVICE_PAUSED: printf("The service is paused.\n");
- break;
- default: printf("Status Unknown\n");
- break;
- }
- }
-
- void QuerySvc(SC_HANDLE hSCM, char *szSvcName)
- {
- //get service handle
- SC_HANDLE hService = ::OpenService(hSCM, szSvcName, SERVICE_QUERY_STATUS);
-
- if (hService == NULL)
- {
- printf("ERROR: COULDN'T OPEN SERVICE\n");
- return;
- }
-
- SERVICE_STATUS status;
-
- //query status
- if(!QueryServiceStatus(hService,&status))
- {
- ::CloseServiceHandle(hSCM);
- PrintError(GetLastError());
- }
-
- PrintStatus(status);
-
- ::CloseServiceHandle(hService);
- }
-
- int Install(SC_HANDLE hSCM, char *szSvcName, char *szFilePath1)
- {
- // Service installation
- char currentDirectory[100];
- char szFilePath[200];
- //if ( GetModuleFileName( NULL, szFilePath, 512 ) == 0 )
- if ( GetCurrentDirectory( 512, currentDirectory ) == 0 )
- {
- PrintError(GetLastError());
- return 0;
- }
-
- sprintf(szFilePath, "%s\\%s", currentDirectory, SZAPPNAME);
- if(Verbose)
- printf("szFilePath %s\n", szFilePath);
-
- SC_HANDLE hService = ::CreateService(
- hSCM, // SCManager database
- SZSERVICENAME, // name of service
- SZSERVICEDISPLAYNAME, // name to display
- SERVICE_ALL_ACCESS, // desired access
- SERVICE_WIN32_OWN_PROCESS, // service type
- SERVICE_AUTO_START, // start type
- //SERVICE_DEMAND_START, // start type
- SERVICE_ERROR_IGNORE, // error control type
- szFilePath, // service's binary
- //SZFILEPATH,
- NULL, // no load ordering group
- NULL, // no tag identifier
- SZDEPENDENCIES, // dependencies
- NULL, // LocalSystem account
- NULL); // no password
-
- if (hService == NULL)
- {
- printf("ERROR: COULDN'T CREATE SERVICE\n");
- PrintError(GetLastError());
- return 0;
- }
-
- ::CloseServiceHandle(hService);
- return 1;
- }
-
-
- int Uninstall(SC_HANDLE hSCM, char *szSvcName)
- {
- //get a handle to the service
- SC_HANDLE hService = ::OpenService(hSCM, szSvcName, SERVICE_STOP | DELETE);
-
- if (hService == NULL)
- {
- printf("ERROR: COULDN'T OPEN SERVICE\n");
- return 0;
- }
-
- // let us first stop the service
- SERVICE_STATUS status;
- ::ControlService(hService, SERVICE_CONTROL_STOP, &status);
-
- //Print the status of the service
- PrintStatus(status);
-
- // now uninstall the service
- BOOL bDelete = ::DeleteService(hService);
- ::CloseServiceHandle(hService);
-
- if (!bDelete) {
- printf("WARNING: SERVICE COULD NOT BE DELETED\n");
- return 0;
- }
- return 1;
- }
-
-
- int StartSvc(SC_HANDLE hSCM, char *szSvcName)
- {
- SC_HANDLE hService = ::OpenService(hSCM, szSvcName, SERVICE_START);
-
- if (hService == NULL)
- {
- printf("ERROR: COULDN'T ACCESS SERVICE\n");
- return 0;
- }
-
- //no arguments
- if(::StartService(hService, 0, NULL)==0)
- {
- PrintError(GetLastError());
- return 0;
- }
-
- ::CloseServiceHandle(hService);
-
- QuerySvc(hSCM, szSvcName);
- return 1;
- }
-
-
- int StopSvc(SC_HANDLE hSCM, char *szSvcName)
- {
- SC_HANDLE hService = ::OpenService(hSCM, szSvcName, SERVICE_STOP);
-
- if (hService == NULL)
- {
- printf("ERROR: COULDN'T OPEN SERVICE\n");
- return 0;
- }
-
- SERVICE_STATUS status;
- if(!::ControlService(hService, SERVICE_CONTROL_STOP, &status) && Verbose)
- {
- printf("ERROR: COULDN'T STOP SERVICE\n");
- return 0;
- }
-
-
- ::CloseServiceHandle(hService);
-
- QuerySvc(hSCM, szSvcName);
- return 1;
- }
-
-
-
- extern
- int ServiceInstaller(char *action, char *pSvcPath)
- {
- char *pSysName = NULL;
- int retVal = 1;
-
- /*
- if(argc == 4)
- {
- pSysName = new char[50]; //if remote system
- strcpy(pSysName, argv[3]);
-
- ImpersonateUser();
- }
- */
-
- //access service control manager
- SC_HANDLE hSCM = ::OpenSCManager(pSysName, SERVICES_ACTIVE_DATABASE, SC_MANAGER_ALL_ACCESS);
-
- if (hSCM == 0)
- {
- printf("ERROR: UNABLE TO OPEN SERVICE MANAGER\n");
- PrintError(GetLastError());
- return 0;
- }
-
-
- if(!strcmp(action,"install"))
- {
- //pSvcPath = strcat(pSvcPath, "\\simple.exe");
- if(Verbose)
- printf("The path of the service is %s\n", pSvcPath);
- retVal = Install(hSCM, "VirtualWiFiService", pSvcPath);
- //delete[] pSvcPath;
- }
- else if(!strcmp(action,"uninstall"))
- {
- retVal = Uninstall(hSCM, "VirtualWiFiService");
- }
- else if(!strcmp(action,"start"))
- {
- retVal = StartSvc(hSCM, "VirtualWiFiService");
- }
- else if(!strcmp(action,"stop"))
- {
- retVal = StopSvc(hSCM, "VirtualWiFiService");
- }
- else if(!strcmp(action,"status"))
- {
- QuerySvc(hSCM, "VirtualWiFiService");
- }
- else
- {
- printf("WARNING: Bad command\n");
- }
-
- ::CloseServiceHandle(hSCM);
-
- delete[] pSysName;
-
- return retVal;
- }
-