c# 调用 c dll 例子
// case 1 传递 int* /////////////////////////////////////////////
extern “C” __declspec(dllexport) int mySum(int *a2,int *b2)
{
// 改变 a1, b1
*a2=...
*b2=...
return a+b;
}
public static extern int mySum (ref int a1,ref int b1); // c# 声明
///////////////////////////////////////////////////////////////////// // case 2 DLL 传入 char* 打印 /////////////////////////////////////////////
extern “C” __declspec(dllexport) void print(const char *str)
{
printf(str);
}
public static extern void print(string str); // c# 声明
///////////////////////////////////////////////////////////////////// // case 3 传入 char* 写回 ///////////////////////////////////////////
void foo(char* bar) {
// do write some information into char* bar
} [DllImport("foobar.dll")]
private static external void foo(StringBuilder bar); public String ReadFoo() {
StringBuilder result = new StringBuilder();
foo(result);
return result.ToString();
}
// 一些字符的处理
public String ReadFoo2() {
StringBuilder strBuilder = new StringBuilder();
foo(strBuilder);
Byte[] buf = Encoding.Unicode.GetBytes(strBuilder.ToString());
String result = Encoding.ASCII.GetString(buf);
}
///////////////////////////////////////////////////////////////////// // case 4 输入数组 ////////////////////////////////////////////////
[DllImport("foobar.dll")]
private unsafe static extern void getpicture(byte* imageBuffer); private byte[] GetImage() {
// size of the picture is 1024 * 1024 at RGB color, 8 bit each color
Byte[] rc = new Byte[ * * ]; // this block contains unsafe code!!!
unsafe {
// create the pointer by disabling garbage collection and
// memory reallocation
fixed (byte* rcPrt = rc) {
this.getpicture(rcPtr);
}
// devalidate pointer and reenable memory reallocation and
// garbage collection
}
// and get safe again return rc;
}
/////////////////////////////////////////////////////////////////
参考:https://www.gadgetweb.de/programming/38-cs-and-the-char-mess.html
注意:结构体中不可以存在方法或者构造函数等其他一切,也就是说只包含成员变量。因为c++的struct如果包含了这些的话,组织结构就会发生异变,与c不同。而c#只单纯处理c的struct!
c# 调用 c dll 例子的更多相关文章
- [源码]Python调用C# DLL例子(Python与.Net交互)
K8Cscan C# DLL例子代码 namespace CscanDLL { public class scan { public static string run(string ip) { if ...
- C#调用C++ Dll
现在项目基本都是旁边C++的哥们做好dll扔给我,然后我调用.好久之前晚上down了一份c#调用c++dll的方法,出处早已经遗忘.闲来无事,放上来好了.原作者看到后可以留言,我会把您链接放上的,帮了 ...
- c#调用c++ dll(二)
当对c++几种调用方式有了解以后我们可以试着写个c++动态连接库了,我们现在来写个简单的c++求和函数并把它封装成dll,供以后的c#调用 我们写dll的时候,个人认为,要写就要把dll写好,写标准, ...
- Native Application 开发详解(直接在程序中调用 ntdll.dll 中的 Native API,有内存小、速度快、安全、API丰富等8大优点)
文章目录: 1. 引子: 2. Native Application Demo 展示: 3. Native Application 简介: 4. Native Ap ...
- C#调用VC dll输出参数
最近做项目需要把以前Vc写的程序整合到VS2005来,用c#写的,以前VC的动态连接库写好了,重写比较麻烦,让C#调用VC dll就好了.但碰到了一个问题:VC中作为输出参数的char*类型的变量应该 ...
- 巧用C#做中间语言 实现Java调用.net DLL
本文将详细为大家介绍一个java调用.net DLL的方法,以实现特殊的客户的特殊要求:“在Java项目中必须使用其提供的用.net写的DLL加密机制!” 环境与工具: ◆.net framework ...
- 在SQL Server 2008中调用.net,dll
原文:在SQL Server 2008中调用.net,dll T-SQL的在执行普通的查询的时候是很高效的,但是在执行循环,判断这样的语句的时候效率就不那么的高了.这时可以借助CLR了,我们可以在SQ ...
- C# 调用外部dll(转)
C# 调用外部dll 一. DLL与应用程序 动态链接库(也称为DLL,即为"Dynamic Link Library"的缩写)是Microsoft Windows最 ...
- [转]C#调用C++dll
本文转载至http://www.cnblogs.com/ysharp/archive/2012/05/25/2517803.html 在合作开发时,C#时常需要调用C++DLL,当传递参数时时常遇到问 ...
随机推荐
- java ListNode链表数据结构
class ListNode{ int val; ListNode next; } 该节点的值 val. 下一个节点 next
- 剑指offer——面试题26:判断二叉树B是否为二叉树A的子结构
#include"iostream" #include"stdio.h" #include"math.h" using namespace ...
- 转 在子线程中new Handler报错--Can't create handler inside thread that has not called Looper.prepare()
在子线程中new一个Handler为什么会报以下错误? java.lang.RuntimeException: Can't create handler inside thread that has ...
- C#方法重载和方法重写的区别
一.重载的条件: 1.必须在同一个类中: 2.方法名必须相同: 3.参数列表不能相同. 二.重写的条件: 1. 在不同的类中2. 发生方法重写的两个方法返回值,方法名,参数列表必须完全一致(必须具有相 ...
- (转)DB2下载地址总结
原文:https://blog.csdn.net/huozengguang/article/details/58602910 DB2 v8.2,v9.1,v9.5,v9.7下载地址 下列都是完全版包含 ...
- CentOS7安装virtualbox
1.进入virtualbox官网 https://www.virtualbox.org/ 2.点击download 3.点击Linux distributions 4.向下翻至如图,并且进入同种框选页 ...
- JavaScript设计模式-2高级类.
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- C 扩展库 - mysql API general outline
Application programs should use this general outline for interacting with MySQL Initialize the MySQL ...
- 原来你是这样的http2......
欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 本文由mariolu发表于云+社区专栏 序言 目前HTTP/2.0(简称h2)已经在广泛使用(截止2018年8月根据Alexa流行度排名的头 ...
- 关于C# 委托(delegate)与事件(event)的用法及事例
C#中的委托和事件对于新手可能会有一点难理解,所以先从一个小例子入手,以便能更好的理解其如何使用.有一个学生每天定闹钟在早上6点起床,所以当每天早上6点的时候,闹钟就会响起来,从而学生才会按时起床. ...