API
OpenSCManager
CreateService
OpenService
StartService
ControlService
DeleteService
CloseServiceHandle
代码示例
void CLoadDriverDlg::OnBtnLoad()
{
CString strPath;
m_edtPath.GetWindowText(strPath);
if (strPath.IsEmpty())
{
::SetDlgItemText(GetSafeHwnd(), IDC_STC_STATE, "请选择驱动文件路径");
return;
}
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL;
schSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
goto SAFE_EXIT;
}
schService = ::CreateService(
schSCManager, // SCManager database
SERVICENAME, // name of service
DISPLAYNAME, // service name to display
SERVICE_ALL_ACCESS, // desired access
SERVICE_KERNEL_DRIVER, // service type
SERVICE_DEMAND_START, // start type
SERVICE_ERROR_NORMAL, // error control type
strPath, // service's binary
NULL, // no load ordering group
NULL, // no tag identifier
NULL, // no dependencies
NULL, // LocalSystem account
NULL); // no password
if (schService == NULL)
{
goto SAFE_EXIT;
}
::SetDlgItemText(GetSafeHwnd(), IDC_STC_STATE, "服务安装成功");
SAFE_EXIT:
ShowState();
SafeCloseService(schService);
SafeCloseService(schSCManager);
}
void CLoadDriverDlg::ShowState()
{
LPVOID lpMsgBuf;
FormatMessage(
FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
GetLastError(),
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPTSTR) &lpMsgBuf,
0,
NULL
);
// Process any inserts in lpMsgBuf.
// ...
// Display the string.
//MessageBox( NULL, (LPCTSTR)lpMsgBuf, "Error", MB_OK | MB_ICONINFORMATION );
::SetDlgItemText(GetSafeHwnd(), IDC_STC_STATE, (LPCTSTR)lpMsgBuf);
// Free the buffer.
LocalFree( lpMsgBuf );
}
void CLoadDriverDlg::OnBtnUnload()
{
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL;
schSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
goto SAFE_EXIT;
}
schService = ::OpenService(
schSCManager, // SCM database
SERVICENAME, // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
goto SAFE_EXIT;
}
if (!DeleteService(schService))
{
goto SAFE_EXIT;
}
::SetDlgItemText(GetSafeHwnd(), IDC_STC_STATE, "服务卸载成功");
SAFE_EXIT:
ShowState();
SafeCloseService(schService);
SafeCloseService(schSCManager);
}
void CLoadDriverDlg::OnBtnStart()
{
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL;
schSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
goto SAFE_EXIT;
}
schService = ::OpenService(
schSCManager, // SCM database
SERVICENAME, // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
goto SAFE_EXIT;
}
if (!StartService(schService, 0 ,NULL))
{
goto SAFE_EXIT;
}
::SetDlgItemText(GetSafeHwnd(), IDC_STC_STATE, "启动成功");
SAFE_EXIT:
ShowState();
SafeCloseService(schService);
SafeCloseService(schSCManager);
}
void CLoadDriverDlg::OnBtnStop()
{
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL;
SERVICE_STATUS serviceStatus;
schSCManager = ::OpenSCManager(NULL, NULL, SC_MANAGER_ALL_ACCESS);
if (schSCManager == NULL)
{
goto SAFE_EXIT;
}
schService = ::OpenService(
schSCManager, // SCM database
SERVICENAME, // service name
SERVICE_ALL_ACCESS);
if (schService == NULL)
{
goto SAFE_EXIT;
}
if (!ControlService(schService, SERVICE_CONTROL_STOP, &serviceStatus))
{
goto SAFE_EXIT;
}
::SetDlgItemText(GetSafeHwnd(), IDC_STC_STATE, "停止成功");
SAFE_EXIT:
ShowState();
SafeCloseService(schService);
SafeCloseService(schSCManager);
}