c# 用委托实现函数指针调用api,提示无法转换
api函数
[DllImport("user32.dll", EntryPoint = "EnumWindows")]
public static extern int EnumWindows(int lpEnumFunc, int lParam );
vb代码
lRet = EnumWindows(AddressOf EnumWinProc, lParam);
c# 代码
public delegate Int32 EnumWinProHandler(int lhWnd, int lparm);
private Int32 EnumWinPro(Int32 lhWnd,Int32 lparm) {}
调用:
EnumWinProHandler e_enumwinpro = null;
e_enumwinpro = new EnumWinProHandler(EnumWinPro);
Debug.Assert(e_enumwinpro != null);
dwret = EnumWindows(e_enumwinpro, lparm);
编译时提示: 错误23 参数 1: 无法从“control_handle.Form1.EnumWinProHandler”转换为“int”
你们怎么看?
c#
委托
参数
函数指针
------解决方案--------------------句柄是一个指针类型的,你直接定义成int在32会上没问题,一碰到64位的问题就来了,应该是
public delegate bool WndEnumCallBack(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern bool EnumWindows(WndEnumCallBack WndEnumProc, IntPtr obj);