Calling function pointer in C#

G

Guest

I've got a (managed) C# class that's called on an interface from a (unmanaged) COM DLL. The method passes a function pointer that I need to call from C# and pass back a couple of parameters. I can find lots of examples about how to go the other direction, that is, provide a C# callback method via delegate to an unmanaged DLL function so the C# gets called back. However, I have not been able to find out how to call a function pointer from C#. Help

Specifically, I'm trying to add property pages to the shell (Explorer) property sheet. My C# code method below is called via an interface from the COM object ..
int IShellPropSheetExt.AddPages (IntPtr /*LPFNADDPROPSHEETPAGE*/ lpfnAddPage, uint /*LPARAM*/ lParam
I need to call back the function "lpfnAddPage" and pass it a couple of parameters and can't figure out how to do that.
 
S

SleazySt

<CODE>
public delegate bool LPFNADDPROPSHEETPAGE(IntPtr hPropSheetPage, int lParam);

[ComVisible(true)]
public IntPtr AddPages(LPFNADDPROPSHEETPAGE lpfnAddPage, int lParam){
//blah blah
IntPtr hPropSheetPage = //blah blah
lpfnAddPage(hPropSheetPage, lParam);
//blah blah
return IntPtr.Zero;
}
</CODE>
AJohnson said:
I've got a (managed) C# class that's called on an interface from a (unmanaged) COM DLL. The method
passes a function pointer that I need to call from C# and pass back a couple of parameters. I can
find lots of examples about how to go the other direction, that is, provide a C# callback method via
delegate to an unmanaged DLL function so the C# gets called back. However, I have not been able to
find out how to call a function pointer from C#. Help!
Specifically, I'm trying to add property pages to the shell (Explorer) property sheet. My C# code
method below is called via an interface from the COM object ...
int IShellPropSheetExt.AddPages (IntPtr /*LPFNADDPROPSHEETPAGE*/ lpfnAddPage, uint /*LPARAM*/ lParam)
I need to call back the function "lpfnAddPage" and pass it a couple of parameters and can't figure
out how to do that.
 
G

Guest

Thanks, but this is the first thing I tried. It compiles but the CLR throws an ExecutionEngineException and IExplorer terminates when I run it. Although I can't tell exactly where it is happening, I suspect it is in the marshalling of parameters for the call to AddPages because it never makes it into that routine. If I make the parameter an IntPtr, then I don't get the ExecutionEngineException and it enters the AddPages routine. But then I can't (or don't know how to) call the callback function!
 
M

Mattias Sjögren

Currently there's no good way to do this. The easiest solution is to
write some C++ code that calls the function. Another way is to use
Reflection Emit to generate code that uses the calli IL instruction.
This is demonstrated here http://pfct4net.sourceforge.net

In Whidbey there will be an easier solution, a way to get a delegate
wrapped around a native function pointer.



Mattias
 
S

SleazySt

Hmmm... That's distressing. I guess that's where somebody got tired of implementing.

Well, as Mattias Sjögren suggested, you can call the calli IL instruction, or as Nicholas Paldino
suggested in this article:

tiny link: http://tinyurl.com/326r4
big link:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&safe=off&selm=#eOKg$N1BHA.1560@tkmsftngp07

you can use the DefinePInvokeMethod function of the TypeBuilder class.

Sorry my suggestion was a bust.


AJohnson said:
Thanks, but this is the first thing I tried. It compiles but the CLR throws an
ExecutionEngineException and IExplorer terminates when I run it. Although I can't tell exactly where
it is happening, I suspect it is in the marshalling of parameters for the call to AddPages because
it never makes it into that routine. If I make the parameter an IntPtr, then I don't get the
ExecutionEngineException and it enters the AddPages routine. But then I can't (or don't know how to)
call the callback function!
 

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