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
 
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
 
Back
Top