all groups > dotnet interop > january 2007 >
You're in the

dotnet interop

group:

casting void * or IntPtr to managemed object


casting void * or IntPtr to managemed object James Whetstone
1/27/2007 5:20:33 PM
dotnet interop:
Hello,

I've got a DLL function that return a void * pointer very similar to
malloc(), and I need to cast it to a managed type. For this example, I'd be
happy to cast it to a string type. Can anyone shed some light on how to do
this in C#?

Thanks!
JW

Re: casting void * or IntPtr to managemed object James Whetstone
1/28/2007 9:52:06 AM
Okay. Yeah, I looked at this method. From the documentation, I understood
it to mean it would *copy* the unmanaged memory to a the managed object, not
*create* the managed object from unmanaged memory. Is this not correct?
Put another way, if I use this method call and create a managed string
object, and then I append some characters to the string, does it actually
use/change the unmanaged memory?

Best,
JW

[quoted text, click to view]

Re: casting void * or IntPtr to managemed object Mattias Sjögren
1/28/2007 10:58:37 AM
James,

[quoted text, click to view]

call Marshal.PtrToString[Ansi/Uni/Auto]()



Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: casting void * or IntPtr to managemed object James Whetstone
1/28/2007 1:09:32 PM
That's fine. So just to clarify then, C# doesn't support type casting (or
otherwise marshalling) unmanaged memory to a managed type in any way, shape
or form, right?

Thanks for you help,
JW

[quoted text, click to view]

Re: casting void * or IntPtr to managemed object Mattias Sjögren
1/28/2007 9:52:20 PM
James,

[quoted text, click to view]

No. If that's what you want, you'll have to copy the changes back to
the original buffer yourself.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Re: casting void * or IntPtr to managemed object Mattias Sjögren
1/29/2007 8:18:21 PM
James,

[quoted text, click to view]

Correct. Managed objects must be allocated on a GC heap.


Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
AddThis Social Bookmark Button