2

I have a C++ library that I use in my C# application. One of the methods in the C++ library returns a char *. How can I convert that char * into a string in my C# application?

Apparently in my C# application, the data type returned in this method is of type sbyte *.

5
  • 1
    P/Invoke or...? (Please tag with the approach used for access.) Commented May 28, 2012 at 23:02
  • 1
    Marshal it as StringBuilder? Commented May 28, 2012 at 23:02
  • @pst i am not sure since i am very new to C++ programming.. i have methods in the sources section and the methods that will be used by the c# application in the header section. In the header section there is the method signatures.. Commented May 28, 2012 at 23:04
  • @cicada where will i use that? in the C++ part or the C#? Commented May 28, 2012 at 23:05
  • Reference: msdn.microsoft.com/en-us/library/k9s9t975.aspx Commented May 28, 2012 at 23:05

1 Answer 1

4

String has a constructor that takes a char*:

char* x = ...
string s = new string(x);

And another one that takes a sbyte*:

sbyte* x = ...
string s = new string(x);
Sign up to request clarification or add additional context in comments.

2 Comments

the following error is being generated when I assign sbyte * x to the C++ method that returns the char * : Error 1 Pointers and fixed size buffers may only be used in an unsafe context.. Any ideas? thanks!
found what the problem was... i had to write unsafe in the method signature... :) thanks a lot !:)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.