By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,242 Members | 1,116 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,242 IT Pros & Developers. It's quick & easy.

Question on converting char* into System.String

P: n/a
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
Nov 16 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
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

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #2

P: n/a
Yan
I am actually coding in C++, so no additinal compilation flags are
required.

*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Nov 16 '05 #3

P: n/a
I am actually coding in C++


And yet you're posting to a C# newsgroup.

Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 16 '05 #4

P: n/a
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

Mattias Sjögren <ma********************@mvps.org> wrote in message news:<uu**************@TK2MSFTNGP14.phx.gbl>...
I am actually coding in C++


And yet you're posting to a C# newsgroup.

Mattias

Nov 16 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.