Solved

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

Posted on 2006-10-24
6
1,692 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

733 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