int
__fastcall Server_Servers::QueryServerStats(AnsiString SERV,AnsiString Service)
... {
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
SC_HANDLE schService , schSCManager ;
schSCManager = OpenSCManager(
SERV.c_str(), // pointer to machine name string
NULL, // pointer to database name string
SC_MANAGER_ALL_ACCESS // type of access
);
schService = OpenService(
schSCManager, // SCM database
Service.c_str(), // service name Topsky_ServerService
SERVICE_ALL_ACCESS);
if (schService == NULL)
...{
PMSGDLG->ShowErrorMsg("服务不存在!");
return -1;
}
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
...{
PMSGDLG->ShowErrorMsg("查询服务状态失败!");
return -1;
}
else
return ssStatus.dwCurrentState;
}
// 启动服务
void __fastcall Server_Servers::StartServerServers(AnsiString SERV,AnsiString Service)
... {
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
SC_HANDLE schService , schSCManager ;
schSCManager = OpenSCManager(
SERV.c_str(), // pointer to machine name string
NULL, // pointer to database name string
SC_MANAGER_ALL_ACCESS // type of access
);
schService = OpenService(
schSCManager, // SCM database
Service.c_str(), // service name Topsky_ServerService
SERVICE_ALL_ACCESS);
if (schService == NULL)
...{
PMSGDLG->ShowErrorMsg("服务不存在!");
return ;
}
if (!StartService(schService, 0, NULL)) //
...{
PMSGDLG->ShowErrorMsg("无法启动服务!");
return ;
}
else
;// PMSGDLG->ShowErrorMsg("服务正在启动...");
// Check the status until the service is no longer start pending.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
...{
PMSGDLG->ShowErrorMsg("查询服务状态失败!");
return ;
}
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
...{
// Save the current checkpoint.
dwOldCheckPoint = ssStatus.dwCheckPoint;
// Wait for the specified interval.
Sleep(ssStatus.dwWaitHint);
// Check the status again.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
break;
// Break if the checkpoint has not been incremented.
if (dwOldCheckPoint >= ssStatus.dwCheckPoint)
break;
}
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
;// PMSGDLG->ShowErrorMsg("启动服务成功!");
else
...{
PMSGDLG->ShowErrorMsg("CurrentState: " + AnsiString(ssStatus.dwCurrentState));
PMSGDLG->ShowErrorMsg("Win32ExitCode: " + AnsiString(ssStatus.dwWin32ExitCode));
PMSGDLG->ShowErrorMsg("ServiceSpecificExitCode: " + AnsiString(ssStatus.dwServiceSpecificExitCode));
PMSGDLG->ShowErrorMsg("CheckPoint: " + AnsiString( ssStatus.dwCheckPoint));
PMSGDLG->ShowErrorMsg("WaitHint: " + AnsiString( ssStatus.dwWaitHint));
}
CloseServiceHandle(schService);
}
// 停止服务
void __fastcall Server_Servers::StopServerServers(AnsiString SERV,AnsiString Service)
... {
SC_HANDLE schSCManager , schService ;
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
schSCManager = OpenSCManager(SERV.c_str(), NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager != NULL)
...{
schService = OpenService(schSCManager, Service.c_str(), SERVICE_STOP | SERVICE_QUERY_STATUS);
if (schService != NULL)
...{
ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus);
Sleep(2000);
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
...{
PMSGDLG->ShowErrorMsg("查询服务状态失败!");
return ;
}
while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
...{
// Save the current checkpoint.
dwOldCheckPoint = ssStatus.dwCheckPoint;
// Wait for the specified interval.
Sleep(ssStatus.dwWaitHint);
// Check the status again.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
break;
// Break if the checkpoint has not been incremented.
if (dwOldCheckPoint >= ssStatus.dwCheckPoint)
break;
}
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
;// PMSGDLG->ShowErrorMsg("停止服务成功!");
else
...{
PMSGDLG->ShowErrorMsg("CurrentState: " + AnsiString(ssStatus.dwCurrentState));
PMSGDLG->ShowErrorMsg("Win32ExitCode: " + AnsiString(ssStatus.dwWin32ExitCode));
PMSGDLG->ShowErrorMsg("ServiceSpecificExitCode: " + AnsiString(ssStatus.dwServiceSpecificExitCode));
PMSGDLG->ShowErrorMsg("CheckPoint: " + AnsiString( ssStatus.dwCheckPoint));
PMSGDLG->ShowErrorMsg("WaitHint: " + AnsiString( ssStatus.dwWaitHint));
}
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}
// 暂停服务
void __fastcall Server_Servers::PauseServerServers(AnsiString SERV,AnsiString Service)
... {
SC_HANDLE schSCManager , schService ;
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
schSCManager = OpenSCManager(SERV.c_str(), NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager != NULL)
...{
schService = OpenService(schSCManager, Service.c_str(), SERVICE_PAUSE_CONTINUE);
if (schService != NULL)
...{
ControlService(schService, SERVICE_CONTROL_PAUSE, &ssStatus);
Sleep(3000);
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}
// 继续服务
void __fastcall Server_Servers::ContinueServerServers(AnsiString SERV,AnsiString Service)
... {
SC_HANDLE schSCManager , schService ;
SERVICE_STATUS ServiceStatus;
schSCManager = OpenSCManager(SERV.c_str(), NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager != NULL)
...{
schService = OpenService(schSCManager, Service.c_str(), SERVICE_PAUSE_CONTINUE);
if (schService != NULL)
...{
QueryServiceStatus(schService, &ServiceStatus);
ControlService(schService, SERVICE_CONTROL_CONTINUE, &ServiceStatus);
Sleep(3000);
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}
// 取得网上邻居
bool __fastcall Server_Servers::EnumHosts(NETRESOURCE * pNetRes)
... {
HANDLE hEnum = NULL;
DWORD dwRet= WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, pNetRes, &hEnum);
if (dwRet != NO_ERROR)
return false;
NETRESOURCE* pResBuf = new NETRESOURCE[8];
DWORD cCount, dwBuf = 8*sizeof( NETRESOURCE);
DWORD i;
do
...{
cCount = (DWORD)-1;
dwRet = WNetEnumResource(hEnum, &cCount, (LPVOID)pResBuf, &dwBuf);
for ( i = 0; i < cCount; ++i)
...{
;// AddHostToVector(&pResBuf[i]);
}
if(pResBuf[i].dwUsage & RESOURCEUSAGE_CONTAINER)
EnumHosts(&pResBuf[i]);
if (dwRet == ERROR_NO_MORE_ITEMS)
...{ //Enum finished.
dwRet = NO_ERROR;
break;
}
else if (dwRet != NO_ERROR)
break;
}
while (dwRet == NO_ERROR);
delete pResBuf;
return (dwRet == NO_ERROR);
}
... {
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
SC_HANDLE schService , schSCManager ;
schSCManager = OpenSCManager(
SERV.c_str(), // pointer to machine name string
NULL, // pointer to database name string
SC_MANAGER_ALL_ACCESS // type of access
);
schService = OpenService(
schSCManager, // SCM database
Service.c_str(), // service name Topsky_ServerService
SERVICE_ALL_ACCESS);
if (schService == NULL)
...{
PMSGDLG->ShowErrorMsg("服务不存在!");
return -1;
}
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
...{
PMSGDLG->ShowErrorMsg("查询服务状态失败!");
return -1;
}
else
return ssStatus.dwCurrentState;
}
// 启动服务
void __fastcall Server_Servers::StartServerServers(AnsiString SERV,AnsiString Service)
... {
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
SC_HANDLE schService , schSCManager ;
schSCManager = OpenSCManager(
SERV.c_str(), // pointer to machine name string
NULL, // pointer to database name string
SC_MANAGER_ALL_ACCESS // type of access
);
schService = OpenService(
schSCManager, // SCM database
Service.c_str(), // service name Topsky_ServerService
SERVICE_ALL_ACCESS);
if (schService == NULL)
...{
PMSGDLG->ShowErrorMsg("服务不存在!");
return ;
}
if (!StartService(schService, 0, NULL)) //
...{
PMSGDLG->ShowErrorMsg("无法启动服务!");
return ;
}
else
;// PMSGDLG->ShowErrorMsg("服务正在启动...");
// Check the status until the service is no longer start pending.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
...{
PMSGDLG->ShowErrorMsg("查询服务状态失败!");
return ;
}
while (ssStatus.dwCurrentState == SERVICE_START_PENDING)
...{
// Save the current checkpoint.
dwOldCheckPoint = ssStatus.dwCheckPoint;
// Wait for the specified interval.
Sleep(ssStatus.dwWaitHint);
// Check the status again.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
break;
// Break if the checkpoint has not been incremented.
if (dwOldCheckPoint >= ssStatus.dwCheckPoint)
break;
}
if (ssStatus.dwCurrentState == SERVICE_RUNNING)
;// PMSGDLG->ShowErrorMsg("启动服务成功!");
else
...{
PMSGDLG->ShowErrorMsg("CurrentState: " + AnsiString(ssStatus.dwCurrentState));
PMSGDLG->ShowErrorMsg("Win32ExitCode: " + AnsiString(ssStatus.dwWin32ExitCode));
PMSGDLG->ShowErrorMsg("ServiceSpecificExitCode: " + AnsiString(ssStatus.dwServiceSpecificExitCode));
PMSGDLG->ShowErrorMsg("CheckPoint: " + AnsiString( ssStatus.dwCheckPoint));
PMSGDLG->ShowErrorMsg("WaitHint: " + AnsiString( ssStatus.dwWaitHint));
}
CloseServiceHandle(schService);
}
// 停止服务
void __fastcall Server_Servers::StopServerServers(AnsiString SERV,AnsiString Service)
... {
SC_HANDLE schSCManager , schService ;
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
schSCManager = OpenSCManager(SERV.c_str(), NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager != NULL)
...{
schService = OpenService(schSCManager, Service.c_str(), SERVICE_STOP | SERVICE_QUERY_STATUS);
if (schService != NULL)
...{
ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus);
Sleep(2000);
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
...{
PMSGDLG->ShowErrorMsg("查询服务状态失败!");
return ;
}
while (ssStatus.dwCurrentState == SERVICE_STOP_PENDING)
...{
// Save the current checkpoint.
dwOldCheckPoint = ssStatus.dwCheckPoint;
// Wait for the specified interval.
Sleep(ssStatus.dwWaitHint);
// Check the status again.
if (!QueryServiceStatus(
schService, // handle to service
&ssStatus) ) // address of status information
break;
// Break if the checkpoint has not been incremented.
if (dwOldCheckPoint >= ssStatus.dwCheckPoint)
break;
}
if (ssStatus.dwCurrentState == SERVICE_STOPPED)
;// PMSGDLG->ShowErrorMsg("停止服务成功!");
else
...{
PMSGDLG->ShowErrorMsg("CurrentState: " + AnsiString(ssStatus.dwCurrentState));
PMSGDLG->ShowErrorMsg("Win32ExitCode: " + AnsiString(ssStatus.dwWin32ExitCode));
PMSGDLG->ShowErrorMsg("ServiceSpecificExitCode: " + AnsiString(ssStatus.dwServiceSpecificExitCode));
PMSGDLG->ShowErrorMsg("CheckPoint: " + AnsiString( ssStatus.dwCheckPoint));
PMSGDLG->ShowErrorMsg("WaitHint: " + AnsiString( ssStatus.dwWaitHint));
}
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}
// 暂停服务
void __fastcall Server_Servers::PauseServerServers(AnsiString SERV,AnsiString Service)
... {
SC_HANDLE schSCManager , schService ;
SERVICE_STATUS ssStatus;
DWORD dwOldCheckPoint;
schSCManager = OpenSCManager(SERV.c_str(), NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager != NULL)
...{
schService = OpenService(schSCManager, Service.c_str(), SERVICE_PAUSE_CONTINUE);
if (schService != NULL)
...{
ControlService(schService, SERVICE_CONTROL_PAUSE, &ssStatus);
Sleep(3000);
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}
// 继续服务
void __fastcall Server_Servers::ContinueServerServers(AnsiString SERV,AnsiString Service)
... {
SC_HANDLE schSCManager , schService ;
SERVICE_STATUS ServiceStatus;
schSCManager = OpenSCManager(SERV.c_str(), NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager != NULL)
...{
schService = OpenService(schSCManager, Service.c_str(), SERVICE_PAUSE_CONTINUE);
if (schService != NULL)
...{
QueryServiceStatus(schService, &ServiceStatus);
ControlService(schService, SERVICE_CONTROL_CONTINUE, &ServiceStatus);
Sleep(3000);
CloseServiceHandle(schService);
}
CloseServiceHandle(schSCManager);
}
}
// 取得网上邻居
bool __fastcall Server_Servers::EnumHosts(NETRESOURCE * pNetRes)
... {
HANDLE hEnum = NULL;
DWORD dwRet= WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0, pNetRes, &hEnum);
if (dwRet != NO_ERROR)
return false;
NETRESOURCE* pResBuf = new NETRESOURCE[8];
DWORD cCount, dwBuf = 8*sizeof( NETRESOURCE);
DWORD i;
do
...{
cCount = (DWORD)-1;
dwRet = WNetEnumResource(hEnum, &cCount, (LPVOID)pResBuf, &dwBuf);
for ( i = 0; i < cCount; ++i)
...{
;// AddHostToVector(&pResBuf[i]);
}
if(pResBuf[i].dwUsage & RESOURCEUSAGE_CONTAINER)
EnumHosts(&pResBuf[i]);
if (dwRet == ERROR_NO_MORE_ITEMS)
...{ //Enum finished.
dwRet = NO_ERROR;
break;
}
else if (dwRet != NO_ERROR)
break;
}
while (dwRet == NO_ERROR);
delete pResBuf;
return (dwRet == NO_ERROR);
}