1.C/C++动态库的编写

下面是我编写的一个比较简单的C++dll文件用来测试,关于如何编写dll文件,我这里便不再赘述,不懂得自行查询,

(1).h文件

#ifdef MYDLL_EXPORTS
#define MYDLL_API __declspec(dllexport)
#else
#define MYDLL_API __declspec(dllimport)
#endif//求两个整数的和(普通类型的参数)
extern "C" MYDLL_API int GetSum(int nNum1, int nNum2);
//获取两个整数中的最大值(含有整数类型的指针为参数)
extern "C" MYDLL_API int GetMaxValue(int *pValue1, int* pValue2);
//获取两个整数中的最小值,最小值以传出参数获取
extern "C" MYDLL_API void GetMinValue(int *pValue1, int* pValue2, int *pMinValue);
//带有char[]与char*参数
extern "C" MYDLL_API int GetLength(char szName[], char* szBirth);

(2).cpp文件

//求两个整数的和(普通类型的参数)
extern "C" MYDLL_API int GetSum(int nNum1, int nNum2)
{
return (nNum1 + nNum2);
} //获取整个整数中的最大值
extern "C" MYDLL_API int GetMaxValue(int *pValue1, int* pValue2)
{
return (*pValue1 > *pValue2) ? *pValue1 : *pValue2;
} //获取两个整数中的最小值,最小值以传出参数获取
extern "C" MYDLL_API void GetMinValue(int *pValue1, int* pValue2, int *pMinValue)
{
if (*pValue1 < *pValue2)
{
*pMinValue = *pValue1;
}
else
{
*pMinValue = *pValue2;
}
}
//带有char[]与char*参数
extern "C" MYDLL_API int GetLength(char szName[], char* szBirth)
{
return (strlen(szName) + strlen(szBirth));
}

2.在C#项目中封装上述库文件为C#接口

上述库文件编译成功后,需要把它拷贝到C#项目的运行目录下,然后代码中使用using System.Runtime.InteropService命名空间即可引用

