对于参数为函数指针的API,C#如何调用?

hahahawk 2003-03-17 01:04:52
调用API,其他的参数都很容易解决,但是函数指针却让我糊涂。
我尝试过用delegate,并且delegate的原理也很象回调函数。
但是执行的时候总是报错,又无法调试。
比如:象RegisterClass这样的api函数,如何调用?请高手不吝指教!
...全文
31 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
hahahawk 2003-03-18
  • 打赏
  • 举报
回复
谢谢你对我的RegisterClass函数问题的解答。不过,还是没有完全解决。你给出的代码WNDCLASS中WndProc类型,我想是delegate吧?我这样使用了,
public delegate int WndProcDelegate(IntPtr hwnd,uint message,int wParam,int lParam);
...
WndProcDelegate d = new WndProcDelegate(WndProc);
wndclass.lpfnWndProc = d;

但是RegisterClass后调用API函数GetLastError返回127,意思是无法找到Wnd_Proc_Not_Fount.
另外我执行GetMessage时候会引发System.ExecutionEngineException异常,并且用catch语句都无法捕捉,程序仍然会终止。诚心请教!
DarthVader 2003-03-18
  • 打赏
  • 举报
回复
up一个
qqchen79 2003-03-18
  • 打赏
  • 举报
回复

public delegate int WndProc(IntPtr hwnd, int msg, int wparam, int lparam);

[StructLayout(LayoutKind.Sequential)]
public struct WNDCLASS {
public int style;

[MarshalAs(UnmanagedType.FunctionPtr)]
public WndProc lpfnWndProc;

public int cbClsExtra;
public int cbWndExtra;
public IntPtr hInstance;
public IntPtr hIcon;
public IntPtr hCursor;
public IntPtr hbrBackground;

[MarshalAs(UnmanagedType.LPStr)]
public string lpszMenuName;
[MarshalAs(UnmanagedType.LPStr)]
public string lpszClassName;
}

[DllImport("user32.dll", CharSet=CharSet.Ansi)]
public extern static short RegisterClass(ref WNDCLASS cls);

110,555

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