调用规则 _stdcall
回调函数参数转换,C#中回调函数以委托的形式。
C++数据类型 | C#数据类型 |
void | void |
int | int |
char * 作为形参 | System.string |
const char * 作为返回值 | IntPtr IntPtr slotName = ""; System.String strSlotName = Marshal.PtrToStringAnsi(slotName); |
Invoke与BeginInvoke的区别,类似于SendMessage与PostMessage的区别,在调试过程中,遇到了关闭套接字界面卡死的现象,因为关闭套接字的时候刷新界面,阻塞了,所以界面卡死!
typedef void (_stdcall * jmapi_typedef_callback_func)(char * psz);
extern "C" __declspec(dllexport) void _stdcall jmapi_set_callback_func(jmapi_typedef_callback_func pFunc);
extern "C" __declspec(dllimport) int _stdcall jmapi_init(int nDeviceType);
extern "C" __declspec(dllimport) int _stdcall jmapi_find_device();
extern "C" __declspec(dllimport) int _stdcall jmapi_exit();
public delegate void delCallbackFunc(System.String s);
public delegate void delRefreshUI(Control c, object o);
[DllImport(@"D:\CFDAS\Debug\JMAPI.dll")]
private static extern int jmapi_init(int nDeviceType);
[DllImport(@"D:\CFDAS\Debug\JMAPI.dll")]
private static extern int jmapi_exit();
[DllImport(@"D:\CFDAS\Debug\JMAPI.dll")]
private static extern int jmapi_find_device();
[DllImport(@"D:\CFDAS\Debug\JMAPI.dll")]
private static extern void jmapi_set_callback_func(delCallbackFunc pFunc);
private delCallbackFunc callbackFunc = null;
callbackFunc = new delCallbackFunc(CSharpCallbackFunc);
jmapi_set_callback_func(callbackFunc);
public void CSharpCallbackFunc(System.String s)
{
this.BeginInvoke(new delRefreshUI(RefreshUI), new object[] { this.listBox1, s });
}
public void RefreshUI(Control c, Object o)
{
((ListBox)c).Items.Add(o.ToString());
}