PC Review


Reply
Thread Tools Rate Thread

C# C++ dll problem - interop services

 
 
Jason
Guest
Posts: n/a
 
      4th Sep 2006
Hi,

I have a problem with some C# code. I have a function in a c++ dll which
returns a BSTR. I have retrieved the BSTR using p/invoke but the data
returned contains what appears to be random data at the start of the string
followed by correct data. I am wondering whether it is the length of the
BSTR prepended to the string that I am picking up, but doesn't
SysAllocStringByteLen get rid of this?

to retrieve the data:

[DllImport("Test")]
private static extern IntPtr getbagstr();

public static string getBagString()
{
IntPtr ptr = getbagstr();
string tmp = Marshal.PtrToStringAnsi(ptr);
Marshal.FreeBSTR(ptr);
return tmp;
}

This code appears to work when I alter the code within getbagstr() to
return "dfksdjfkjd"; for example, but fails to return the extra data when
returning an stl::string

extern "C" __declspec( dllexport ) BSTR getbagstr();
extern "C" __declspec( dllexport ) BSTR getbagstr() {
const char * buff = g.getbag()->getbagstr(true).c_str();
return SysAllocStringByteLen (buff , lstrlen(buff));
}


If I set the data from my C# class, see below, the string returned from the
code above is a few bytes of junk as if i only have a pointer.

private static extern void setbag(IntPtr ptr);

public static void setdata(string str)
{
IntPtr ptr = Marshal.StringToBSTR(str);
setbag(ptr.ToPointer());
Marshal.FreeBSTR(ptr);
}

The corresponding setbag function accepts LPSTR, so I guess passing an
IntPtr from a BSTR is the wrong way to go, but I am not sure anymore.
Please someone help!
 
Reply With Quote
 
 
 
 
Jason
Guest
Posts: n/a
 
      4th Sep 2006
On Mon, 4 Sep 2006 21:22:52 +0100, Jason wrote:

> Hi,
>
> I have a problem with some C# code. I have a function in a c++ dll which
> returns a BSTR. I have retrieved the BSTR using p/invoke but the data
> returned contains what appears to be random data at the start of the string
> followed by correct data. I am wondering whether it is the length of the
> BSTR prepended to the string that I am picking up, but doesn't
> SysAllocStringByteLen get rid of this?
>
> to retrieve the data:
>
> [DllImport("Test")]
> private static extern IntPtr getbagstr();
>
> public static string getBagString()
> {
> IntPtr ptr = getbagstr();
> string tmp = Marshal.PtrToStringAnsi(ptr);
> Marshal.FreeBSTR(ptr);
> return tmp;
> }
>
> This code appears to work when I alter the code within getbagstr() to
> return "dfksdjfkjd"; for example, but fails to return the extra data when
> returning an stl::string
>
> extern "C" __declspec( dllexport ) BSTR getbagstr();
> extern "C" __declspec( dllexport ) BSTR getbagstr() {
> const char * buff = g.getbag()->getbagstr(true).c_str();
> return SysAllocStringByteLen (buff , lstrlen(buff));
> }
>
>
> If I set the data from my C# class, see below, the string returned from the
> code above is a few bytes of junk as if i only have a pointer.
>
> private static extern void setbag(IntPtr ptr);
>
> public static void setdata(string str)
> {
> IntPtr ptr = Marshal.StringToBSTR(str);
>----- setbag(ptr.ToPointer());
> Marshal.FreeBSTR(ptr);
> }
>
> The corresponding setbag function accepts LPSTR, so I guess passing an
> IntPtr from a BSTR is the wrong way to go, but I am not sure anymore.
> Please someone help!


Sorry, did not call ToPointer(), should really be

setbag(ptr);
 
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
Interop.CDO uses Interop.ADODB which has a higher version... muriwai Microsoft C# .NET 7 27th Oct 2011 08:00 PM
Problems using IntPtr and Interop services to get image from unmanaged code cewood@techMatrixLLC.net Microsoft C# .NET 0 20th Sep 2007 05:58 AM
Return string from c++ dll - interop services Jason Microsoft C# .NET 5 7th Sep 2006 09:15 PM
Is there a published COM Interop Wrapper for Interop.MSDASC.dll? Burton G. Wilkins Microsoft ADO .NET 0 10th Nov 2003 01:57 AM
Interop Services ?? C Microsoft Dot NET 0 4th Jul 2003 04:38 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 10:25 AM.