Kep*_*boy 2 .net c# unmanaged marshalling
是什么促成了这个问题:我正在尝试更改运行Windows服务的帐户.我决定使用win32 api而不是WMI,并开始查看ChangeServiceConfig.
我以为我可以简单地在非托管方法签名中使用SecureString类型,它可以正常工作.嗯,不太好.这让我想知道,有没有人知道什么本机(win32)类型SecureString类被编组为(默认情况下)非托管代码?
我从来没有得到标题中问题的答案,所以我仍然不知道SecureString被编组为非托管代码.
但是,我确实通过使用其他一些答案中的建议来使我的代码工作.我提供了以下代码.
internal static class NativeService
{
...
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool ChangeServiceConfig(IntPtr hService, uint nServiceType, uint nStartType, uint nErrorControl, string lpBinaryPathName, string lpLoadOrderGroup, IntPtr lpdwTagId, [In] char[] lpDependencies, string lpServiceStartName, IntPtr lpPassword, string lpDisplayName);
...
public const uint SERVICE_NO_CHANGE = 0xffffffff;
}
internal class ClassThatConfiguresService
{
...
public void ConfigureStartupAccount(IntPtr service, string userName, SecureString password)
{
passwordPointer = Marshal.SecureStringToGlobalAllocUnicode(password);
try
{
if(!NativeService.ChangeServiceConfig(service, NativeService.SERVICE_NO_CHANGE, NativeService.SERVICE_NO_CHANGE, NativeService.SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, userName, passwordPointer, null))
throw new Win32Exception(Marshal.GetLastWin32Error());
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(passwordPointer);
}
}
...
}
Run Code Online (Sandbox Code Playgroud)
我使用的是Marshal.SecureStringToGlobalAllocUnicode,而不是Marshal.SecureStringToGlobalAllocUnicode,因为这是非托管函数所期望的,但除此之外它还可以使用.
希望其他人觉得这很有用.白马.