pass a value from unmanaged code to managed code

K

kimberly.walker

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...
 
B

Bruno van Dooren

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
(e-mail address removed)
Remove only "_nos_pam"
 
J

Jochen Kalmbach [MVP]

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/
 
B

Bruno van Dooren

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

didn't know that. never tried it.
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
(e-mail address removed)
Remove only "_nos_pam"
 

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

Top