关于从C ++调用C#COM dll的问题 [英] Question about calling c# COM dll from c++
问题描述
嗨
我有一个关于从C ++调用c#COM dll的问题,如下所示.
这是C#COM dll的方法签名.
公共 对象 GetData(对象 []列表,字符串视图)
这是从ITypeLib viewver打开tlb文件时的方法签名
HRESULT GetData(
[在中] SAFEARRAY(VARIANT)列表,
[在中] BSTR视图,
[out,retval] VARIANT * pRetVal);
这是调用该方法的C ++代码
SAFEARRAY * a;
SAFEARRAYBOUND aDim [ 1 ];
aDim [ 0 ].lLbound = 0 ;
aDim [ 0 ].cElements = 1 ;
a = SafeArrayCreate(VT_VARIANT, 1 ,aDim);
BSTR数据;
wchar_t datawt [ 100 ];
字符 tmp [ 100 ] = {" TESTDATA"};
mbstowcs(datawt,tmp, 100 );
数据=(BSTR)数据重量;
变化的vout;
VariantInit(& vout);
vout.vt = VT_BSTR;
vout.bstrVal =数据;
HRESULT hr = S_OK;
长索引= 0 ;
如果(hr = SafeArrayPutElement(a,& index,& vout))
{
SafeArrayDestroy(a);
}
_variant_t结果= k-> GetData(a," );
我发现有一个对象数组,当调用COM dll时,其长度为1作为c ++代码,但对象数组中的数据为空白.
有谁知道为什么数据为空白以及该问题的解决方案是什么?
Thx
尝试使用:
vout.bstrVal = _bstr_t(" );
代替:
wchar_t datawt [ 100 ]; 字符 tmp [ 100 ] = {" TESTDATA"}; mbstowcs(datawt,tmp, 100 ); data =(BSTR)datawt;
调用如下:k-> GetData(a,"VIEW");看起来不好.
将文本字符串传递到COM时,请始终传递BSTR的新实例,然后由COM运行时将其释放.
在上面的示例中,您应该传递_bstr_t(_T("VIEW")).copy();
Hi
I have a question about calling c# COM dll from c++ as below.
This is the method signature of c# COM dll
public object GetData(object[] list, string view)
And this is the method signature when open tlb file from ITypeLib viewver
HRESULT GetData(
[in] SAFEARRAY(VARIANT) list,
[in] BSTR view,
[out, retval] VARIANT* pRetVal);
And this is the c++ code that call the method
SAFEARRAY* a;
SAFEARRAYBOUND aDim[1];
aDim[0].lLbound = 0;
aDim[0].cElements = 1;
a = SafeArrayCreate( VT_VARIANT, 1, aDim);
BSTR data;
wchar_t datawt[100];
char tmp[100]={"TESTDATA"};
mbstowcs(datawt,tmp,100);
data = (BSTR)datawt;
VARIANT vout;
VariantInit(&vout);
vout.vt = VT_BSTR;
vout.bstrVal = data;
HRESULT hr= S_OK;
long index = 0;
if(hr = SafeArrayPutElement(a,&index,&vout))
{
SafeArrayDestroy(a);
}
_variant_t result = k->GetData(a,"VIEW");
I''ve found there is an object array which has lenght is 1 as the c++ code when COM dll is called but the data in the object array is blank.
Does anyone know why the data is blank and what is the solution for this problem ?
Thx
try using :
vout.bstrVal = _bstr_t("TESTDATA");
instead of :
wchar_t datawt[100]; char tmp[100]={"TESTDATA"}; mbstowcs(datawt,tmp,100); data = (BSTR)datawt;
Call like this: k->GetData(a,"VIEW"); doesn''t look good.
When passing a text string into COM, always pass a new instance of BSTR, which then will be deallocated by COM run-time.
In the example above, you should pass _bstr_t(_T("VIEW")).copy();
这篇关于关于从C ++调用C#COM dll的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!