你如何手动编组BSTR从.NET到COM?
我试图将存储在SecureString
中的秘密从我的.NET应用程序传递给COM对象上的方法。我想避免将SecureString
转换为String
,因为这不是你应该如何使用SecureString
。然后秘密将在托管内存中以明文形式显示,并在那里闲逛很长一段时间。相反,我正在尝试使用Right Way™:将其转换为非托管内存中的BSTR,将其传递给COM方法,然后使用Marshal.ZeroFreeBSTR
清除BSTR,以便密码仅在有限时间内暴露。
TblImp.exe为此COM对象生成的.NET接口期望将密钥作为String
传递,然后将其封送到BSTR。这不是我想要的,因为它意味着我必须把我的秘密放在String
,所以跟着这个Lean Method for Invoking COM,我已经创建了我自己的界面,以便我可以自定义它并摆脱String
。这是我开始的:
[ComImport, Guid("XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX")]
[InterfaceType(ComInterfaceType.InterfaceIsIDispatch)]
public interface ITheComObjectDispatcher
{ // +--- the naughty string
[return: MarshalAs(UnmanagedType.BStr)] // |
[DispId(1)] // V
string TheMethod([In, MarshalAs(UnmanagedType.BStr)] string secret);
}
[ComImport, Guid("YYYYYYYY-YYYY-YYYY-YYYY-YYYYYYYYYYYY")]
public class TheComObject
{}
这有效 - 但我必须将SecureString
转换为String
才能使用它,如下所示:
private string UseTheMethod(SecureString secret)
{
var comObject = new TheComObject();
var comObjectDispatcher = (ITheComObjectDispatcher)comObject;
var secretAsString = NaughtilyConvertSecureStringToString(secret);
return comObjectDispatcher.TheMethod(secretAsString);
}
private static string NaughtilyConvertSecureStringToString(SecureString value)
{
var valuePtr = Marshal.SecureStringToGlobalAllocUnicode(value);
try
{
return Marshal.PtrToStringUni(valuePtr); // now the secret is in managed memory :(
}
finally
{
Marshal.ZeroFreeGlobalAllocUnicode(valuePtr);
}
}
相反,我想使用像Marshal.SecureStringToBSTR
这样的方法,并跳过转换为String
。我试过这个......
[return: MarshalAs(UnmanagedType.BStr)]
[DispId(1)]
string TheMethod([In, MarshalAs(UnmanagedType.BStr)] IntPtr secret);
... 还有这个...
[return: MarshalAs(UnmanagedType.BStr)]
[DispId(1)]
string TheMethod([In] IntPtr secret);
...用这个调用代码......
private string UseTheMethod(SecureString secret)
{
var comObject = new TheComObject();
var comObjectDispatcher = (ITheComObjectDispatcher)comObject;
var secretPtr = Marshal.SecureStringToBSTR(secret);
try
{
return comObjectDispatcher.TheMethod(secretPtr);
}
finally
{
Marshal.ZeroFreeBSTR(secretPtr);
}
}
...但它不起作用:错误的值传递给COM方法。它不会因错误而失败 - 它只会产生不同的结果。
问题:
假设你在.idl中有这个:
interface ITheComObjectDispatcher : IDispatch
{
HRESULT TheMethod(BSTR secret, [out, retval] BSTR *pOut);
};
这将与.NET的tlbimp(如您所见)相似:
[ComImport, TypeLibType((short) 0x10c0), Guid("D4089F1D-5D83-4D1C-92CD-5941B35D43AA")]
public interface ITheComObjectDispatcher
{
[return: MarshalAs(UnmanagedType.BStr)]
[MethodImpl(MethodImplOptions.InternalCall, MethodCodeType=MethodCodeType.Runtime), DispId(0x60020000)]
string TheMethod([MarshalAs(UnmanagedType.BStr)] string secret);
}
因此,如果您不想使用不安全的string
参数,则不能直接使用它。解决方案是在C#中重新定义COM接口,例如这样(注意InterfaceIsDual
选项):
[InterfaceType(ComInterfaceType.InterfaceIsDual), Guid("D4089F1D-5D83-4D1C-92CD-5941B35D43AA")]
public interface ITheComObjectDispatcher2
{
[DispId(0x60020000)]
IntPtr TheMethod(IntPtr secret);
}
并像这样使用它:
var comObject = new TheComObject();
var comObjectDispatcher = (ITheComObjectDispatcher2)comObject;
var ptr = Marshal.SecureStringToBSTR(mySecureString);
try
{
var outPtr = doc.TheMethod(ptr);
// get the output string
var output = Marshal.PtrToStringBSTR(outPtr);
// free the callee-allocated BSTR
Marshal.FreeBSTR(outPtr);
}
finally
{
// free the secure string allocated BSTR
Marshal.ZeroFreeBSTR(ptr);
}