473,395 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

C# C++ dll problem - interop services

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!
Sep 4 '06 #1
1 4179
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);
Sep 4 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Razzie | last post by:
Hey all, I developed an application as a com object. I set the 'register for com interop' in the project properties to true. How do I deploy this on another machine? I can't add the DLL as a...
20
by: Razzie | last post by:
Hey all, I'm really going through a small hell right now - I've completely lost it :) I made a project, using two interop libraries from exchange (created them as in this msdn article:...
2
by: Joris Dobbelsteen | last post by:
Is it possible to make a Windows Service that has a COM interop. I want to synchronize my database and ISA server blocklist. The application is quite simple and should do the trick, provided I...
2
by: Alex D. | last post by:
Hi .I cant seem to find a way to authorize asp.net user to create an Excel application using this: Microsoft.Office.Interop.Excel.ApplicationClass excel = new...
3
by: Jay A. Moritz | last post by:
I have an application that loads IE and iterates through web pages to retrieve information from the page then activates specific controls to retrieve the next page and iterate through that page for...
0
by: Laura Papez | last post by:
(note: orginally posted to the dotnet.framework.webservices.enhancements folks - thought this group may have more experience in this area. Ta, Laura.) Hi, I have created a secure Web Service...
2
by: Richard Collette | last post by:
Hi, I have a service, that runs perfectly when executed outside of the web service environment. When called as a web service I get the exception listed below sporadically. A call to the web...
0
by: SuperJason | last post by:
We have a web application that uses 2 different virtual directories. - The first is an ASP.NET application that has web services that can be used to authenticate various clients. - The second is...
5
by: Jason | last post by:
Hi, I am having some trouble returning a string from a c++ dll. I tend to get junk data back and I am not sure of the method, my code so far: Function is declared in c++ dll 'Test' and...
5
by: muriwai | last post by:
Hi, I have a C# assembly project under Visual Stuio 2008 Pro on Windows Server 2008. I converted the project from VS 2005. The project references COM->Microsoft CDO for Windows 2000 Library...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.