以下是msdn中的例子! [C#] using System; using System.Runtime.InteropServices;
public delegate bool CallBack(int hwnd, int lParam);
public class EnumReportApp {
[DllImport("user32")] public static extern int EnumWindows(CallBack x, int y);
public static void Main() { CallBack myCallBack = new CallBack(EnumReportApp.Report); EnumWindows(myCallBack, 0); }
public static bool Report(int hwnd, int lParam) { Console.Write("Window handle is "); Console.WriteLine(hwnd); return true; } }
我在自己的程序中是这样调用的 //定义委托 public unsafe delegate void CallBack(int hwnd, void* Param); //引入Dll中的函数! [DllImport("JPSystemApi.dll",EntryPoint="JPRegisterStreamReadCallback")] public unsafe static extern int JPRegisterStreamReadCallback(CallBack pCallBack,void* Context); //实例化委托 CallBack F_myCallBack = new CallBack(JPRegister_StreamRead_Callback); //注册委托 int i = JPSystem._JPRegisterStreamReadCallback(F_myCallBack,n); GC.KeepAlive(F_myCallBack);//不回收 public unsafe static void JPRegister_StreamRead_Callback(int channel_number,void* point) { //this.richTextBox1.AppendText("start"+"\n"); MessageBox.Show("显卡不支持!","Error",MessageBoxButtons.OK,MessageBoxIcon.Error); } 完全按照msdn中的过程来得!可是回调函数还是没有运行!不知道是什么原因?大家能给我个答复吗?
|