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