I have C# Windows service class:
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 ...
}
}
How to convert an IntPtr (like this.ServiceHandle) to a "System.Runtime.InteropServices.SafeHandle"? so that I can use that in the function call "SetServiceObjectSecurity()"? My ultimate aim is to give admin permission to the Service.
Have you tried using SafeHandle.SetHandle(IntPtr handle)
as explained here https://msdn.microsoft.com/de-de/library/system.runtime.interopservices.safehandle.sethandle(v=vs.110).aspx and see if that works for you?
EDIT:
Since SafeHandle.SetHandle(IntPtr handle)
is a protected method, you could create a derived class like this:
public class SafeHandleExtended : SafeHandle
{
public void SetHandleExtended(IntPtr handle)
{
this.SetHandle(handle);
}
// .. SafeHandle's abstract methods etc. that you need to implement
}
Although I have not tested this for correctness, so I do not know if this works the way you want it to.