void pointers in Managed C++ for use in C#

  • Thread starter Thread starter Ryan
  • Start date Start date
R

Ryan

I'm writing a wrapper class in Managed C++ for some libraries that I need
to use in C#. The problem I'm having is that some of the functions that
are being wrapped take void*. Further, some take function pointers for
function definitions that use void* as a parameter. I don't know how to
deal with this...I'd like to be able to cast to an object or something
along those lines, but that doesn't seem possible.

How would one go about doing this?

Thanks.
 
I'm not sure I understand exactly what you want, but it sounds like
the GCHandle struct might help. You can allocate one of those for any
object, and then cast it to an IntPtr and void* (and back).



Mattias
 
I'm not sure I understand exactly what you want, but it sounds like
the GCHandle struct might help. You can allocate one of those for any
object, and then cast it to an IntPtr and void* (and back).

Here's an example of something that I'm trying to do, for which I've yet to
find a way to "do it right"; Microsoft's tutorials (that I've found) don't
really cover it. This is a short example of what I'm talking about...

---

public __delegate long HookFunc(long Type, long id, Object __gc* UserData);

private System::Collections::SortedList* delegates = new SortedList();

void HookFunction(long Type, HookFunc* Func, Object __gc* UserData)
{
// how to add Func to the SortedList... ?
delegates->Add(__box(Type), Func);

// When calling the api that is being used it requires a function
// pointer. I'd rather use delegates to keep things more C# friendly
// however I can't just cast it, as I'm trying to do here (compilation
// error)...
CppLibCall(Type, (FUNCPTR)(void*)Function, UserData);
}

Thanks,
Ryan
 
Back
Top