PC Review


Reply
Thread Tools Rate Thread

BSTR from COM to C# always null

 
 
Volker Kugler
Guest
Posts: n/a
 
      14th Jul 2005
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

 
Reply With Quote
 
 
 
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      14th Jul 2005
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.

"Volker Kugler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
Volker Kugler
Guest
Posts: n/a
 
      14th Jul 2005
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

 
Reply With Quote
 
Willy Denoyette [MVP]
Guest
Posts: n/a
 
      14th Jul 2005

"Volker Kugler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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.


 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
what is LPCTSTR and BSTR in c#.net? Atul Rane Microsoft C# .NET 1 11th Sep 2008 01:15 PM
How do I retrun a BSTR from C++ to VB.NET Michael Tissington Microsoft VB .NET 8 8th Jul 2005 06:41 PM
BYTE* to BSTR =?Utf-8?B?RWRJblBob2VuaXg=?= Microsoft VC .NET 4 6th May 2005 08:09 PM
BSTR conversion =?Utf-8?B?RGlnaWZveA==?= Microsoft VC .NET 0 22nd Oct 2004 06:13 AM
Re: Shoehorning a BSTR into C# Mattias Sjögren Microsoft C# .NET 2 30th Apr 2004 08:44 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:36 PM.