ServiceMgr.cpp
资源名称:warftpd.zip [点击查看]
上传用户:surprise9
上传日期:2007-01-04
资源大小:426k
文件大小:8k
源码类别:
Ftp客户端
开发平台:
Visual C++
- // This is part of the WAR SOFTWARE SERIES initiated by Jarle Aase
- // Copyright 1996 by Jarle Aase. All rights reserved.
- // See the "War Software Series Licende Agreement" for details concerning
- // use and distribution.
- // ---
- // This source code, executables and programs containing source code or
- // binaries or proprietetary technology from the War Software Series are
- // NOT alloed used, viewed or tested by any governmental agencies in
- // any countries. This includes the government, departments, police,
- // military etc.
- // ---
- // This file is intended for use with Tab space = 2
- // Created and maintained in MSVC Developer Studio
- // ---
- // NAME : ServiceMgr.cpp
- // PURPOSE : NT Service Manager class
- // PROGRAM :
- // DATE : Dec. 22 1996
- // AUTHOR : Jarle Aase
- // ---
- // REVISION HISTORY
- //
- #include "stdafx.h"
- #include "WarSoftware.h" // Common headerfile for all daemons
- #include "winsvc.h"
- #include "ServiceMgr.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- // Test if the service is currently installed
- BOOL CNTServiceMgr::IsInstalled(LPCSTR ServiceName)
- {
- BOOL bResult = FALSE;
- if (!IsNT())
- return FALSE; // Not NT
- // Open the Service Control Manager
- SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
- NULL, // ServicesActive database
- SC_MANAGER_ALL_ACCESS); // full access
- if (hSCM) {
- // Try to open the service
- SC_HANDLE hService = ::OpenService(hSCM,
- ServiceName,
- SERVICE_QUERY_CONFIG);
- if (hService) {
- bResult = TRUE;
- ::CloseServiceHandle(hService);
- }
- ::CloseServiceHandle(hSCM);
- }
- return bResult;
- }
- BOOL CNTServiceMgr::Install(DWORD dwStartType,
- LPCSTR ExeName,
- LPCSTR Name, LPCSTR VisualName,
- LPCSTR LoginName, LPCSTR Password)
- {
- // Open the Service Control Manager
- SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
- NULL, // ServicesActive database
- SC_MANAGER_ALL_ACCESS); // full access
- if (!hSCM) return FALSE;
- // Get the executable file path
- CString FilePath;
- FilePath.Format("%s\%s", GetStartupPath(), ExeName);
- // Create the service
- SC_HANDLE hService = ::CreateService(hSCM,
- Name,
- VisualName,
- SERVICE_ALL_ACCESS,
- SERVICE_WIN32_OWN_PROCESS,
- dwStartType, // start condition
- SERVICE_ERROR_NORMAL,
- FilePath,
- NULL,
- NULL,
- "LanmanWorkstation Tcpip Afd ",
- (LoginName && *LoginName) ? LoginName : NULL,
- (Password && *Password) ? Password : NULL);
- if (!hService) {
- ::CloseServiceHandle(hSCM);
- return FALSE;
- }
- // make registry entries to support logging messages
- // Add the source name as a subkey under the Application
- // key in the EventLog service portion of the registry.
- char szKey[256];
- HKEY hKey = NULL;
- strcpy(szKey, "SYSTEM\CurrentControlSet\Services\EventLog\Application\");
- strcat(szKey, Name);
- if (::RegCreateKey(HKEY_LOCAL_MACHINE, szKey, &hKey) != ERROR_SUCCESS) {
- ::CloseServiceHandle(hService);
- ::CloseServiceHandle(hSCM);
- return FALSE;
- }
- // Add the Event ID message-file name to the 'EventMessageFile' subkey.
- CString EvPath;
- EvPath.Format("%s\WarDaemonLib.dll", GetStartupPath());
- ::RegSetValueEx(hKey,
- "EventMessageFile",
- 0,
- REG_EXPAND_SZ,
- (CONST BYTE*)(LPCSTR)EvPath,
- strlen(EvPath) + 1);
- // Set the supported types flags.
- DWORD dwData = EVENTLOG_ERROR_TYPE | EVENTLOG_WARNING_TYPE | EVENTLOG_INFORMATION_TYPE;
- ::RegSetValueEx(hKey,
- "TypesSupported",
- 0,
- REG_DWORD,
- (CONST BYTE*)&dwData,
- sizeof(DWORD));
- ::RegCloseKey(hKey);
- // tidy up
- ::CloseServiceHandle(hService);
- ::CloseServiceHandle(hSCM);
- strcpy(szKey, "SYSTEM\CurrentControlSet\Services\");
- strcat(szKey, Name);
- strcat(szKey, "\Parameters");
- DWORD dwDisp;
- if (RegCreateKeyEx(HKEY_LOCAL_MACHINE,szKey,0,"",REG_OPTION_NON_VOLATILE,
- KEY_WRITE,NULL,&hKey,&dwDisp) == ERROR_SUCCESS)
- {
- //Set current directory
- ::RegSetValueEx(hKey,
- "Path",
- 0,
- REG_EXPAND_SZ,
- (CONST BYTE*)(LPCSTR)GetStartupPath(),
- strlen((LPCSTR)GetStartupPath()) + 1);
- ::RegCloseKey(hKey);
- }
- return TRUE;
- }
- BOOL CNTServiceMgr::Uninstall(LPCSTR Name)
- {
- // Open the Service Control Manager
- SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
- NULL, // ServicesActive database
- SC_MANAGER_ALL_ACCESS); // full access
- if (!hSCM) return FALSE;
- BOOL bResult = FALSE;
- SC_HANDLE hService = ::OpenService(hSCM, Name, DELETE);
- if (hService) {
- if (::DeleteService(hService)) {
- bResult = TRUE;
- } else {
- ;
- }
- ::CloseServiceHandle(hService);
- }
- ::CloseServiceHandle(hSCM);
- return bResult;
- }
- BOOL CNTServiceMgr::MyStartService(LPCSTR Name)
- {
- SERVICE_STATUS sStatus;
- // Open the Service Control Manager
- SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
- NULL, // ServicesActive database
- SC_MANAGER_ALL_ACCESS); // full access
- if (!hSCM) return FALSE;
- BOOL bResult = FALSE;
- SC_HANDLE hService = ::OpenService(hSCM, Name, SERVICE_ALL_ACCESS);
- if (hService)
- {
- if (::StartService(hService,0,NULL))
- {
- if (QueryServiceStatus(hService, &sStatus))
- {
- DWORD cp;
- while(sStatus.dwCurrentState != SERVICE_RUNNING)
- {
- cp = sStatus.dwCheckPoint;
- Sleep(sStatus.dwWaitHint);
- if (!::QueryServiceStatus(hService, &sStatus))
- break;
- if (cp >= sStatus.dwCheckPoint)
- break;
- }
- bResult = (sStatus.dwCurrentState == SERVICE_RUNNING);
- }
- }
- else
- {
- ;
- }
- ::CloseServiceHandle(hService);
- }
- ::CloseServiceHandle(hSCM);
- return bResult;
- }
- BOOL CNTServiceMgr::ControlService(LPCSTR Name, DWORD Command, DWORD *pStatus)
- {
- SERVICE_STATUS sStatus;
- DWORD fdwAccess;
- /* The required service object access depends on the control. */
- switch (Command)
- {
- case SERVICE_CONTROL_STOP:
- fdwAccess = SERVICE_STOP;
- break;
- case SERVICE_CONTROL_PAUSE:
- case SERVICE_CONTROL_CONTINUE:
- fdwAccess = SERVICE_PAUSE_CONTINUE;
- break;
- default:
- Command = SERVICE_CONTROL_INTERROGATE;
- fdwAccess = SERVICE_INTERROGATE;
- break;
- }
- SC_HANDLE hSCM = ::OpenSCManager(NULL, // local machine
- NULL, // ServicesActive database
- SC_MANAGER_CONNECT);
- if (!hSCM)
- return FALSE;
- BOOL bResult = FALSE;
- SC_HANDLE hService = ::OpenService(hSCM, Name, fdwAccess);
- if (hService)
- {
- if (::ControlService(hService,Command,&sStatus))
- {
- bResult = TRUE;
- if (pStatus)
- *pStatus = sStatus.dwCurrentState;
- }
- else
- {
- ;
- }
- ::CloseServiceHandle(hService);
- }
- ::CloseServiceHandle(hSCM);
- return bResult;
- }