Solved

How to convert a C++ "char*" to a C# "String"?

Posted on 2006-10-24
6
1,676 Views
Last Modified: 2008-01-09
I have a DLL written in C++ that transfers a character array (char*) and I need to copy this character array into a C# String. I use Visual Studio 2005 and C#.
0
Comment
Question by:Finch
6 Comments
 
LVL 52

Assisted Solution

by:Carl Tawn
Carl Tawn earned 200 total points
ID: 17797688
The constructor for the string type accepts a char* as a parameter so you should just be able to use something like:

    string myString = new string("your char*");
0
 
LVL 48

Accepted Solution

by:
AlexFM earned 300 total points
ID: 17798356
See Marshal.PtrToStringAnsi Method. Show C++ function prototype for details.
0
 

Author Comment

by:Finch
ID: 17799395
Both answers given work similarly, and the string is now available at the C# level. But now I am struggling with a runtime error when the return to the DLL is executed. So this has probably turned into a "Marshalling" question, I've bumped the reward points for this question accordingly.  I get the following error message as I return from the C# to the C++ DLL:

      Runtime Check Failure #0: The value of ESP was not properly saved across a function call.....

My function call prototype on the C++ DLL side is:
    typedef void(*MyCallbackFunction)(char*, int);

My Callback function on the C# side looks like this:
    public void MyCallbackFunction(IntPtr Message, int ErrorLevel);
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 48

Expert Comment

by:AlexFM
ID: 17801395
So, what exactly C++ function does, and what is your C# code? Working with interoperability, it is important to know what is parameters type (in, out), what function allocates memory, what function is responsible to release memory etc. Give enough information.
0
 
LVL 21

Expert Comment

by:theGhost_k8
ID: 17801543
try n use for Marshal class
marshal.ptrtostring function could come to rescue..
0
 
LVL 20

Expert Comment

by:Daniel Van Der Werken
ID: 17803658
Are you using Platform Invoke?  Sounds like you're trying to use an unManaged DLL without using P/Invoke.  

http://msdn2.microsoft.com/en-us/library/fzhhdwae.aspx
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question