1,649
社区成员
发帖
与我相关
我的任务
分享
#if !defined(JADE_NEW_RETURN)
#inlcude "MiscFunction.h"
#endif
#include "JadeScManager.h"
#pragma comment(lib,"Advapi32")
JadeScManager::JadeScManager():m_hMag(NULL),m_lpBase(NULL),m_dwError(0),m_scLock(NULL)
{
m_hMag = OpenSCManager(NULL,NULL,SC_MANAGER_ALL_ACCESS);
if(m_hMag == NULL)
m_dwError = GetLastError();
}
JadeScManager::JadeScManager(LPCTSTR lpMachineName,LPCTSTR lpDatabaseName)
:m_hMag(NULL),m_lpBase(NULL),m_dwError(0),m_scLock(NULL)
{
m_hMag = OpenSCManager(lpMachineName,lpMachineName,SC_MANAGER_ALL_ACCESS);
if(m_hMag == NULL)
m_dwError = GetLastError();
}
JadeScManager::~JadeScManager()
{
if(m_hMag != NULL)
CloseServiceHandle(m_hMag);
if(m_lpBase != NULL)
{
if(HeapFree(GetProcessHeap(),0,(void*)m_lpBase))
m_lpBase = NULL;
}
}
DWORD JadeScManager::QueryWin32Service(DWORD dwSvrType,DWORD dwState,DWORD &dwBytesNeeded,
DWORD &dwCount,LPENUM_SERVICE_STATUS *lpBase)
{
if(m_hMag == NULL)
return m_dwError;
if(m_lpBase != NULL)
{
*lpBase = m_lpBase;
return JADE_HEAP_USED;
}
DWORD dwResumeHandle = 0,dwReturned = 0;
HANDLE hHeap = GetProcessHeap();
__try
{
m_lpBase = (LPENUM_SERVICE_STATUS)HeapAlloc(hHeap,HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS
,sizeof(ENUM_SERVICE_STATUS));
m_dwError = EnumServicesStatus(m_hMag,dwSvrType,dwState,m_lpBase,sizeof(ENUM_SERVICE_STATUS),
&dwBytesNeeded,&dwReturned,&dwResumeHandle);
//绝大部都应该是0
if(m_dwError == 0)
{
m_dwError= GetLastError();
if(m_dwError == 234)
{
//表示分配的空间过短
m_lpBase = (LPENUM_SERVICE_STATUS)HeapReAlloc(hHeap,HEAP_ZERO_MEMORY|HEAP_GENERATE_EXCEPTIONS,
(void*)m_lpBase,dwBytesNeeded);
}
else
{
*lpBase = NULL;
dwBytesNeeded = 0;
dwCount = 0;
if(HeapFree(hHeap,0,m_lpBase))
m_lpBase = NULL;
return m_dwError;
}
}
m_dwError = EnumServicesStatus(m_hMag,dwSvrType,dwState,m_lpBase,dwBytesNeeded,
&dwBytesNeeded,&dwCount,&dwResumeHandle);
if(m_dwError == 0)
{
*lpBase = NULL;
dwBytesNeeded = 0;
dwCount = 0;
if(HeapFree(hHeap,0,m_lpBase))
m_lpBase = NULL;
return (m_dwError = GetLastError()); //操作有误
}
//
*lpBase = m_lpBase;
}
__except(ExceptCode(GetExceptionCode(),GetExceptionInformation()))
{
lpBase = NULL;
dwBytesNeeded = 0;
dwCount = 0;
if(m_lpBase != NULL)
{
if(HeapFree(hHeap,0,m_lpBase))
m_lpBase = NULL;
}
return GetExceptionCode();
}
return 0;
}
DWORD JadeScManager::ReleaseQuery(LPENUM_SERVICE_STATUS lpBase)
{
__try
{
if(m_lpBase != NULL)
{
if(HeapFree(GetProcessHeap(),0,(void*)m_lpBase))
{
m_lpBase = NULL;
lpBase = NULL;
}
else
return GetLastError();
}
}
__except(ExceptCode(GetExceptionCode(),GetExceptionInformation()))
{
return GetExceptionCode();
}
return 0;
}
#if _WIN32_WINNT < 0x0500
inline DWORD JadeScManager::JadeService::QueryServiceStatus(LPSERVICE_STATUS lpServiceStatus)
{
return ( 0 == ::QueryServiceStatus(m_hSvr,lpServiceStatus)?GetLastError():0);
}
#endif
inline DWORD JadeScManager::GetServiceKeyName(JadeService &lpSvr,LPCTSTR lpDispName
,CString &szKeyName)
{
DWORD dwBufLen = 0,dwRetVal = 0;
LPSTR lpKey = NULL;
::GetServiceKeyName(lpSvr.m_hSvr,lpDispName,NULL,&dwBufLen);
if( ERROR_INSUFFICIENT_BUFFER != (dwRetVal = GetLastError()))
return dwRetVal;
lpKey = (LPSTR)malloc(dwBufLen);
if(!lpKey) return -1;
ZeroMemory(lpKey,dwBufLen);
if(!::GetServiceKeyName(lpSvr.m_hSvr,lpDispName,lpKey,&dwBufLen))
{
free(lpKey);
return GetLastError();
}
szKeyName = lpKey;
free(lpKey);
return 0;
}
inline DWORD JadeScManager::StartService(JadeService &lpSvr,DWORD dwNumServiceArgs
,LPCTSTR* lpServiceArgVectors)
{
BOOL bSuc = FALSE;
bSuc = ::StartService(lpSvr.m_hSvr,dwNumServiceArgs,lpServiceArgVectors);
return bSuc == TRUE?0:GetLastError();
}
inline DWORD JadeScManager::Lock(void)
{
if(m_hMag == NULL)
return m_dwError;
m_scLock = LockServiceDatabase(m_hMag);
return m_scLock == NULL?GetLastError():0;
}
inline DWORD JadeScManager::Unlock(void)
{
return 0 == UnlockServiceDatabase(m_scLock)?GetLastError():0;
}
inline DWORD JadeScManager::CreateService(JadeService &lpSvr,LPCTSTR lpSvrName,LPCTSTR lpDspName,
DWORD dwDsrAccess,DWORD dwSvrType,DWORD dwStartType,DWORD dwErrorControl,
LPCTSTR lpBinaryPathName,LPCTSTR lpLoadOrderGroup,LPDWORD lpdwTagId,
LPCTSTR lpDependencies,LPCTSTR lpSvrStartName,LPCTSTR lpPassword)
{
lpSvr.m_hSvr = ::CreateService(m_hMag,lpSvrName,lpDspName,dwDsrAccess,dwSvrType,dwStartType,dwErrorControl,
lpBinaryPathName,lpLoadOrderGroup,lpdwTagId,lpDependencies,lpSvrStartName,
lpPassword);
return lpSvr.m_hSvr == NULL?GetLastError():0;
}
inline DWORD JadeScManager::DeleteSvr(JadeService &lpSvr)
{
return 0 == DeleteService(lpSvr.m_hSvr)?GetLastError():0;
}