ServiceMgr.cpp
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:8k
源码类别:

Ftp客户端

开发平台:

Visual C++

  1. // This is part of the WAR SOFTWARE SERIES initiated by Jarle Aase
  2. // Copyright 1996 by Jarle Aase. All rights reserved.
  3. // See the "War Software Series Licende Agreement" for details concerning 
  4. // use and distribution.
  5. // ---
  6. // This source code, executables and programs containing source code or
  7. // binaries or proprietetary technology from the War Software Series are
  8. // NOT alloed used, viewed or tested by any governmental agencies in
  9. // any countries. This includes the government, departments, police, 
  10. // military etc.
  11. // ---
  12. // This file is intended for use with Tab space = 2
  13. // Created and maintained in MSVC Developer Studio
  14. // ---
  15. // NAME : ServiceMgr.cpp
  16. // PURPOSE : NT Service Manager class
  17. // PROGRAM : 
  18. // DATE : Dec. 22 1996
  19. // AUTHOR : Jarle Aase
  20. // ---
  21. // REVISION HISTORY
  22. // 
  23. #include "stdafx.h"
  24. #include "WarSoftware.h" // Common headerfile for all daemons
  25. #include "winsvc.h"
  26. #include "ServiceMgr.h"
  27. #ifdef _DEBUG
  28. #define new DEBUG_NEW
  29. #undef THIS_FILE
  30. static char THIS_FILE[] = __FILE__;
  31. #endif
  32. // Test if the service is currently installed
  33. BOOL CNTServiceMgr::IsInstalled(LPCSTR ServiceName)
  34. {
  35.     BOOL bResult = FALSE;
  36. if (!IsNT())
  37. return FALSE; // Not NT
  38.     // Open the Service Control Manager
  39.     SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
  40.                                      NULL, // ServicesActive database
  41.                                      SC_MANAGER_ALL_ACCESS); // full access
  42.     if (hSCM) {
  43.         // Try to open the service
  44.         SC_HANDLE hService = ::OpenService(hSCM,
  45.                                            ServiceName,
  46.                                            SERVICE_QUERY_CONFIG);
  47.         if (hService) {
  48.             bResult = TRUE;
  49.             ::CloseServiceHandle(hService);
  50.         }
  51.         ::CloseServiceHandle(hSCM);
  52.     }
  53.     
  54.     return bResult;
  55. }
  56. BOOL CNTServiceMgr::Install(DWORD dwStartType, 
  57. LPCSTR ExeName,
  58. LPCSTR Name, LPCSTR VisualName,
  59. LPCSTR LoginName, LPCSTR Password)
  60. {
  61.     // Open the Service Control Manager
  62.     SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
  63.                                      NULL, // ServicesActive database
  64.                                      SC_MANAGER_ALL_ACCESS); // full access
  65.     if (!hSCM) return FALSE;
  66.     // Get the executable file path
  67.     CString FilePath;
  68. FilePath.Format("%s\%s", GetStartupPath(), ExeName);
  69.     // Create the service
  70.     SC_HANDLE hService = ::CreateService(hSCM,
  71.                                          Name,
  72.                                          VisualName,
  73.                                          SERVICE_ALL_ACCESS,
  74.                                          SERVICE_WIN32_OWN_PROCESS,
  75.                                          dwStartType,        // start condition
  76.                                          SERVICE_ERROR_NORMAL,
  77.                                          FilePath,
  78.                                          NULL,
  79.                                          NULL,
  80.                                          "LanmanWorkstationTcpipAfd",
  81.                                          (LoginName && *LoginName) ? LoginName : NULL,
  82.                                          (Password && *Password) ? Password : NULL);
  83.     if (!hService) {
  84.         ::CloseServiceHandle(hSCM);
  85.         return FALSE;
  86.     }
  87.     // make registry entries to support logging messages
  88.     // Add the source name as a subkey under the Application
  89.     // key in the EventLog service portion of the registry.
  90.     char szKey[256];
  91.     HKEY hKey = NULL;
  92.     strcpy(szKey, "SYSTEM\CurrentControlSet\Services\EventLog\Application\");
  93.     strcat(szKey, Name);
  94.     if (::RegCreateKey(HKEY_LOCAL_MACHINE, szKey, &hKey) != ERROR_SUCCESS) {
  95.         ::CloseServiceHandle(hService);
  96.         ::CloseServiceHandle(hSCM);
  97.         return FALSE;
  98.     }
  99.     // Add the Event ID message-file name to the 'EventMessageFile' subkey.
  100. CString EvPath;
  101. EvPath.Format("%s\WarDaemonLib.dll", GetStartupPath());
  102.     ::RegSetValueEx(hKey,
  103.                     "EventMessageFile",
  104.                     0,
  105.                     REG_EXPAND_SZ, 
  106.                     (CONST BYTE*)(LPCSTR)EvPath,
  107.                     strlen(EvPath) + 1);     
  108.     // Set the supported types flags.
  109.     DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
  110.     ::RegSetValueEx(hKey,
  111.                     "TypesSupported",
  112.                     0,
  113.                     REG_DWORD,
  114.                     (CONST BYTE*)&dwData,
  115.                      sizeof(DWORD));
  116.     ::RegCloseKey(hKey);
  117.     // tidy up
  118.     ::CloseServiceHandle(hService);
  119.     ::CloseServiceHandle(hSCM);
  120. strcpy(szKey, "SYSTEM\CurrentControlSet\Services\");
  121. strcat(szKey, Name);
  122. strcat(szKey, "\Parameters");
  123. DWORD dwDisp;
  124. if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,szKey,0,"",REG_OPTION_NON_VOLATILE,
  125. KEY_WRITE,NULL,&hKey,&dwDisp) == ERROR_SUCCESS)
  126. {
  127. //Set current directory
  128. ::RegSetValueEx(hKey,
  129.                     "Path",
  130.                     0,
  131.                     REG_EXPAND_SZ, 
  132.                     (CONST BYTE*)(LPCSTR)GetStartupPath(),
  133.                     strlen((LPCSTR)GetStartupPath()) + 1);     
  134. ::RegCloseKey(hKey);
  135. }
  136.     return TRUE;
  137. }
  138. BOOL CNTServiceMgr::Uninstall(LPCSTR Name)
  139. {
  140.     // Open the Service Control Manager
  141.     SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
  142.                                      NULL, // ServicesActive database
  143.                                      SC_MANAGER_ALL_ACCESS); // full access
  144.     if (!hSCM) return FALSE;
  145.     BOOL bResult = FALSE;
  146.     SC_HANDLE hService = ::OpenService(hSCM, Name, DELETE);
  147.     if (hService) {
  148.         if (::DeleteService(hService)) {
  149.             bResult = TRUE;
  150.         } else {
  151.             ;
  152.         }
  153.         ::CloseServiceHandle(hService);
  154.     }
  155.     
  156.     ::CloseServiceHandle(hSCM);
  157.     return bResult;
  158. }
  159. BOOL CNTServiceMgr::MyStartService(LPCSTR Name)
  160. {
  161. SERVICE_STATUS sStatus;
  162. // Open the Service Control Manager
  163.   SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
  164.                                    NULL, // ServicesActive database
  165.                                      SC_MANAGER_ALL_ACCESS); // full access
  166.   if (!hSCM) return FALSE;
  167.   BOOL bResult = FALSE;
  168.   SC_HANDLE hService = ::OpenService(hSCM, Name, SERVICE_ALL_ACCESS);
  169.   if (hService)
  170. {
  171. if (::StartService(hService,0,NULL)) 
  172. {
  173. if (QueryServiceStatus(hService, &sStatus))
  174. {
  175. DWORD cp;
  176. while(sStatus.dwCurrentState != SERVICE_RUNNING)
  177. {
  178. cp = sStatus.dwCheckPoint;
  179. Sleep(sStatus.dwWaitHint);
  180. if (!::QueryServiceStatus(hService, &sStatus))
  181. break;
  182. if (cp >= sStatus.dwCheckPoint)
  183. break;
  184. }
  185. bResult = (sStatus.dwCurrentState == SERVICE_RUNNING);
  186. }
  187. else 
  188. {
  189. ;
  190. }
  191. ::CloseServiceHandle(hService);
  192. }
  193. ::CloseServiceHandle(hSCM);
  194. return bResult;
  195. }
  196. BOOL CNTServiceMgr::ControlService(LPCSTR Name, DWORD Command, DWORD *pStatus)
  197. {
  198. SERVICE_STATUS sStatus; 
  199. DWORD fdwAccess;
  200. /* The required service object access depends on the control. */ 
  201. switch (Command)
  202. case SERVICE_CONTROL_STOP: 
  203. fdwAccess = SERVICE_STOP; 
  204. break; 
  205. case SERVICE_CONTROL_PAUSE: 
  206. case SERVICE_CONTROL_CONTINUE: 
  207. fdwAccess = SERVICE_PAUSE_CONTINUE; 
  208. break; 
  209. default: 
  210. Command = SERVICE_CONTROL_INTERROGATE;
  211. fdwAccess = SERVICE_INTERROGATE; 
  212. break;
  213.   } 
  214. SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
  215.                                    NULL, // ServicesActive database
  216.  SC_MANAGER_CONNECT);
  217. if (!hSCM) 
  218. return FALSE;
  219. BOOL bResult = FALSE;
  220.   SC_HANDLE hService = ::OpenService(hSCM, Name, fdwAccess);
  221.   if (hService)
  222. {
  223. if (::ControlService(hService,Command,&sStatus)) 
  224. {
  225. bResult = TRUE;
  226. if (pStatus)
  227. *pStatus = sStatus.dwCurrentState;
  228. else 
  229. {
  230. ;
  231. }
  232. ::CloseServiceHandle(hService);
  233. }
  234. ::CloseServiceHandle(hSCM);
  235. return bResult;
  236. }