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 / June 2005

Tip: Looking for answers? Try searching our database.

Convert void pointer to System.Object

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tommy Vernieri - 17 Jun 2005 22:02 GMT
Hopefully someone can help me out with this; if what I'm asking is even
possible.

My basic question is how can I convert a void * into an Object and then
later convert it back again.

Here is a little more explanation in case there are other solutions I
could try. (I'm using .NET v1.1.)

I would like to call a managed method with an Object reference as one
of its parameters.  I am trying to make the call from managed C++.  The
managed C++ is called by a native C++ application which passes it a
const void *. (If it would help I could change this to a void *.)

I'm trying to do this because I want the native C++ code to create some
data, point to it somehow, and call the managed C++ passing in that
pointer.  Then the managed C++ does some work, and eventually calls
back to the native C++ passing it that same pointer.

The work that the managed C++ code does includes passing the pointer to
C#, so as far as I know that means it needs to be converted into
_something_ other than a void *.

I figured that a const void * was the right way to do this.  Are there
any other suggestions about how to get it done?

Is this an inherently unwise strategy?  I figure that if the native C++
is allocating the memory, it can pass the pointer to managed code and
then eventually get it back and as long as the native C++ didn't
deallocate the memory, the pointer will still be valid.

Thanks for any help!
Mattias Sjögren - 18 Jun 2005 00:21 GMT
Tommy,

>The work that the managed C++ code does includes passing the pointer to
>C#, so as far as I know that means it needs to be converted into
>_something_ other than a void *.

Not necessarily, you can pass a void* directly to C# if you don't mind
using unsafe code. If you prefer not to use unsafe code you can
consider casting it to an IntPtr. Or if the C# code don't need access
to the actual pointer value, wrap it in some other object.

Mattias

Signature

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

Tommy Vernieri - 18 Jun 2005 19:16 GMT
Thanks for the reply Mattias.

I had tried converting it to an IntPtr but for some reason I was
unsuccessful (now that I know it can be done I may look into the
details more).

Not a problem though, the idea of wraping it in a managed object does
exactly what I need.

Thanks!
- Tommy

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.