[英]change status of windows services in c/c++
如何從C / C ++程序中更改Windows服務的當前狀態?
例如,Mysql作為服務運行,其當前狀態為“已啟動”...如何檢查狀態以及如何從ac / c ++程序更改其狀態? 如果我想將其狀態從“已啟動”更改為“已停止” - 我該如何在c / c ++中執行此操作?
QueryServiceStatus
可用於確定服務的狀態。
查看其他服務功能以更改狀態。 甚至還有一個完整的Starting a Service示例(以及匹配的Stopping a Service代碼)。
一個小代碼片段,你應該開始:
/* Open service control manager. */
SC_HANDLE scm_handle = OpenSCManager(0,
0,
SC_MANAGER_ALL_ACCESS);
/* Ensure (0 != scm_handle) */
/* Open service. */
SC_HANDLE service_handle = OpenService(scm_handle,
"mysql-service-name",
SERVICE_ALL_ACCESS);
/* Ensure (0 != service_handle) */
/* Try to stop the service if it is running. */
SERVICE_STATUS status; /* This may need populated differently for mysql. */
status.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
if (ControlService(service_handle, SERVICE_CONTROL_STOP, &status))
{
Sleep(1000);
while (QueryServiceStatus(service_handle, &status))
{
if(status.dwCurrentState == SERVICE_STOP_PENDING)
{
Sleep(1000);
}
else
{
break;
}
}
if (status.dwCurrentState == SERVICE_STOPPED)
{
/* Success: service stopped. */
}
else
{
/* Failure: service not stopped. */
}
}
else
{
/* Failed to issue stop request. */
}
CloseServiceHandle(service_handle);
CloseServiceHandle(scm_handle);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.