shoehorn a managed object into an LPVOID

G

Guest

hello
I've got a C DLL that I've built which has got a callback function. Now I
want to call it from C#.

The method of using a delegate for the callback function works absolutely
beautifully.
However, the exported function of the Dll (that I PInvoke from c#) has an
LPVOID parameter for extradata, which it passes to the callback function.
How would I go about coding it if I wanted to pass a reference to a C# class
through this LPVOID? Do I need a HandleRef, and IntPtr, unsafe code?

Please enlighten me!

Thanks
 
M

Mattias Sjögren

However, the exported function of the Dll (that I PInvoke from c#) has an
LPVOID parameter for extradata, which it passes to the callback function.
How would I go about coding it if I wanted to pass a reference to a C# class
through this LPVOID? Do I need a HandleRef, and IntPtr, unsafe code?

Do you really have to use it? Since a delegate can refer to an
instance method, you canusually just store whatever state you need for
the callback as instance fields in the same class as the callback
method.

If you still want to use it, check out the GCHandle struct.



Mattias
 
B

Bonj

Do you really have to use it? Since a delegate can refer to an
instance method, you canusually just store whatever state you need for
the callback as instance fields in the same class as the callback
method.

Ah - that's an option I suppose...
If you still want to use it, check out the GCHandle struct.

I have done. THe only question is, the GC won't "sweep it away" while the
unmanaged code is running will it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top