views:

660

answers:

2

I am working on wrapping a large number of .h and .lib files from native C++ to Managed C++ for eventual use as a referenced .dll in C#.

Some of the native C++ functions have a return type of void*. I am not sure how to handle this when I pass back the value to my calling code. For instance: if a C# app calls my dll wrapper, what do I return from the native call:

void* start(ThreadFunc,void *, unsigned *);

I am currently attempting to box the return in a generic System::Object^ with no luck. This is the call in the wrapper:

m_NativeThread->start(cb, 
  GCHandle::ToIntPtr(GCHandle::Alloc(o)).ToPointer(),
  static_cast<unsigned int*>(GCHandle::ToIntPtr(GCHandle::Alloc(u)).ToPointer())));

Can anyone offer a solution?

Thanks.

A: 

Can you make it an IntPtr? What do you expect the client to do with the void*?

Kim Gräsman
I wouldn't know what they would do with the void * honestly. I guess it would help if I knew why it is declared with a void* in the first plalce...What is the purpose of a void *? Why/When would I use it?
TomO
A void-pointer is a pointer to an unknown type, kind-of like a reference to System.Object in .NET, but without the associated runtime type info (that is, you can't interrogate the reference for its type). I phrased my question badly, I really meant: "What does the start() function expect that you use its return value for?" Because if it's never used, you can just discard it. If it's used as a handle for e.g. terminating the thread, I'd expose it as an IntPtr.
Kim Gräsman
+2  A: 

If your managed code needs to see the data in the void*:

You can't cast a void* to unmanaged memory to a managed object reference. To turn this into managed memory, you'd have to use Marshal.Copy() or Marshal.PtrToStructure(). That will of course only work if you know the type of the data that the void* points to. source

If your managed code doesn't need to see the data in the void*:

Store it in an IntPtr if your managed code doesn't need to see what it is and just passes it back into the unmanaged code later on. source

plastic chris