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;
}
}

C#与C互操作的更多相关文章

  1. 没有为 COM 互操作注册程序集 请使用 regasm.exe /tlb 注册该程序集——解决办法

    错误现象: 错误 6 没有为 COM 互操作注册程序集“DevExpress.Utils.v13.1, Version=13.1.7.0, Culture=neutral, PublicKeyToke ...

  2. VS2013中, 无法嵌入互操作类型“……”,请改用适用的接口的解决方法

    使用VS2013,在引用COM组件的时候,出现了无法嵌入互操作类型“……”,请改用适用的接口的错误提示. 查阅资料,找到解决方案,记录如下: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作 ...

  3. VS2010中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法(转自网络)

    最近开始使用VS2010,在引用COM组件的时候,出现了无法嵌入互操作类型“……”,请改用适用的接口的错误提示.查阅资料,找到解决方案,记录如下: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌入 ...

  4. 与Java互操作

    课程内容涵盖了Java互操作性. Javap 类 异常 特质 单例对象 闭包和函数 变化性 Javap javap的是JDK附带的一个工具.不是JRE,这里是有区别的. javap反编译类定义,给你展 ...

  5. Microsoft.Office.Interop.Excel 程序集引用 ,Microsoft.Office.Interop.Excel.ApplicationClass 无法嵌入互操作类型

    using Microsoft.Office.Interop.Excel   添加程序集引用 方法:在引用--程序集--扩展中,添加引用Microsoft.Office.Interop.Excel,此 ...

  6. ArcEngine 无法嵌入互操作类型

    说明: 在.net 4.0中,声明 IPoint point = new PointClass();会出现下面这个错误 错误 2 类型"ESRI.ArcGIS.Geometry.PointC ...

  7. 错误 24 无法嵌入互操作类型“ESRI.ArcGIS.Geometry.PointClass”。请改用适用的接口。 E:\MyGIS\MyGIS\Form1.cs 78 37 MyGIS

    解决办法:选中那个引用,在属性页,将“嵌入互操作”设置为false

  8. 无法嵌入互操作类型“ESRI.ArcGIS.Carto.RectangleElementClass”。请改用适用的接口。

    右键点击应用的程序集 ESRI.ArcGIS.Controls,修改"嵌入互操作类型"的值即可

  9. 无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口

    解决 把Microsoft.Office.Interop.Excel.DLL的嵌入互操作类型改为ture就可以了

  10. ArcEngine10.1二次开发错误: 无法嵌入互操作类型,请改用适用的接口

    在之前配置ArcEngine.VS2010二次开发程序的时候,遇见"无法嵌入互操作类型,请改用适用的接口"的错误,在网上查了下,下面引用解决方法. 解决方式为在提示错误的引用上面右 ...

随机推荐

  1. 【学到了】golang的[]byte可以append string类型的数据

    上代码: func Test_use_string(t *testing.T){ arr := make([]byte,0, 100) arr = append(arr, "abcd&quo ...

  2. MySQL 知识点总结(简易版)

    MySQL 总结(简易版) 基本语法 0. 1基本语法 # 登录MySQL $ mysql -u root -p12345612 # 退出MySQL数据库服务器 exit; -- 显示所有数据库 sh ...

  3. go多版本管理

    在日常开发工作过程中,很多时候我们都需要在自己的机器上安装多个go版本,像是go1.16引入的embed,go1.18引入了泛型:又或是自己本地使用的是最新版,但公司的项目中使用的go1.14.go1 ...

  4. TienChin 渠道管理-前端展示渠道信息

    在编写 Vue 项目的时候我们可以使用 IDEA 当中提供的一个工具叫做 structure,也就是说可以很轻松的列举出当前 Vue 文件的大致结构,点那个就会跳转到对应的地方. 简简单单介绍一个编写 ...

  5. WinForm之ComboBox实现模糊查询

    ComboBox实现模糊查询 第一步:在Form_Load事件中绑定数据源 第二步:写一个方法,返回一个List<string>类型的集合来存储控件中的Items 第三步:在Form_Lo ...

  6. 逆天的全排列函数next_permutation()

    next_permutation 是算法库(<algorithm>)里的一个用于求全排列的函数,其定义为 next_permutation(_BidIt _First, _BidIt _L ...

  7. HWS山大专区PWN双一血 & CRYPTO-WP

    2023.11.18 两天半的比赛,就打了半天(因为要赶去打香山杯决赛了),不过结果还算好,人生第一次拿了两个一血hhh.写wp的时候人在中大南校北门的酒店里:) controller 格式化字符串泄 ...

  8. Power BI 15 DAY

    业务(表结构)数据分析 1.业务理解 准确 全面 2.数据收集 了解需要用到的数据有哪些 5W2H 结构化数据 SQL.通过查询获取数据库资源 多源表结构数据 企业数据库数据 文本文件数据 Excel ...

  9. Hooks与普通函数的区别

    Hooks与普通函数的区别 在这里的Hooks具体指的是自定义Hooks,自定义的Hooks与我们定义的普通函数类似,都可以封装逻辑,以实现逻辑的复用.Hooks实际上是一种特殊的函数,而由于Hook ...

  10. Oracle设置和删除不可用列

    Oracle设置和删除不可用列 1.不可用列是什么? 就是表中的1个或多个列被ALTER TABLE-SET UNUSED 语句设置为无法再被程序利用的列. 2.使用场景? If you are co ...