Question on converting char* into System.String

  • Thread starter Thread starter Yan Vinogradov
  • Start date Start date
Y

Yan Vinogradov

There are two ways to convert a char* received from the unmanaged code
into a managed System.String object that I am aware of. The first one
is to simply construct String object passing char* as constructor
parameter. The second one is to use Marshal::PtrToStringAnsi (or
similar) method. Both seem to work just fine. Are there any catches
that I am missing?

Thanks,
Yan
 
Both seem to work just fine. Are there any catches
that I am missing?

The most notable difference is that the String constructor takes a
real pointer and therefore requires you to compile with /unsafe to use
it from C#. PtrToStringAnsi takes an IntPtr so it can be used in
"safe" code.




Mattias
 
I am actually coding in C++, so no additinal compilation flags are
required.
 
Very good point Mattias, my bad. The reason being that
dotnet.languages.vc group does not seem very popular. Besided, I was
under the impression that the question I was asking was dotnet
specific rather than language specific.

Yan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top