473,406 Members | 2,549 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,406 software developers and data experts.

Return string from c++ dll - interop services

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 exported like this:

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

I want use the function via interop services - p/invoke - to display the
string in a simple c# program:

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

public static string getBagString()
{
return Marshal.PtrToStringAuto(getbagstr());
}

The data I am returning is not correct. All display bag does is store and
return the string as below. I am returning int data correctly, but strings
are proving difficult. Anybody able to help?

void Bag::setdisplaybag(const string & str) {
displaybag = str;
}

string Bag::getdisplaybag() { return displaybag; }

Sep 3 '06 #1
5 9484
public static string getBagString()
{
return Marshal.PtrToStringAuto(getbagstr());
}
SysAllocStringByteLen returns a BSTR containing ANSI characters. So
PtrToStringAuto will only work on Win9x platforms. Try changing it to

IntPtr ptr = getbagstr();
string tmp = Marshal.PtrToStringAnsi(ptr);
Marshal.FreeBSTR(ptr);
return s;
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 3 '06 #2
On Sun, 03 Sep 2006 23:21:35 +0200, Mattias Sjögren wrote:
> public static string getBagString()
{
return Marshal.PtrToStringAuto(getbagstr());
}

SysAllocStringByteLen returns a BSTR containing ANSI characters. So
PtrToStringAuto will only work on Win9x platforms. Try changing it to

IntPtr ptr = getbagstr();
string tmp = Marshal.PtrToStringAnsi(ptr);
Marshal.FreeBSTR(ptr);
return s;
Thanks Mattias, this worked. I have been checking through the p/invoke
examples on your website which is an excellent resource. However, I
wondered if you could provide an example for this code:

extern "C" __declspec( dllexport ) __stdcall void setdisplaybag(LPSTR
tiles);
extern "C" __declspec( dllexport ) __stdcall void setdisplaybag(LPSTR
tiles) {
//set display bag code
}

Assuming the above is the exported c++ function, how should C# pass in the
BSTR safely? How should the BSTR be freed or should it?

[DllImport("Test")]
private static extern void setdisplaybag(IntPtr ptr);

public static void SetDisplayBag(string str)
{
IntPtr tmp = Marshal.StringToBSTR(str);
setdisplaybag(tmp);
//Marshal.FreeBSTR(tmp);
}
Sep 3 '06 #3
>Assuming the above is the exported c++ function, how should C# pass in the
>BSTR safely?
I see nothing about BSTRs in the function signatures, so I would
simply declare it as

static extern void setdisplaybag(string tiles);

Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 4 '06 #4
On Tue, 05 Sep 2006 00:05:07 +0200, Mattias Sjögren wrote:
I see nothing about BSTRs in the function signatures, so I would
simply declare it as

static extern void setdisplaybag(string tiles);
Ok fine, but when I input this i get strange output:

settiles("ZZZZZZZZZYYYYYYYYYYYYYY");
Console.WriteLine(getbagstr());

console output:

v►☺YYYYYYYYYYYYYZZZZZZZZZ

Where are the extra characters coming from at the beginning and what is
needed to avoid them? Thanks in advance.
Sep 5 '06 #5
Jason,
>Where are the extra characters coming from at the beginning and what is
needed to avoid them? Thanks in advance.
I don't know without seeing all the C++ code. It doesn't happen here.
I wrote the folowing test code and it roundtrips the string correctly.

// C++ DLL
#include <windows.h>
#pragma comment(lib, "oleaut32.lib")

char buf[100];

extern "C"
{
__declspec(dllexport) BSTR __stdcall getbagstr()
{
return SysAllocStringByteLen(buf, lstrlen(buf));
}

__declspec(dllexport) void __stdcall setdisplaybag(char *tiles)
{
lstrcpyn(buf, tiles, 100);
}

}

// C# client
using System;
using System.Runtime.InteropServices;

class Test
{
[DllImport("test.dll")]
static extern IntPtr getbagstr();

[DllImport("test.dll")]
static extern void setdisplaybag(string tiles);

static void Main()
{
setdisplaybag("ZZZZZZZZZYYYYYYYYYYYYYY");
IntPtr p = getbagstr();
Console.WriteLine(Marshal.PtrToStringAnsi(p));
Marshal.FreeBSTR(p);
}
}
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Sep 7 '06 #6

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

Similar topics

11
by: Tom Leylan | last post by:
(I posted this in languages.vb also... I can't figure out where things go if you use a little of a lot of things) Hi all... I'm looking for an example (or a pointer to one) related to the...
6
by: Bruce W.1 | last post by:
The intent of my web service is an RSS feed from a blog. Originally I used a StringBuilder to make the XML and returned a string from the webmethod. But this doesn't display properly in IE. So...
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:...
10
by: Mark Jerde | last post by:
I'm trying to learn the very basics of using an unmanaged C++ DLL from C#. This morning I thought I was getting somewhere, successfully getting back the correct answers to a C++ " int SumArray(int...
9
by: MSDNAndi | last post by:
Hi, I have a set of simple webservices calls that worked fine using .NET Framework 1.0. I am calling a Java/Apache based webservices, the calling side is not able to supply a proper WSDL. ...
1
by: louis_la_brocante | last post by:
Dear all, I am having trouble generating a client proxy for a webservice whose methods return a "complex" type. The type is complex in that it is a class whose members are a mix of primitive...
1
by: Bart Roozendaal | last post by:
Hi, I have setup a (my first) C# assembly. I am using the COM Interop services to use the assembly from a Delphi program. Is there a way to return a XmlDomDocument object from the assembly to...
1
by: v4u2chat | last post by:
Do I need to extend any of classes from AXIS to return multiple values? I'm exposing the following method as web service through AXIS to return multiple values. public ContactAddress...
1
by: fretIT | last post by:
Hello, when I write web method using C# in Visual basic 2005, I can't return the string value to the client request. I got such kind of error Not all code paths return a value. Don't know how...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.