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二次开发程序的时候,遇见"无法嵌入互操作类型,请改用适用的接口"的错误,在网上查了下,下面引用解决方法. 解决方式为在提示错误的引用上面右 ...
随机推荐
- ASP.NET MVC 通过ActionFilter获取请求的参数
using System; using System.Collections.Generic; using System.Linq; using System.Net.Http; using Syst ...
- vim 从嫌弃到依赖(2)——vim 模式
在上一篇文章中我们获取到了neovim 并对它进行了基础配置.现在已经具备一般编辑器的基本功能了.让我们先学会如何使用vim基本功能进行编辑,后面再看如何进行配置,以达到某某IDE或者编辑器的效果 v ...
- 微信小程序-页面跳转数据传递
在之前的文章当中我们都实现了一个功能就是可以从上一个页面传递数据给下一个页面,那么我们能不能从下一个页面传递数据给上一个页面呢,答案是可以的. 所以说本文这次主要介绍的内容就是返回上一个页面时传递参数 ...
- 3.5 Windows驱动开发:应用层与内核层内存映射
在上一篇博文<内核通过PEB得到进程参数>中我们通过使用KeStackAttachProcess附加进程的方式得到了该进程的PEB结构信息,本篇文章同样需要使用进程附加功能,但这次我们将实 ...
- 记忆化搜索 P1028 数的计算
P1028 [NOIP2001 普及组] 数的计算 - 洛谷 | 计算机科学教育新生态 (luogu.com.cn) 一开始是想暴力搜索的,也就是枚举比n/2小的数,但是只过了5个点,其他点都TLE ...
- 小知识:RHEL7上设置Keepalived日志重定向
1.配置 /etc/sysconfig/keepalived 文件 2.添加keepalived日志保存位置的配置 3.修改 /lib/systemd/system/keepalived.servic ...
- macOS 上 常用的操作
首先 mac上 若使用的是windows的键盘,那么需要把ctrl 键,设置成 cmd键,因为mac上大多数操作都是 基于cmd键. 1.将ctrl键,修改为cmd键,这样后 复制.粘贴.剪切.全选等 ...
- ASCII、Unicode、UTF8 10年后,史无前例,自己用js实现《专题3》
我自己史无前例的开发了一个网页小工具,可以利用这个工具 直接查询到 一个字符的unicode二进制的定义,和utf8格式下内存中 存储的二进制. =========================== ...
- OGG-Postgres实时同步到MySQL
(一)数据库信息 名称 源端数据库 目标端数据库 数据库类型 Postgresql 12.4 MySQL 5.7 IP地址 20.2.127.23 20.2.127.24 端口 5432 3306 数 ...
- JS Leetcode 80. 删除有序数组中的重复项 II题解,常规解法与快慢双指针做法
壹 ❀ 引 今天的题目来自LeetCode80. 删除有序数组中的重复项 II,是一道难度中等,但实际挺简单的一道题,题目描述如下: 给你一个有序数组 nums ,请你 原地 删除重复出现的元素,使每 ...