Click here to Skip to main content
15,901,035 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
Hi
I have a question about calling c# COM dll from c++ as below.

This is the method signature of c# COM dll
C#
public object GetData(object[] list, string view)


And this is the method signature when open tlb file from ITypeLib viewver

C++
HRESULT GetData(
                        [in] SAFEARRAY(VARIANT) list,
                        [in] BSTR view,
                        [out, retval] VARIANT* pRetVal);


And this is the c++ code that call the method

C++
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
Posted
Updated 19-Apr-10 0:09am
v2
Comments
PotatisPulver 3-Aug-10 12:05pm    
Reason for my vote of 4
clear code

try using :
C#
vout.bstrVal = _bstr_t("TESTDATA");

instead of :
C#
wchar_t datawt[100];
char tmp[100]={"TESTDATA"};
mbstowcs(datawt,tmp,100);
data = (BSTR)datawt;
 
Share this answer
 
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();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900