C#调用C/C++ DLL 参数传递和回调函数的总结
Int型传入:
Dll端:
|
extern "C" __declspec(dllexport) int Add(int a, int b) { return a+b; } |
C#端:
|
[DllImport("aeClient2.0.dll", CallingConvention =CallingConvention.Cdecl)] public static extern unsafe int Add(int a, int b); |
Int型传入传出:
Dll端:
|
extern "C" __declspec(dllexport) int Add(int *a, int* b) { *a = 2; *b = 3; return add(2, 3); } |
C#端
|
[DllImport("aeClient2.0.dll", CallingConvention =CallingConvention.Cdecl)] public static extern unsafe int Add(int a, int b); |
String传入:
Dll端:
|
extern "C" __declspec(dllexport) BOOL sendMessage(char* msg) { CString str = msg; ::AfxMessageBox(str.GetBuffer(0)); return 3; } |
C#端:
|
[DllImport("aeClient1.1.dll", CallingConvention =CallingConvention.Cdecl)] public static extern int sendMessage(string msg);
string strin = "llqq哈t哈t哈t呵?呵?"; sendMessage(strin); |
String型传入传出:
Dll端:
|
extern "C" __declspec(dllexport) BOOL GetErrorMessage(char *szErrorMessage ) { CString str = szErrorMessage; AfxMessageBox(str); char tmp[] = "nm世界和平nmnm"; strcpy(szErrorMessage,tmp); return 3; } |
C#端:
|
[DllImport("aeClient1.1.dll", CallingConvention =CallingConvention.Cdecl)] public static extern int GetErrorMessage(StringBuilder msg);
StringBuilder buf = new StringBuilder(1024);//指定的buf大小必须大于传入的字符长度 buf.Append("abc中国人"); int outdata = GetErrorMessage(buf, 1); string strout = buf.ToString(); |
结构体传入:
Dll端:
|
class NET_INFO_STRUCT { public: DWORD nDurationTime; //持?续?时º¡À间? double nReceiveByte; //接¨®收º?字Á?节¨² double nSendByte; //发¤¡é送¨ª字Á?节¨² WORD word; char buf[200]; FILETIME time; }; extern "C" __declspec(dllexport) BOOL NetGetConnectDetail(NET_INFO_STRUCT lpNetInfo) { ::AfxMessageBox(lpNetInfo.buf); return 3; }
|
C#端
|
public struct FILETIME { public uint high; public uint low; } public struct NET_INFO_STRUCT { public uint nDurationTime; //持?续?时º¡À间? public double nReceiveByte; //接¨®收º?字Á?节¨² public double nSendByte; //发¤¡é送¨ª字Á?节¨² public ushort ush; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = public string str; public FILETIME time; } ; [DllImport("aeClient1.1.dll", CallingConvention =CallingConvention.Cdecl)] public static extern unsafe int NetGetConnectDetail(NET_INFO_STRUCT lpNetInfo);
NET_INFO_STRUCT stru = new NET_INFO_STRUCT(); stru.nDurationTime = 8989; stru.nReceiveByte = 89.89; stru.nSendByte = 89.8900; stru.ush = 56; stru.str = "来来去去ypfisja"; stru.time.high = 24; stru.time.low = 17; NetGetConnectDetail(stru); |
结构体数组传出:
Dll端:
|
struct UIM_BOOK_STRUCT { int UimIndex; char szName[15]; char szPhone[21]; }; extern "C" __declspec(dllexport) int ReadUimAllBook(UIM_BOOK_STRUCT lpUimBookItem[],intnMaxArraySize) { lpUimBookItem[0].UimIndex = 345; strcpy(lpUimBookItem[0].szName , "dsd"); strcpy(lpUimBookItem[0].szPhone , "bbbb");
lpUimBookItem[1].UimIndex = 111; strcpy(lpUimBookItem[1].szName , "ddddd"); strcpy(lpUimBookItem[1].szPhone , "vvvvvv");
lpUimBookItem[2].UimIndex = 2222; strcpy(lpUimBookItem[2].szName , "d3343434sd"); strcpy(lpUimBookItem[2].szPhone , "bbbfggfggggfgb");
return 4; } |
C#端:
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct UIM_BOOK_STRUCT { public int UimIndex; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = public string szName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = public string szPhone; }; [DllImport("aeClient1.1.dll", CallingConvention =CallingConvention.Cdecl)] public static extern unsafe int ReadUimAllBook([Out] UIM_BOOK_STRUCT[] lpUimBookItem, intnMaxArraySize);
UIM_BOOK_STRUCT[] p = new UIM_BOOK_STRUCT[20]; int rets = ReadUimAllBook(p, p.Length); |
回调函数传递字符串:
Dll端:
|
typedef int (*pfCallBack)(int a, char b[]); pfCallBack CallBackfun = NULL;
extern "C" __declspec(dllexport) void SetCB(int (*pfCallBack)(int a, charb[])) { CallBackfun = pfCallBack; } char ch[100]; extern "C" __declspec(dllexport) void callTheCB() { int a = 4; memset(ch, '\0', 100); strcpy(ch, "aabbcc"); CallBackfun(a, ch); } |
C#端
|
//定¡§义°?一°?个?委¡¥托ªD,ê?其?返¤¦Ì回?类¤¨¤型¨ª和¨ª形?参?与®?方¤?法¤¡§体¬?的Ì?返¤¦Ì回?类¤¨¤型¨ª形?参?一°?致? [UnmanagedFunctionPointer(CallingConvention.Cdecl)]//一°?定¡§要°a加¨®上¦?这a句?,ê?要°a不?然¨?C#中D的Ì?回?调Ì¡Â函¡¥数ºy只?要°a被À?调Ì¡Â用®?一°?次ä?,ê?程¨¬序¨°就¨ª异°¨¬常¡ê退ª?出?了¢?!ê?!ê?!ê? public delegate int callBackHandler(int a, callBackHandler fun;//声¦¨´明¡Â一°?个?委¡¥托ªD变À?量¢? [DllImport("xxx.dll", CallingConvention =CallingConvention.Cdecl)] public static extern unsafe void SetCB(callBackHandler fun1); [DllImport("xxx.dll", CallingConvention =CallingConvention.Cdecl)] public static extern unsafe void callTheCB();
int localFun(int a, [MarshalAs(UnmanagedType.LPStr)]StringBuilder b) { MessageBox.Show(b.ToString()); return 0; }
fun = new callBackHandler(localFun);//给?委¡¥托ªD变À?量¢?赋3值¦Ì SetCB(fun);//用®?委¡¥托ªD当Ì¡À做Á?函¡¥数ºy指?针?作Á¡Â为a参?数ºy传ä?入¨? callTheCB(); |
回调函数传递结构体:
Dll端:
|
struct REvent { REvent(ONEVENTSTRUCT* pEVENTSTRUCT); WORD m_ChangeMask; WORD m_State; char m_Source[200]; };
typedef void (*pfCallBackEvent)(REvent eve/*, char pfCallBackEvent CallBackfunEvent = NULL; extern "C" __declspec(dllexport) void SetCBEvent(void (*pfCallBackEvent)(REvent eve/*, { CallBackfunEvent = pfCallBackEvent; } void callCBEvent(REvent eve/*, char m_Source[]*/) { CallBackfunEvent(eve/*, m_Source*/); }
//调用回调函数 callCBEvent(*pREvent/*, pEvent->m_Message.GetBuffer(0)*/); |
C#端:
|
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)] public struct REvent { public ushort m_ChangeMask; public ushort m_State; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = public string m_Source; };
[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]//可¨¦以°?指?定¡§编À¨¤码?类¤¨¤型¨ª public struct EventSourceTag { //[MarshalAs(UnmanagedType.ByValTStr, SizeConst = 200)] public IntPtr name; }; //定¡§义°?一°?个?委¡¥托ªD,ê?其?返¤¦Ì回?类¤¨¤型¨ª和¨ª形?参?与®?方¤?法¤¡§体¬?的Ì?返¤¦Ì回?类¤¨¤型¨ª形?参?一°?致? [UnmanagedFunctionPointer(CallingConvention.Cdecl, CharSet = CharSet.Ansi)]//一°?定¡§要°a加¨®上¦?这a句?,ê?要°a不?然¨?C#中D的Ì?回?调Ì¡Â函¡¥数ºy只?要°a被À?调Ì¡Â用®?一°?次ä?,ê?程¨¬序¨°就¨ª异°¨¬常¡ê退ª?出?了¢?!ê?!ê?!ê? public delegate void CBEventHandle(REvent eve/*, CBEventHandle cbFun;//声¦¨´明¡Â一°?个?委¡¥托ªD变À?量¢? [DllImport("xxx.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] public static extern unsafe void SetCBEvent(CBEventHandle fun);
void CBEvent(REvent eve/*, [MarshalAs(UnmanagedType.LPStr)]StringBuilder source*/) { //string str = Marshal.PtrToStringAnsi(source); //string str = System.Text.Encoding.Default.GetString(source); MessageBox.Show(eve.m_Message.ToString()); }
cbFun = new CBEventHandle(CBEvent); SetCBEvent(cbFun); |
C#调用C/C++ DLL 参数传递和回调函数的总结的更多相关文章
- vc调用BCB的dll 参数传递 报错
可能原因: 调用方式约定不一致. 函数调用约定如下: 1. __cdecl:C 和 C++ 程序的缺省调用规范. 2. __stdcall:标准调用约定(即WINAPI调用约定),也就是pascal调 ...
- Ajax前台调用后台方法、AJAX Pro2(回调函数)
//获取分店 function cityResult() { if (cityName != "") { $("#ddlcity_").find("o ...
- DLL与EXE之间的通讯调用 以及 回调函数的线程执行空间
dll 与 exe 之间的通讯方式有很多种, 本文采用回调函数的方法实现, 本文也将研究多线程,多模块的情况下,回调函数所在的线程, 啥也不说了,先附上代码: 下面的是dll模块的的, dll的工程文 ...
- C++回调函数(callback)的使用
什么是回调函数(callback) 模块A有一个函数foo,他向模块B传递foo的地址,然后在B里面发生某种事件(event)时,通过从A里面传递过来的foo的地址调用foo,通知A发生了什么事 ...
- [转]C++回调函数(callback)的使用
原文地址:http://blog.sina.com.cn/s/blog_6568e7880100p77y.html 什么是回调函数(callback) 模块A有一个函数foo,他向模块B传递fo ...
- 理解 JavaScript 回调函数并使用
JavaScript中,函数是一等(first-class)对象:也就是说,函数是 Object 类型并且可以像其他一等对象(String,Array,Number等)一样使用.它们可以"保 ...
- javascript 函数初探 (四)--- 回调函数
回调函数 既然函数与任何被赋值给变量的数据是相同的,那么她当然可以像其他数据那样被定义.删除.拷贝,以及当成参数传递给其它函数. 我们定义一个函数,这个函数有两个函数类型的参数,然后他会分别执行这两个 ...
- 理解和使用 JavaScript 中的回调函数
理解和使用 JavaScript 中的回调函数 标签: 回调函数指针js 2014-11-25 01:20 11506人阅读 评论(4) 收藏 举报 分类: JavaScript(4) 目录( ...
- 理解 JS 回调函数中的 this
任何变量或对象都有其赖以生存的上下文.如果简单地将对象理解为一段代码,那么对象处在不同的上下文,这段代码也会执行出不同的结果. 例如,我们定义一个函数 getUrl 和一个对象 pseudoWindo ...
随机推荐
- EChats+Ajax之柱状图的数据交互
原文链接:https://blog.csdn.net/qq_37936542/article/details/79723710 一:下载 echarts.min.js 选择完整版进行下载,精简版和常用 ...
- java并发api总结
开发十年,就只剩下这套架构体系了! >>> 1.java.util.concurrent包 1.1 Executors Executor:接口,仅有一个方法为execute(Ru ...
- JQuery中Ajax详细参数使用案例
JQuery中Ajax详细参数使用案例 参考文档:http://www.jb51.net/shouce/jquery1.82/ 参考文档:http://jquery.cuishifeng.cn/jQu ...
- [javase学习笔记]-6.6 基本数据类型參数与引用数据类型參数的传递过程
这一节基本数据类型參数和引用数据类型參数的传递过程. 数据类型參数和引用參数我们在前面章节中都已涉及到了,那么我们来看看以下的两段代码: //基本数据类型參数传递 class Demo { publi ...
- [Angular] Export directive functionalities by using 'exportAs'
Directive ables to change component behaives and lookings. Directive can also export some APIs which ...
- [_UICascadingTextStorage attributesAtIndex:effectiveRange:]: Range or index out of bounds
之前写过一篇<如何更好地限制一个UITextField的输入长度>,在文章最后得到的结论是可以直接使用 UIKIT_EXTERN NSString *const UITextFieldTe ...
- Role-based access control modeling and auditing system
A role-based access control (RBAC) modeling and auditing system is described that enables a user to ...
- KVO的使用(1)
1.在某个类中添加下面方法: -(void)viewWillAppear:(BOOL)animated{ [[NSNotificationCenter defaultCenter] addObserv ...
- 不要放弃使用border-box
不知道有多少老前端像我这样,在项目中很少使用box-sizing这个属性值.因为CSS2.1中只有content-box这一种盒子模式,在CSS3还没有流行的时候,大家在工作中大量基于这种盒子模式写C ...
- 【14.36%】【codeforces 614C】Peter and Snow Blower
time limit per test2 seconds memory limit per test256 megabytes inputstandard input outputstandard o ...