c#程序调用以BSTR作为输入参数的vc++函数
本文关键字:参数 输入 vc++ 函数 程序 调用 BSTR | 更新日期: 2023-09-27 18:10:38
从c#程序中,我调用vc++函数,其中BSTR作为输入参数。我没有得到任何返回值。我的c#程序崩溃了。
c#: SampleCS,
var value = object。GetValue(10、"测试");
vc++: SampleCPP,
GetValue(long index, BSTR key, double * Value){CString str = key;....}
任何想法如果我错过了什么或做错了什么?
注意:如果我从VB6调用vc++的GetValue函数,那么它可以正常工作。
Marshal
类具有从托管代码中处理BSTR
的方法。试试这样做:
// Note that the BSTR parameter in C++ becomes
// an IntPtr in this PInvoke declaration.
[DllImport("your.dll")]
private void GetValue(long index, IntPtr key, ref double value);
使用此方法:
double result = 0;
var stringValue = "Foo";
var bstrValue = Marshal.StringToBSTR(stringValue);
try
{
GetValue(0, bstrValue, ref result);
}
finally
{
Marshal.FreeBSTR(bstrValue);
}