public static extern System.IntPtr OpenSCManager(
System.String lpMachineName,
System.String lpDatabaseName,
System.UInt32 dwDesiredAccess
);
[DllImport("advapi32.dll",EntryPoint = "CreateServiceA")]
public static extern System.IntPtr CreateService(
System.IntPtr hSCManager,
System.String lpServiceName,
System.String lpDisplayName,
System.UInt32 dwDesiredAccess,
System.UInt32 dwServiceType,
System.UInt32 dwStartType,
System.UInt32 dwErrorControl,
System.String lpBinaryPathName,
System.String lpLoadOrderGroup,
System.IntPtr lpdwTagId,
System.String lpDependencies,
System.String lpServiceStartName,
System.String lpPassword
);
[DllImport("advapi32.dll")]
public static extern System.Boolean CloseServiceHandle(
System.IntPtr hSCObject
);
System.IntPtr hService,
System.UInt32 dwInfoLevel,
ref System.String lpInfo
);
其中lpInfo的聲明原型是一個LPVOID,如果設置Description屬性的話指向的是一個結構:
LPTSTR lpDescription;
} SERVICE_DESCRIPTION, *LPSERVICE_DESCRIPTION;
這個結構裏面包含了一個字符指針,也就是說需要在函數調用時傳遞一個指向字符指針的指針,偷懶起見,ref System.String就足夠了,無需再定義結構了,呵呵!
{
System.Boolean fRet = false;
System.IntPtr hServiceManager = IntPtr.Zero,hService = IntPtr.Zero;
System.String sServicePath = null,sDesc = null;
hServiceManager = OpenSCManager(Environment.MachineName,null,SC_MANAGER_ALL_ACCESS);
if (hServiceManager != IntPtr.Zero)
{
hService = CreateService(hServiceManager,"sampleservice","Sample Service",SERVICE_ALL_ACCESS,
SERVICE_WIN32_OWN_PROCESS | SERVICE_INTERACTIVE_PROCESS,SERVICE_AUTO_START,SERVICE_ERROR_NORMAL,
sServicePath,null,IntPtr.Zero,null,null,null);
if (hService != IntPtr.Zero)
{
sDesc = "This is a sample service.";
fRet = ChangeServiceConfig2(hService,SERVICE_CONFIG_DESCRIPTION,ref sDesc);
CloseServiceHandle(hService);
hService = IntPtr.Zero;
}
CloseServiceHandle(hServiceManager);
hServiceManager = IntPtr.Zero;
}
return fRet;
}
public static extern System.IntPtr OpenService(
System.IntPtr hSCManager,
System.String lpServiceName,
System.UInt32 dwDesiredAccess
);
[DllImport("advapi32.dll")]
public static extern System.Boolean DeleteService(
System.IntPtr hService
);
比較簡單沒什麼可多說的看一個完整的例子:
{
System.Boolean fRet = false;
System.IntPtr hServiceManager = IntPtr.Zero,hService = IntPtr.Zero;
if (hServiceManager != IntPtr.Zero)
{
hService = OpenService(hServiceManager,"sampleservice",SERVICE_ALL_ACCESS);
if (hService != IntPtr.Zero)
{
fRet = DeleteService(hService);
CloseServiceHandle(hService);
hService = IntPtr.Zero;
}
CloseServiceHandle(hServiceManager);
hServiceManager = IntPtr.Zero;
}
return fRet;
}
[DllImport("advapi32.dll")]
public static extern System.IntPtr OpenSCManager(
System.String lpMachineName,
System.String lpDatabaseName,
System.UInt32 dwDesiredAccess
);
[DllImport("advapi32.dll",EntryPoint = "CreateServiceA")]
public static extern System.IntPtr CreateService(
System.IntPtr hSCManager,
System.String lpServiceName,
System.String lpDisplayName,
System.UInt32 dwDesiredAccess,
System.UInt32 dwServiceType,
System.UInt32 dwStartType,
System.UInt32 dwErrorControl,
System.String lpBinaryPathName,
System.String lpLoadOrderGroup,
System.IntPtr lpdwTagId,
System.String lpDependencies,
System.String lpServiceStartName,
System.String lpPassword
);
[DllImport("advapi32.dll")]
public static extern System.IntPtr OpenService(
System.IntPtr hSCManager,
System.String lpServiceName,
System.UInt32 dwDesiredAccess
);
[DllImport("advapi32.dll")]
public static extern System.Boolean DeleteService(
System.IntPtr hService
);
[DllImport("advapi32.dll")]
public static extern System.Boolean CloseServiceHandle(
System.IntPtr hSCObject
);
[DllImport("advapi32.dll")]
public static extern System.Boolean ChangeServiceConfig2(
System.IntPtr hService,
System.UInt32 dwInfoLevel,
ref System.String lpInfo
);
// Service Control Manager object specific access types
public const System.UInt32 SC_MANAGER_CONNECT = 0x0001;
public const System.UInt32 SC_MANAGER_CREATE_SERVICE = 0x0002;
public const System.UInt32 SC_MANAGER_ENUMERATE_SERVICE = 0x0004;
public const System.UInt32 SC_MANAGER_LOCK = 0x0008;
public const System.UInt32 SC_MANAGER_QUERY_LOCK_STATUS = 0x0010;
public const System.UInt32 SC_MANAGER_MODIFY_BOOT_CONFIG = 0x0020;
public const System.UInt32 SC_MANAGER_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
SC_MANAGER_CONNECT |
SC_MANAGER_CREATE_SERVICE |
SC_MANAGER_ENUMERATE_SERVICE |
SC_MANAGER_LOCK |
SC_MANAGER_QUERY_LOCK_STATUS |
SC_MANAGER_MODIFY_BOOT_CONFIG;
// Service object specific access type
public const System.UInt32 SERVICE_QUERY_CONFIG = 0x0001;
public const System.UInt32 SERVICE_CHANGE_CONFIG = 0x0002;
public const System.UInt32 SERVICE_QUERY_STATUS = 0x0004;
public const System.UInt32 SERVICE_ENUMERATE_DEPENDENTS = 0x0008;
public const System.UInt32 SERVICE_START = 0x0010;
public const System.UInt32 SERVICE_STOP = 0x0020;
public const System.UInt32 SERVICE_PAUSE_CONTINUE = 0x0040;
public const System.UInt32 SERVICE_INTERROGATE = 0x0080;
public const System.UInt32 SERVICE_USER_DEFINED_CONTROL = 0x0100;
public const System.UInt32 SERVICE_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED |
SERVICE_QUERY_CONFIG |
SERVICE_CHANGE_CONFIG |
SERVICE_QUERY_STATUS |
SERVICE_ENUMERATE_DEPENDENTS |
SERVICE_START |
SERVICE_STOP |
SERVICE_PAUSE_CONTINUE |
SERVICE_INTERROGATE |
SERVICE_USER_DEFINED_CONTROL;
// service type
public const System.UInt32 SERVICE_KERNEL_DRIVER = 0x00000001;
public const System.UInt32 SERVICE_FILE_SYSTEM_DRIVER = 0x00000002;
public const System.UInt32 SERVICE_ADAPTER = 0x00000004;
public const System.UInt32 SERVICE_RECOGNIZER_DRIVER = 0x00000008;
public const System.UInt32 SERVICE_DRIVER = SERVICE_KERNEL_DRIVER |
SERVICE_FILE_SYSTEM_DRIVER |
SERVICE_RECOGNIZER_DRIVER;
public const System.UInt32 SERVICE_WIN32_OWN_PROCESS = 0x00000010;
public const System.UInt32 SERVICE_WIN32_SHARE_PROCESS = 0x00000020;
public const System.UInt32 SERVICE_WIN32 = SERVICE_WIN32_OWN_PROCESS |
SERVICE_WIN32_SHARE_PROCESS;
public const System.UInt32 SERVICE_INTERACTIVE_PROCESS = 0x00000100;
public const System.UInt32 SERVICE_TYPE_ALL = SERVICE_WIN32 |
SERVICE_ADAPTER |
SERVICE_DRIVER |
SERVICE_INTERACTIVE_PROCESS;
// Start Type
public const System.UInt32 SERVICE_BOOT_START = 0x00000000;
public const System.UInt32 SERVICE_SYSTEM_START = 0x00000001;
public const System.UInt32 SERVICE_AUTO_START = 0x00000002;
public const System.UInt32 SERVICE_DEMAND_START = 0x00000003;
public const System.UInt32 SERVICE_DISABLED = 0x00000004;
// Error control type
public const System.UInt32 SERVICE_ERROR_IGNORE = 0x00000000;
public const System.UInt32 SERVICE_ERROR_NORMAL = 0x00000001;
public const System.UInt32 SERVICE_ERROR_SEVERE = 0x00000002;
public const System.UInt32 SERVICE_ERROR_CRITICAL = 0x00000003;
// Info levels for ChangeServiceConfig2 and QueryServiceConfig2
public const System.UInt32 SERVICE_CONFIG_DESCRIPTION = 1;
public const System.UInt32 SERVICE_CONFIG_FAILURE_ACTIONS = 2;