C#与C互操作
C#给C++传递char**(转载)
extern "C" _declspec(dllexport)void GetResult(char* a,char** pBuf) { sprintf((pBuf[0]),"vc11111111"); sprintf((pBuf[1]),"vc222222222222222"); }
C#部分写法:
// [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; } }