CSharp Tips:调用API注册和注销Windows Service

王朝c#·作者佚名  2006-12-17
窄屏简体版  字體:   |    |    |  超大  

0、写在前面 [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.Boolean CloseServiceHandle(

System.IntPtr hSCObject

); public static extern System.Boolean ChangeServiceConfig2(

System.IntPtr hService,

System.UInt32 dwInfoLevel,

ref System.String lpInfo

);

其中lpInfo的声明原型是一个LPVOID,如果设置Description属性的话指向的是一个结构: typedef struct _SERVICE_DESCRIPTION {

LPTSTR lpDescription;

} SERVICE_DESCRIPTION, *LPSERVICE_DESCRIPTION;

这个结构里面包含了一个字符指针,也就是说需要在函数调用时传递一个指向字符指针的指针,偷懒起见,ref System.String就足够了,无需再定义结构了,呵呵! private static System.Boolean RegistService()

{

System.Boolean fRet = false;

System.IntPtr hServiceManager = IntPtr.Zero,hService = IntPtr.Zero;

System.String sServicePath = null,sDesc = null; sServicePath = Application.StartupPath + @"\sampleservice.exe";

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;

}

[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

);

比较简单没什么可多说的看一个完整的例子: private static System.Boolean UnRegistService()

{

System.Boolean fRet = false;

System.IntPtr hServiceManager = IntPtr.Zero,hService = IntPtr.Zero; hServiceManager = OpenSCManager(Environment.MachineName,null,SC_MANAGER_ALL_ACCESS);

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;

}

// declare APIs for service

[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

); public const System.UInt32 STANDARD_RIGHTS_REQUIRED = 0xF0000;

// 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;

 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
© 2005- 王朝網路 版權所有 導航