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

Easy C# String problem....

Hi Guys,

Im trying to pass a string from a C++ DLL back into a C# application.

In the C++ DLL the string is a char array,

static char
myArray [2048];

extern "C" __declspec(dllexport) char *getMyString (void)
{
sprintf (myArray, "the string I want");

return myArray;
}

In the C# Application I have this :-

[DllImport("myDll.Dll", CharSet=CharSet.Auto)] public static extern char
[] getMyString();
This all seems to work ok but I don't know how to get this char [] into a C#
string...

Any help appreciated.

Thanks ,

Todd.
Nov 17 '05 #1
1 1105
This all seems to work ok but I don't know how to get this char [] into a C#
string...


Well if it was that easy you would use the String constructor that
takes a char[]. But in fact you have to declare it as

[DllImport("myDll.Dll", CharSet=CharSet.Auto)] public static extern
IntPtr getMyString();

and then use Marshal.PtrToStringAnsi() to retrieve the string from the
returned pointer.
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2

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

Similar topics

8
by: max | last post by:
Hi, I've tried the std::getline and std::string. It seams to work but I get some tourbles with it. WIth the folowing code: if(ne_menu=='4') { cout << "Nom du Client/Projet:";...
2
by: andy.dreistadt | last post by:
Hi all, I came across another problem that is probably pretty easy but, again, due to my rusty-ness with C, I'm a little stumped. I have a struct that looks like this: /* Instrument Data...
3
by: Eifel-benz | last post by:
I'm working the first days with Visual C#.net. So most of you think these questions are really easy. 1. How can I read out the windows-account? 2. How can I read datas from an INI-file? A...
6
by: Ryan Smith | last post by:
I am trying to store a string into a string variable via the following code however am receiving an error and cant figure out what i am doing wrong. Any feedback is greatly appreciated. Line...
1
by: Mad Scientist Jr | last post by:
can someone explain how to simply populate a grid in .net ? the way i understand it, there is no more msflexgrid, and instead is this new control that has to be tied to a dataset, and it is a real...
3
by: kathy | last post by:
I want to know what is the easy way to check if a string is a number or not? the number can be int, float, double, scientific,... what is the easy way for only interger?
4
by: Rick Stevens | last post by:
I am not an access expert, could anyone tell me if the following would be easy to do?? I receive emails from a specific email address, that advise me if a specific piece of equipment my company...
1
by: samadams_2006 | last post by:
Hello, I haven't done much programming in .asp and was wondering if someone could provide me with some sample code to do the following task... I have a Web Service called: ...
20
by: raylopez99 | last post by:
Inspired by Chapter 8 of Albahari's excellent C#3.0 in a Nutshell (this book is amazing, you must get it if you have to buy but one C# book) as well as Appendix A of Jon Skeet's book, I am going...
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
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
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
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.