Passing 'this' to unmanaged code

G

Guest

I am trying to call an unmanaged dll from c#, the function is asyncronous and expects a callback function and a parameter which is labelled lpvoid but which expects the address of the calling object, which is then returned by the callback function. Using a delegate the call back function works perfectly and I can pass value objects as parameters, but a not reference object, is it possible and if so how do you do it.

PS I had hoped to get away with ignoring the parameter, as I expected the delegate to return to callback function of the object that called it. But when used from muliple objects simultaneously, all the callbacks are to the last object that calls the function. In other words, the unmanaged code always calls back on the last function given it.
 
G

Guest

Hi,

Unfortunately what you want to do is not valid, if I remember correctly, "this" is not an actual object variable and hence cannot be passed as reference. this is actually a keyword, which does not allow values being set to it.

This is probably not the answer you want. if you really want, you can just create another object variable, set it's reference to "this" and pass that by ref, but if you do get a new object instance returned, you cannot replace the context that "this" represents..


Cheers

Eddie de Bear
 
M

Mattias Sjögren

Using a delegate the call back function works perfectly and I can pass value objects as parameters, but a not reference object, is it possible and if so how do you do it.

You can use the GCHandle struct to get a handle to an object. The
handle can be passed to/from unmanaged code and turned back into an
object reference.



Mattias
 

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