The opinions expressed herein are my own personal opinions. They are not necessarily fact or sactioned by any other person or organization. If you disagree that's your right. It's also my right to not care.
© Copyright 2010, Chris Tacke
In one of the hands-on labs here at MEDC a developer asked how to pass a managed funtion to a native DLL as a callback function. I promised him a sample, so here's a simple one that shows how to call EnumWindows:
public delegate int EnumWindowsProc(IntPtr hwnd, IntPtr lParam);public partial class Form1 : Form{ EnumWindowsProc callbackDelegate; IntPtr callbackDelegatePointer; [DllImport("coredll.dll", SetLastError = true)] public static extern bool EnumWindows(IntPtr lpEnumFunc, uint lParam); public Form1() { InitializeComponent(); callbackDelegate = new EnumWindowsProc(EnumWindowsCallbackProc); callbackDelegatePointer = Marshal.GetFunctionPointerForDelegate(callbackDelegate); EnumWindows(callbackDelegatePointer, 0); } public int EnumWindowsCallbackProc(IntPtr hwnd, IntPtr lParam) { System.Diagnostics.Debug.WriteLine("Window: " + hwnd.ToString()); return 1; }}
Remember Me