pinvoke with embedded function pointer

E

edgekaos

I want to pass a structure to a unmanaged dll function. Inside this
structure there is a pointer to a callback function. Unmanaged code
will call this managed function as a callback function. I know how to
pass the structure and define a delegate for the callback but I don't
know how to define the marshelling for the callback pointer.
 
W

Willy Denoyette [MVP]

edgekaos said:
I want to pass a structure to a unmanaged dll function. Inside this
structure there is a pointer to a callback function. Unmanaged code
will call this managed function as a callback function. I know how to
pass the structure and define a delegate for the callback but I don't
know how to define the marshelling for the callback pointer.

Pass a delegate's function pointer like this:

public delegate void MyDelegate(...);

struct MyStruct {
..
..
[MarshalAs(UnmanagedType.FunctionPtr)]
public Delegate func;
}

MyStruct mStr = new MyStruct ();
mStr.func = new MyDelegate(...);
...

Willy.
 

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