BSTR from COM to C# always null

V

Volker Kugler

Hi,

i want to transmit a string from a C++-COM-DLL to C#. I wrote this
code:

STDMETHODIMP CWinpcap::hrGetAdapterNameFromAdapterNumber(int
iAdapterNumber, BSTR * AdapterName)
{
int index = 1;
dev = alldevs;

CComBSTR bstr = A2BSTR(dev->name);
*AdapterName = bstr.Copy();

return S_OK;
}

in VCC6. Then i generate the DLL and use tlbimp to generate a Wrapper
for C#.

In C# i use this code:

int nr = 0;

string sAdapterName = null;
WINPCAP_COM_ZUGRIFFLib.WinpcapClass wpcap = new
WINPCAP_COM_ZUGRIFFLib.WinpcapClass();
wpcap.hrGetNumberOfAdapters(ref nr);
wpcap.hrGetAdapterNameFromAdapterNumber(1,ref sAdapterName);

But when debugging in C#, sAdapterName is always empty. When i debug
the C++-DLL, i can see, that AdapterName points to the first character
of the BSTR.

Does anybody know what i made wrong?

Thanks for your help

Volker Kugler
 
W

Willy Denoyette [MVP]

Not sure how you managed to compile this, a BSTR* should be marked as an
[out] argument in the wrapper class, that means it should be passed as out
argument in C#.

..... (1,out sAdapterName);

I suggest you check the wrapper method signature using ildasm

Willy.
 
V

Volker Kugler

Hi,

i use the tlbimp to generate a wrapper an in C# it is shown as ref
string.

In C++ it is marked as [out].

Thanks

Volker
 
W

Willy Denoyette [MVP]

Volker Kugler said:
Hi,

i use the tlbimp to generate a wrapper an in C# it is shown as ref
string.

In C++ it is marked as [out].

Thanks

Volker


The question was how does it look like in IL. Run ildasm.exe on the interop
assembly and check the method signature.

Willy.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top