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二次开发程序的时候,遇见"无法嵌入互操作类型,请改用适用的接口"的错误,在网上查了下,下面引用解决方法. 解决方式为在提示错误的引用上面右 ...
随机推荐
- FM的一种简易解调方式
理论来源 <高低频电路设计与制作> 铃木雅臣 著.这位作者的书写的都不错 电路图 模拟思路 设输入信号为 \[s(t) = \sin(w_0 t + \Delta w\int_{- \in ...
- python实现zip分卷压缩与解压
1. python实现zip分卷压缩 WinHex 开始16进制一个一个文件对比 WinRar 创建的分卷压缩和单个 zip 文件的差异. 如果想把单个大文件 test.zip -> 分卷文件 ...
- ROS节点通信(一)消息发布和订阅
目录 1.说明 2.创建工作空间 3.创建功能包 4.编写自定义传输类型文件 5.编写源代码 5.1.编写发布者代码 5.2.编写订阅者代码 6.编译 7.启动运行 8.查看ROS网络结构图 1.说明 ...
- 还没发布就来了?iPhone 15系列机型价格配置图曝光
明天凌晨1点,iPhone 15系列就将正式发布,不过在发布前夕,有网友根据此前的各种爆料,制作了一张有关新iPhone的各型号价格和配置的图片,虽然这并不是官方公布的图片,但是也或多或少包含了人们对 ...
- Nexus系列:简介和安装(Windows、Linux)以及反向代理Nexus
目录 简介 安装 Windows Linux Nexus相关命令 Nginx反向代理Nexus 简介 Sonatype Nexus是一个Maven仓库管理器,可以节省网络带宽并加速项目搭建的进程.它可 ...
- Delphi - Case 可以多条件 指定在一起,今天才知道;逗号分隔
- HBase-Hbase启动异常java.lang.IllegalArgumentException: object is not an instance of declaring class
1.问题描述 HBase启动时异常如下: java.lang.IllegalArgumentException: object is not an instance of declaring clas ...
- [SpringBoot][Maven]关于maven pom文件的packaging属性
关于maven pom文件的packaging属性 前几天在调试源码运行程序的时候,因为将项目中pom文件的packaging属性用错导致源码包无法引入使用而报Bean注入错误,在此进行总结整理记录. ...
- SP21690 POWERUP - Power the Power Up 题解
题目传送门 前置知识 扩展欧拉定理 解法 直接对 \(a\) 和 \(b^c\) 分讨,跑一遍扩展欧拉定理就行了. 另外由于本题的特殊规定 \(0^0=1\),故需要在当 \(a=0\) 时,对 \( ...
- NC16696 [NOIP2001]统计单词个数
题目链接 题目 题目描述 给出一个长度不超过200的由小写英文字母组成的字母串(约定;该字串以每行20个字母的方式输入,且保证每行一定为20个).要求将此字母串分成k份( 1 < k ≤ 40 ...