如何使用回调功能 [英] How to work with Callback function
问题描述
亲爱的所有人,
我需要调用dll(在c/c ++中生成)中定义的回调函数到c#.net/vb.net应用程序.我希望代码和方法能够做到这一点.
我真的不知道如何使用回调函数.
我在net中提到,发现为此需要使用委托.
怎么做 ?有人可以详细向我解释一下吗?
以下是定义
Dear all,
I need to invoke the callback function defined in the dll(build in c/c++) to the c#.net/vb.net application.I want the code and methodology to do it.
I really don''t know how to do it with the callback functions.
I referred in net and found that it is need to use delegates for this purpose.
How to do it ? can any one explain me in detail about this ?
The following is the definition
/* Callback function type */
typedef BOOL (CALLBACK* FUNCSCANBACK)(HGLOBAL hBuf, UINT unLines);
/*Function call*/
WORD WINAPI readdevice(WORD Lines,FUNCSCANBACK funcScanBack)
funcScanBack - callback function address. Reference to "FUNCSCANBACK" definition:
hBuf - Image buffer handle for callback.
unLines - Image lines in the buffer.
返回TRUE表示继续读取作业,FALSE表示取消读取作业.
Return TRUE means continue read job, FALSE means cancel read job.
推荐答案
public delegate UNKNWON FUNCSCANBACK(IntPtr hBuf,UInt32 unLines);
public class yourClass{
[DllImport("yourdll")]
public static extern UInt16 readdevice(UInt16 Lines, FUNCSCANBACK y);
void invokeFunc(){
readdevice(0,new FUNCSCANBACK(myCallBackFunc));
}
UNKNOWN myCallBackFunc(IntPtr hBuf,UInt32 unLines){
}
}
我使用UNKNOWN作为返回类型,因为我不知道CALLBACK *?
I used UNKNOWN as return type since I have no idea about CALLBACK*?
这篇关于如何使用回调功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!