欢迎来到天天文库
浏览记录
ID:61967784
大小:16.30 KB
页数:5页
时间:2021-04-06
《CSharp如何调用dll中带指针参数.docx》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、C#调用C++DLL的方法,不同参数类型的调用方法1.参数为基本类型,例如int,float,char等[C++]voidfun(intvalue);voidfun(floatvaue);voidfun(charch);[C#][DllImport("xxx.dll")]publicstaticexternvoidfun(Int32value);[DllImport("xxx.dll")]publicstaticexternvoidfun(floatvalue);[DllImport("xxx.dll")]publicstaticexternvoid
2、fun(charch);2.参数为基本类型+指针,例如int*,float*,char*等[C++]voidfun(int*value);voidfun(float*vaue);voidfun(char*ch);[C#][DllImport("xxx.dll")]publicstaticexternvoidfun(refInt32value);[DllImport("xxx.dll")]publicstaticexternvoidfun(reffloatvalue);参数为char*,在C#中有几种实现方式A.publicstaticexternvo
3、idfun(stringch);//ch内容不会改变B.publicstaticexternvoidfun(StringBuilderch);//ch内容会改变3.参数为结构体[C++]structpoint{intvalue;//基本类型charch;//基本类型intnumber[100];//数组charbuffer[100];//字符串数组};voidfun(pointpt);[C#][StructLayout(LayoutKind.Sequential)]publicstructpoint{publicInt32value;publicch
4、arch;[MarshalAs(UnmanagedType.ByValArray,SizeConst=100)]publicInt32[]number;[MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]publicchar[]buffer;}[DllImport("xxx.dll")]publicstaticexternvoidfun(pointpt);4.参数为结构体指针[C++]voidfun(point*pt);[C#][DllImport("xxx.dll")]publicstaticexte
5、rnvoidfun(refpointpt);5.参数为结构体,并且结构体还嵌套结构体[C++]structpoint{intvalue;//基本类型charch;//基本类型intnumber[100];//数组charbuffer[100];//字符串数组structpointpt;//嵌套结构体};voidfun(pointpt);[C#][StructLayout(LayoutKind.Sequential)]publicstructpoint{publicInt32value;publiccharch;[MarshalAs(Unmanaged
6、Type.ByValArray,SizeConst=100)]publicInt32[]number;[MarshalAs(UnmanagedType.ByValTStr,SizeConst=100)]publicchar[]buffer;publicpointpt;}[DllImport("xxx.dll")]publicstaticexternvoidfun(pointpt);6.参数为结构体,并且结构体还嵌套结构体指针或者双指针[C++]structpoint{intvalue;//基本类型charch;//基本类型intnumber[100]
7、;//数组charbuffer[100];//字符串数组structpoint*p1;//嵌套结构体指针structpoint**p2;//嵌套结构体双指针};voidfun(pointpt);[C#][StructLayout(LayoutKind.Sequential)]publicstructpoint{publicInt32value;publiccharch;[MarshalAs(UnmanagedType.ByValArray,SizeConst=100)]publicInt32[]number;[MarshalAs(UnmanagedT
8、ype.ByValTStr,SizeConst=100)]publicchar[]buffer;public
此文档下载收益归作者所有