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;
}
}
C#与C互操作的更多相关文章
- 没有为 COM 互操作注册程序集 请使用 regasm.exe /tlb 注册该程序集——解决办法
错误现象: 错误 6 没有为 COM 互操作注册程序集“DevExpress.Utils.v13.1, Version=13.1.7.0, Culture=neutral, PublicKeyToke ...
- VS2013中, 无法嵌入互操作类型“……”,请改用适用的接口的解决方法
使用VS2013,在引用COM组件的时候,出现了无法嵌入互操作类型“……”,请改用适用的接口的错误提示. 查阅资料,找到解决方案,记录如下: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌入互操作 ...
- VS2010中,无法嵌入互操作类型“……”,请改用适用的接口的解决方法(转自网络)
最近开始使用VS2010,在引用COM组件的时候,出现了无法嵌入互操作类型“……”,请改用适用的接口的错误提示.查阅资料,找到解决方案,记录如下: 选中项目中引入的dll,鼠标右键,选择属性,把“嵌入 ...
- 与Java互操作
课程内容涵盖了Java互操作性. Javap 类 异常 特质 单例对象 闭包和函数 变化性 Javap javap的是JDK附带的一个工具.不是JRE,这里是有区别的. javap反编译类定义,给你展 ...
- Microsoft.Office.Interop.Excel 程序集引用 ,Microsoft.Office.Interop.Excel.ApplicationClass 无法嵌入互操作类型
using Microsoft.Office.Interop.Excel 添加程序集引用 方法:在引用--程序集--扩展中,添加引用Microsoft.Office.Interop.Excel,此 ...
- ArcEngine 无法嵌入互操作类型
说明: 在.net 4.0中,声明 IPoint point = new PointClass();会出现下面这个错误 错误 2 类型"ESRI.ArcGIS.Geometry.PointC ...
- 错误 24 无法嵌入互操作类型“ESRI.ArcGIS.Geometry.PointClass”。请改用适用的接口。 E:\MyGIS\MyGIS\Form1.cs 78 37 MyGIS
解决办法:选中那个引用,在属性页,将“嵌入互操作”设置为false
- 无法嵌入互操作类型“ESRI.ArcGIS.Carto.RectangleElementClass”。请改用适用的接口。
右键点击应用的程序集 ESRI.ArcGIS.Controls,修改"嵌入互操作类型"的值即可
- 无法嵌入互操作类型“Microsoft.Office.Interop.Excel.ApplicationClass”。请改用适用的接口
解决 把Microsoft.Office.Interop.Excel.DLL的嵌入互操作类型改为ture就可以了
- ArcEngine10.1二次开发错误: 无法嵌入互操作类型,请改用适用的接口
在之前配置ArcEngine.VS2010二次开发程序的时候,遇见"无法嵌入互操作类型,请改用适用的接口"的错误,在网上查了下,下面引用解决方法. 解决方式为在提示错误的引用上面右 ...
随机推荐
- Mysql 8.0前后,实现创建用户,指定用户只能访问指定的数据库中的表
最近在做公司项目的过程中,出现了这样的需求.第三方系统需要将数据写到我们的业务系统中,由于目前这些数据没有实际的使用场景,在讨论下,为简单快捷,选择第二种方案,即不书写接口,第三方系统通过数据库直接将 ...
- 你的代码已被埋在北极冰雪之下,保存千年——GitHub北极代码保险库
GitHub存档计划:北极代码保险库 在2019 GitHub 宇宙大会(GitHub Universe 2019)上,他们提到了一个问题,1000年后的软件会是什么样?人类会是什么样子?对此我们只能 ...
- 【二】Latex入门使用、常见指令
参考链接:https://blog.csdn.net/cocoonyang/article/details/78036326 \documentclass[12pt, a4paper]{article ...
- Laravel日期处理
1. 常用: echo Carbon::now(); // 2023-04-08 18:07:24 echo Carbon::today(); // 2023-04-08 00:00:00 echo ...
- Hello,World! 6.28
代码 public class Hello{ public static void main(String[] args){ System.out.print("Hello,World!&q ...
- 内存泄漏定位工具之 mtrace(一)
1 前言 mtrace(memory trace),是 GNU Glibc 自带的内存问题检测工具,它可以用来协助定位内存泄露问题.它的实现源码在glibc源码的malloc目录下,其基本设计原理为设 ...
- electron 安装不同的版本的方法
1.官网:http://www.electronjs.org/ 2.思考,既然是npm 安装,那么肯定也在 npm中央仓库有,那么去中央仓库看下: npm i -D electron@11.0.4
- MySQL中 int(11)和int(10)有没有区别!!
结论:int(11) int(3) int(20) 若不勾选填充0,那么效果统统一样,若勾选了填充0:查询出来的效果 会是这样: int(11) 00000000123 int(3) 123 ...
- Linux--split命令(将一个大文件拆分成多个小文件)
一.split命令简介 split是Linux系统中的一个文件拆分命令,它可以将一个大文件拆分成多个小文件.这对于处理大型文件,或者需要将数据分解到多个文件中的场景非常有用. 二.split命令的使用 ...
- 机器学习基础03DAY
特征降维 降维 PCA(Principal component analysis),主成分分析.特点是保存数据集中对方差影响最大的那些特征,PCA极其容易受到数据中特征范围影响,所以在运用PCA前一定 ...