我有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()“中使用它了?我的最终目标是给予行政许可的服务。
发布于 2017-04-13 09:51:47
您试过像这里解释的那样使用SafeHandle.SetHandle(IntPtr handle)吗,https://msdn.microsoft.com/de-de/library/system.runtime.interopservices.safehandle.sethandle(v=vs.110).aspx,看看它是否适合您?
编辑:由于SafeHandle.SetHandle(IntPtr handle)是一个受保护的方法,您可以创建如下所示的派生类:
public class SafeHandleExtended : SafeHandle
{
public void SetHandleExtended(IntPtr handle)
{
this.SetHandle(handle);
}
// .. SafeHandle's abstract methods etc. that you need to implement
}虽然我还没有测试它的正确性,所以我不知道它是否按照您希望的方式工作。
https://stackoverflow.com/questions/43388912
复制相似问题