Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / Interop / January 2007

Tip: Looking for answers? Try searching our database.

casting void * or IntPtr to managemed object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
James Whetstone - 28 Jan 2007 01:20 GMT
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
Mattias Sjögren - 28 Jan 2007 09:58 GMT
James,

>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#?

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

Mattias

Signature

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

James Whetstone - 28 Jan 2007 17:52 GMT
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

> James,
>
[quoted text clipped - 6 lines]
>
> Mattias
Mattias Sjögren - 28 Jan 2007 20:52 GMT
James,

>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?

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

Mattias

Signature

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

James Whetstone - 28 Jan 2007 21:09 GMT
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

> James,
>
[quoted text clipped - 6 lines]
>
> Mattias
Mattias Sjögren - 29 Jan 2007 19:18 GMT
James,

>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?

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

Mattias

Signature

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


Rate this thread:







Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.