如果不想直接拷贝,可在C#项目中建立一个文件夹,如名称为Dll,将上述库文件拷贝到此文件夹,然后打开C#项目,点击项目->属性->生成事件中添加copy "$(SolutionDir)Dll\*.dll" "$(TargetDir)此批处理语句。程序编译成功后便会自行将此库文件拷贝到运行目录下

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices; namespace Dll_Test
{
public class MyDllHelper
{
private delegate void CallBackDelegate(int nValue1, int nValue2);
//接口实现
public int GetMySum(int num1, int num2)
{
return (GetSum(num1, num2));
} public int GetMyMaxValue(IntPtr Value1, IntPtr Value2)
{
return GetMaxValue(Value1, Value2);
} public void GetMyMinValue(IntPtr Value1, IntPtr Value2, ref int MinValue)
{
GetMinValue(Value1, Value2, ref MinValue);
} public int GetMyLength(string strName, string strBirth)
{
return GetLength(strName, strBirth);
} [DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetSum(int num1, int num2);
[DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern int GetMaxValue(IntPtr Value1, IntPtr pValue2);
[DllImport("MyDll.dll", CallingConvention = CallingConvention.Cdecl)]
private static extern void GetMinValue(IntPtr Value1, IntPtr Value2, ref int MinValue);
[DllImport("MyDll.dll", CallingConvention =CallingConvention.Cdecl)]
private static extern int GetLength(string strName, string strBirth);
}
}

3.接口实现

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks; namespace Dll_Test
{
class Program
{
static void Main(string[] args)
{
MyDllHelper myDll = new MyDllHelper();
Console.WriteLine("两个整数和为:{0}", myDll.GetMySum(, )); int nValue1 = ;
int nValue2 = ;
IntPtr ptr1 = Marshal.AllocHGlobal(sizeof(int));
IntPtr ptr2 = Marshal.AllocHGlobal(sizeof(int)); Marshal.WriteInt32(ptr1, nValue1);
Marshal.WriteInt32(ptr2, nValue2);
Console.WriteLine("两个整数中的最大值:{0}", myDll.GetMyMaxValue(ptr1, ptr2)); int nMinValue = ;
myDll.GetMyMinValue(ptr1, ptr2, ref nMinValue);
Console.WriteLine("两个整数中最大值:{0}", nMinValue); Marshal.FreeHGlobal(ptr1);
Marshal.FreeHGlobal(ptr2); string strName = "zhangsan";
string strBirth = "1993-01-01";
int nLength = myDll.GetMyLength(strName, strBirth);
Console.WriteLine("两个字符串的总长度为:{0}", nLength); Console.ReadKey();
}
}
}

C#直接使用DllImport调用C/C++动态库(dll文件)的更多相关文章

  1. Android NDK开发及调用标准linux动态库.so文件

    源:Android NDK开发及调用标准linux动态库.so文件 预备知识及环境搭建 1.NDK(native development Kit)原生开发工具包,用来快速开发C.C++动态库,并能自动 ...

  2. C++调用C#的动态库dll

    以往我们经常是需要使用C#来调用C++的dll,这通过PInvoke就能实现.现在在实际的项目过程中,有时会遇到在C++的项目中调用某个C#的dll来完成特定的某个功能,我们都知道,Native C+ ...

  3. C/C++ 关于生成静态库(lib)/动态库(dll)文件如何使用(基于windows基础篇)

    1. 首先,如何制作一个静态库(lib)? 额, 对于静态库,我们知道,里头是不应该有Main函数,它只是一个配合文件.之所以称之为lib静态库,其实就是指,我们需要用到lib里头的函数时,我们才会去 ...

  4. Java 加载动态库 dll 文件

    不知道具体原理,但是,加载 dll 文件时,带路径或者更改 dll 文件的名字,都会报错.虽然库记载成功了,但是处女座认为这不可接受.于是有了这个解决方案. 在根目录为库创建软连接,然后使用 syst ...

  5. C# 调用其他的动态库开发应注意的问题

    1.背景 程序开发语言可以说是五花八门,这就引出了一个新问题 ,不同语言开发的系统进行对接时相关调用的问题. 下面我主要说一下我自己在做接口开发时遇到的问题及解决方法仅供参考,我使用的C#开发进行对接 ...

  6. 在VS2015中用C++编写可被其它语言调用的动态库DLL

    转自:http://blog.csdn.net/songyi160/article/details/50754705 VS2015用C++创建动态库DLL步骤如下: (1)启动VS2015>文件 ...

  7. 关于C#调用非托管动态库方式的性能疑问

    最近的项目中,因为一些原因,需要C#调用非托管(这里为C++)的动态库.网上喜闻乐见的方式是采用静态(DllImport)方式进行调用.偶然在园子里看到可以用动态(LoadLibrary,GetPro ...

  8. C#调用C/C++动态库 封送结构体,结构体数组

    一. 结构体的传递 #define JNAAPI extern "C" __declspec(dllexport) // C方式导出函数 typedef struct { int ...

  9. C++与C#有关对库(动态库dll,静态库.lib)文件的调用

    1 动态库的相互调用 1.1 C#调用C++ dll步骤(只能导出方法): 1. c++建立空项目->源文件文件夹中添加cpp文件和函数 2. c++属性设置中,配置类型设置为动态库dll,公共 ...

随机推荐

  1. _C#发送邮箱

    public ActionResult lead() { SendEmail("邮箱号", "吃饭么?", "你要吃什么啊"); retur ...

  2. PHP反射原理的实现

    反射 反射,直观理解就是根据到达地找到出发地和来源.我们可以仅仅通过一个光秃秃对象就能知道它所属的类.拥有哪些方法. 反射是指在PHP运行状态中,扩展分析PHP程序,导出或提出关于类.方法.属性.参数 ...

  3. import提升导致Fundebug报错:“请配置apikey”

    摘要: 解释一下"请配置apikey"报错的原因. 部分Fundebug用户使用import来导入js文件时,出现了"请配置apikey"的报错,这是由于imp ...

  4. 20190325-HTML框架、audio标签、vedio标签、source标签、HTML表单

    目录 1.HTML框架 frameset:框架标记 frame:框架内文件 iframe:内嵌框架 2.audio标签 src:URL(可以用source标签替代) autoplay:自动播放 pre ...

  5. 关于在Python2中使用列表推导式会遇到的问题

    摘自<流畅的Python>第二部分第二章2.2 Python 2.x 中,在列表推导中 for 关键词之后的赋值操作可能会影响列表推导上下文中的同名变量.像下面这个 Python 2.7 ...

  6. sql server 临时表(上) Tempdb概述

    一.概述 在sql server里临时表存储在TempDB库中,TempDB是一个系统数据库,它只有Simple恢复模式,也是最小日志记录操作.主要用于存放局部临时表,全局临时表,表变量,都是基于临时 ...

  7. LSB和MSB

    最低有效位(the least significant bit,lsb)是指一个二进制数字中的第0位(即最低位),具有权值为2^0,可以用它来检测数的奇偶性.与之相反的称之为最高有效位.在大端序中,l ...

  8. phpstorm设置编码格式

    phpstorm设置编码格式 默认: utf-8格式 设置方法: file -> settings -> Editor -> file encodng -> project e ...

  9. 推荐六款炫酷的HTML5效果插件

    1. HTML5 3D图片阴影翻转动画 效果很酷 分享一款很酷的HTML5 3D动画特效,这款3D特效可以为你的图片增加阴影的效果,而且可以让图片在鼠标滑过的时候出现3D翻转的动画效果.这和HTML5 ...

  10. Python中的一些小技巧

    1.Boolean值可以当做一个数值 a = [5,6,7,8,9] print(a[True]) #prints 6 print(a[False]) #prints 5 2.两种方法实现 a = 1 ...