if (m_scm!=NULL) //1st if
{
m_svc=OpenService(
m_scm,
"AccessControlService",
//Enables calling of the ControlService function to stop the service
//Enables calling of the QueryServiceStatus function to query the status of the service.
SERVICE_STOP|SERVICE_QUERY_STATUS
);
if (m_svc!=NULL)//2st if
{
QueryServiceStatus(m_svc,&m_ServiceStatus);//查询server的状态
if (m_ServiceStatus.dwCurrentState==SERVICE_RUNNING)//3st if
{
ControlService(m_svc,SERVICE_CONTROL_STOP,&m_ServiceStatus);
MessageBox("STOP SERVVICE SUCCEEDS !");
}//end 3st if
CloseServiceHandle(m_svc);
}//end of 2st if
CloseServiceHandle(m_scm);
}//end 1st if
GetDlgItem(IDB_STR)->EnableWindow(TRUE);
GetDlgItem(IDB_STOP)->EnableWindow(FALSE);
}
UpdateData(true);
m_scm=OpenSCManager(
NULL, // 指定计算机名为本机
NULL, // 指定要打开的service control managerdatabase名, 默认为空
SC_MANAGER_CREATE_SERVICE // 允许创建服务对象并把它加入database
);
if (m_scm!=NULL)//if open server database succeeds
{
/*SC_HANDLE*/m_svc=CreateService(
m_scm, // server database 的句柄
"AccessControlService", // Service名字
"AccessControlService", // 为Service显示用名
SERVICE_ALL_ACCESS, // 指定server的使用权限,可使用所有的权限
// 指定server的类型,
// Service that runs in its own process
// The service can interact with the desktop
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,
SERVICE_AUTO_START, // 以自动方式开始
SERVICE_ERROR_IGNORE, //说明当Service在启动中出错时采取什么动作
m_ExeFile,
NULL,NULL,NULL,NULL,NULL
);
if (m_svc!=NULL)
{
MessageBox("CREATE SERVIVR SUCCEEDS ! ");
}
else
{
MessageBox("CREATE SERVIVR FAILED") ;
}
Windows NT Services are securable objects in that they are associated with a Security Descriptor and a Discretionary Access Control List (DACL). The DACL associated with the Service has sole control over access to the Service. No special user rights or privileges are needed to manipulate a Service. An application requests a handle to a Service via the OpenService or CreateService API. If the user has the requested access to the Service object, a valid handle is returned. If the requested access is denied by the system, an error code 5 or "access denied" is returned.
sample code:
#include <windows.h>
#include <aclapi.h>
#include <stdio.h>
#include <tchar.h>
//
// Obtain a handle to the Service Controller.
//
schManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT);
if (schManager == NULL)
DisplayError(GetLastError(), TEXT("OpenSCManager"));
//
// Obtain a handle to the service.
//
schService = OpenService(schManager, argv[1],
READ_CONTROL | WRITE_DAC);
if (schService == NULL)
DisplayError(GetLastError(), TEXT("OpenService"));
//
// Get the current security descriptor.
//
if (!QueryServiceObjectSecurity(schService,
DACL_SECURITY_INFORMATION, psd, 0, &dwSize)){
if (GetLastError() == ERROR_INSUFFICIENT_BUFFER){
psd = (PSECURITY_DESCRIPTOR)HeapAlloc(GetProcessHeap(),
HEAP_ZERO_MEMORY, dwSize);
if (psd == NULL){
DisplayError(0, TEXT("HeapAlloc"));
// note HeapAlloc does not support GetLastError()
}
//
// Get the DACL.
//
if (!GetSecurityDescriptorDacl(psd, &bDaclPresent, &pacl,
&bDaclDefaulted))
DisplayError(GetLastError(), TEXT("GetSecurityDescriptorDacl"));
//
// Initialize a NEW Security Descriptor.
//
if (!InitializeSecurityDescriptor(&sd, SECURITY_DESCRIPTOR_REVISION))
DisplayError(GetLastError(),
TEXT("InitializeSecurityDescriptor"));
//
// Set the new DACL in the Security Descriptor.
//
if (!SetSecurityDescriptorDacl(&sd, TRUE, pNewAcl, FALSE))
DisplayError(GetLastError(), TEXT("SetSecurityDescriptorDacl"));
//
// Set the new DACL for the service object.
//
if (!SetServiceObjectSecurity(schService, DACL_SECURITY_INFORMATION,
&sd))
DisplayError(GetLastError(), TEXT("SetServiceObjectSecurity"));
//
// Close the handles.
//
if (!CloseServiceHandle(schManager))
DisplayError(GetLastError(), TEXT("CloseServiceHandle"));
if (!CloseServiceHandle(schService))
DisplayError(GetLastError(), TEXT("CloseServiceHandle"));