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

pass a value from unmanaged code to managed code

P: n/a
Im very new to coding in C++ so use to coding in C#. My question is
how to pass some values (string) from unmanaged code to managed code. I
have two source files on a win32 console application one I used #pramga
managed to change to managed code but I need to get the values from the
unmanaged code and pass it to the managed code. Thanks...

Jun 16 '06 #1
Share this Question
Share on Google+
4 Replies


P: n/a
> Im very new to coding in C++ so use to coding in C#. My question is
how to pass some values (string) from unmanaged code to managed code. I
have two source files on a win32 console application one I used #pramga
managed to change to managed code but I need to get the values from the
unmanaged code and pass it to the managed code. Thanks...


Is the unmanaged string unicode?
if so, you can simply pass it to the System.String constructor.
otherwise you could use the PtrToStringAnsi method of the Marshal class
in the System.Runtime.InteropServices namespace.

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Jun 16 '06 #2

P: n/a
Hi Bruno!
Is the unmanaged string unicode?
if so, you can simply pass it to the System.String constructor.
You can also pass ANSI-Strings to the String constructor.
otherwise you could use the PtrToStringAnsi method of the Marshal class
in the System.Runtime.InteropServices namespace.


PtrToStringANsi is only helpfull in converting "String" into ANSI! Not
vice-versa!

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jun 16 '06 #3

P: n/a
Hello Jochen,
Is the unmanaged string unicode?
if so, you can simply pass it to the System.String constructor.

You can also pass ANSI-Strings to the String constructor.


didn't know that. never tried it.
otherwise you could use the PtrToStringAnsi method of the Marshal class
in the System.Runtime.InteropServices namespace.

PtrToStringANsi is only helpfull in converting "String" into ANSI! Not
vice-versa!


MSDN doesn't think so :-)

<quote>
Copies all characters up to the first null from an unmanaged ANSI string to
a managed String. Widens each ANSI character to Unicode.
[Visual Basic] Overloads Public Shared Function PtrToStringAnsi(IntPtr) As
String
[C#] public static string PtrToStringAnsi(IntPtr);
</quote>

--

Kind regards,
Bruno van Dooren
br**********************@hotmail.com
Remove only "_nos_pam"
Jun 16 '06 #4

P: n/a
Hi Bruno!
PtrToStringANsi is only helpfull in converting "String" into ANSI! Not
vice-versa!


MSDN doesn't think so :-)


Upps.. yes I forgot that there are two functions... sorry...

--
Greetings
Jochen

My blog about Win32 and .NET
http://blog.kalmbachnet.de/
Jun 17 '06 #5

This discussion thread is closed

Replies have been disabled for this discussion.