16,548
社区成员




bool TFormCreateCompetitPro::RestartMySQLService()
{
::SC_HANDLE schSCManager,
schService;
SERVICE_STATUS_PROCESS sspStatus;
SERVICE_STATUS ssStatus;
DWORD dwBytesNeeded;
int waitCount = 10;
//打开服务控制管理器,并获得句柄
if ((schSCManager = OpenSCManager(NULL, SERVICES_ACTIVE_DATABASE, SC_MANAGER_CONNECT)) == NULL)
return false;
//打开服务
if ((schService = OpenServiceA(schSCManager, "MySQL", SERVICE_ALL_ACCESS)) == NULL)
{
CloseServiceHandle(schSCManager);
return false;
}
//检查服务状态
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &sspStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
//如果服务未启动,则启动服务
if (sspStatus.dwCurrentState == SERVICE_STOPPED)
{
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
// 等待启动完成
do
{
if (waitCount <= 0)
return false;
::Sleep(1500);
//检查服务状态
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &sspStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
waitCount--;
}
while (sspStatus.dwCurrentState != SERVICE_RUNNING);
}
//如果服务正在停止中,则等待完全停止,再启动服务
else if (sspStatus.dwCurrentState == SERVICE_STOP_PENDING)
{
// 等待停止完成
do
{
::Sleep(1500);
//检查服务状态
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &sspStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
}
while (sspStatus.dwCurrentState != SERVICE_STOPPED);
//启动服务
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
// 等待启动完成
do
{
if (waitCount <= 0)
return false;
::Sleep(1500);
//检查服务状态
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &sspStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
waitCount--;
}
while (sspStatus.dwCurrentState != SERVICE_RUNNING);
}
// 如果正在运行中,则停止服务,再启动服务
else if (sspStatus.dwCurrentState == SERVICE_RUNNING)
{
//停止服务
if(!ControlService(schService, SERVICE_CONTROL_STOP, &ssStatus))
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
// 等待停止完成
do
{
::Sleep(1500);
//检查服务状态
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &sspStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
}
while (sspStatus.dwCurrentState != SERVICE_STOPPED);
//启动服务
if (!StartService(
schService, // handle to service
0, // number of arguments
NULL) ) // no arguments
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
// 等待启动完成
do
{
if (waitCount <= 0)
return false;
::Sleep(1500);
//检查服务状态
if (!QueryServiceStatusEx(
schService, // handle to service
SC_STATUS_PROCESS_INFO, // information level
(LPBYTE) &sspStatus, // address of structure
sizeof(SERVICE_STATUS_PROCESS), // size of structure
&dwBytesNeeded ) ) // size needed if buffer is too small
{
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return false;
}
waitCount--;
}
while (sspStatus.dwCurrentState != SERVICE_RUNNING);
}
CloseServiceHandle(schService);
CloseServiceHandle(schSCManager);
return true;
}