Solved

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

Posted on 2006-10-24
6
1,665 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
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

 
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

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Question has a verified solution.

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

Suggested Solutions

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

777 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