我有一个C#的Windows服务类:
class MyService : ServiceBase
{
private void InitializeComponent() {
//some other code ...
SafeHandle sHandle = this.ServiceHandle; // I want to do this but this fails.
SetServiceObjectSecurity(sHandle, secInfo, binaryDescriptor);
//some more code ...
}
}
如何将IntPtr(例如this.ServiceHandle)转换为“System.Runtime.InteropServices.SafeHandle”?这样我就可以在函数调用“SetServiceObjectSecurity()”中使用它了。我的最终目标是给服务提供管理员权限。
ServiceSecurity
或ServiceAccessRule
类。总共大约有50行代码(主要是样板代码)。好的一面是,这意味着您不必在安全描述符或非托管调用上瞎折腾。 - Jeroen Mostert