Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1781
  • Last Modified:

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

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
Finch
Asked:
Finch
2 Solutions
 
Carl TawnSystems and Integration DeveloperCommented:
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
 
AlexFMCommented:
See Marshal.PtrToStringAnsi Method. Show C++ function prototype for details.
0
 
FinchAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
AlexFMCommented:
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
 
K VDatabase ConsultantCommented:
try n use for Marshal class
marshal.ptrtostring function could come to rescue..
0
 
Daniel Van Der WerkenIndependent ConsultantCommented:
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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now