C#与C互操作
C#给C++传递char**(转载)
1 2 3 4 5 6 | extern "C" _declspec( dllexport ) void GetResult( char * a, char ** pBuf) { sprintf ((pBuf[0]), "vc11111111" ); sprintf ((pBuf[1]), "vc222222222222222" ); } |
C#部分写法:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | // [DllImport( "CPPCharXingXing.dll" , EntryPoint = "GetResult" , CallingConvention = CallingConvention.Cdecl)] public static extern void GetResultCharXingXing( string a, IntPtr[] refSb); static void Main( string [] args) { //StringBuilder sb = new StringBuilder(); //bool b=GetResultCharXing(0,sb); //string str = sb.ToString(); //b= GetResultCharXing(1, sb); //str = sb.ToString(); StringBuilder refSb = new StringBuilder(); string a = "TomCat" ; string b = "aa" ; //不能为空字符串 string c = "cc" ; //不能为空字符串 IntPtr[] pts = new IntPtr[2]; pts[1] = Marshal.StringToHGlobalAnsi(c); pts[0] = Marshal.StringToHGlobalAnsi(b); GetResultCharXingXing(a,pts); string s = Marshal.PtrToStringAnsi(pts[0]); string ss = Marshal.PtrToStringAnsi(pts[1]); //MessageBox.Show($"S为{s}----ss为{ss}"); Marshal.FreeHGlobal(pts[0]); Marshal.FreeHGlobal(pts[1]); } |
C#给C传递char * 型
c传递const char * ,对于c# 就是byte *,因为c中char只占1位;
而 char 就是byte
const char * 也可以是string ,但是char *必须是 byte*,但最好变为byte* 型
因此,传入的buff全部为byte*型
C# 调用c可变参数的库函数sprintf,必须要注意的是:类型必须严格对应上,例如:format中,含有"%d",对应参数必须为int型
[DllImport("msvcrt.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] static unsafe extern int sprintf(byte* buff, byte* format, byte* arg1, byte* arg2); [DllImport("msvcrt.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] static unsafe extern int sprintf(byte* buff, string format, string arg0, int arg1, byte* arg2, double arg3, double arg4, double arg5); [DllImport("msvcrt.dll", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)] static unsafe extern int sprintf(byte* buff, string format, string arg0, byte* arg1); public static int Csprintf(char[] str, string format, string arg1, string arg2) { unsafe { byte* strr = stackalloc byte[str.Length]; for (int j = 0; j < str.Length; j++) { strr[j] = (byte)str[j]; } // byte* formatt = stackalloc byte[format.Length + 1]; for (int j = 0; j < format.Length; j++) { formatt[j] = (byte)format[j]; } formatt[format.Length] = (byte)'\0'; // byte* arg11 = stackalloc byte[arg1.Length + 1]; for (int j = 0; j < arg1.Length; j++) { arg11[j] = (byte)arg1[j]; } arg11[arg1.Length] = (byte)'\0'; // byte* arg22 = stackalloc byte[arg2.Length + 1]; for (int j = 0; j < arg2.Length; j++) { arg22[j] = (byte)arg2[j]; } arg22[arg2.Length] = (byte)'\0';//记得要添加字符串结束标志 // int t = sprintf(strr, formatt, arg11, arg22); // for (int j = 0; j < str.Length; j++) { str[j] = (char)strr[j]; } return t; } } public static int sprintf(byte[] buffer, string format, string arg0, char[] arg1) { unsafe { byte* strr = stackalloc byte[buffer.Length]; // byte* arg01 = stackalloc byte[arg1.Length + 1]; for (int j = 0; j < arg1.Length; j++) { arg01[j] = (byte)arg1[j]; } arg01[arg1.Length] = (byte)'\0'; // int t = sprintf(strr, format, arg0, arg01); // for (int j = 0; j < buffer.Length; j++) { buffer[j] = strr[j]; } return t; } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2019-03-04 测绘线性代数(三):伪逆解