c 与java的互操作_C#与C++与互操作实例讲解

一、C#调用C++库

1、创建C++库

打开VisualStudio,创建一个C++工程,输入项目名称HelloWorldLib

bc4778b6739497bc5de96da04e393875.png

确定,然后下一步。选择应用程序类型为DLL

05595f120c2114ed654d38890141e0cb.png

单击完成,我们就创建好了一个C++库的项目。

这里为了方便,我们直接在HelloWorldLib.cpp里定义函数

C++库导出有两种方式

一、以C语言接口的方式导出

这种方法就是在函数前面加上 extern "C" __declspec(dllexport)

加上extern "C"后,会指示编译器这部分代码按C语言的进行编译,而不是C++的。

#include "stdafx.h"

#include

extern "C" __declspec(dllexport) void HelloWorld(char* name);

extern "C" __declspec(dllexport) void HelloWorld(char* name)

{

std::cout << "Hello World " << name << std::endl;

}

二、以模块定义文件的方式导出

在工程上右键,选择添加-》新建项

b0bc1d5657d2954d8a4e93ce307bf3cd.png

然后选择代码-》模块定义文件

cbd009eeef00b1dca5a77dfc565ed15a.png

在Source.def中输入

LIBRARY

EXPORTS

HelloWorld

EXPORTS下面就是要导出的函数,这里不需要添加分号隔开,直接换行就行。

此时,我们函数的定义如下

#include "stdafx.h"

#include

void HelloWorld(char* name);

void HelloWorld(char* name)

{

std::cout <

}

编译,生成dll。这里需要注意的是,如果生成是64位的库,C#程序也要是64位的,否则会报错。

2、使用C#调用

接下来我们新建一个C#控制台项目

d77c5012d1e15cb32ef4af0c7473d906.png

打开前面C++库生成的目录,将HelloWorldLib.dll复制到C#工程的Debug目录下。也可以不复制,只需在引用dll的时候写上完整路径就行了。这里我是直接复制到Debug目录下

using System.Runtime.InteropServices;

namespace ConsoleApplication2

{

class Program

{

[DllImport("HelloWorldLib.dll")]

public static extern void HelloWorld(string name);

//可以通过EntryPoint特性指定函数入口,然后为函数定义别名

[DllImport("HelloWorldLib.dll", EntryPoint = "HelloWorld")]

public static extern void CustomName(string name);

static void Main(string[] args)

{

HelloWorld("LiLi");

//跟上面是一样的

CustomName("QiQi");

}

}

}

运行程序,结果如下:

8a7a031ff4ff414a0c1bb628911fc72e.png

这样就成功创建了一个C#可以调用的C++库

下面我们动态调用C++库,这里委托的作用就比较明显了。把委托比喻为C++的函数指针,一点也不为过。

我们在C++库中再新增一个函数GetYear(),用来获取当前年份。

int GetYear();

int GetYear()

{

SYSTEMTIME tm;

GetLocalTime(&tm);

return tm.wYear;

}

记得在导出文件中(Source.def)增加GetYear。编译,生成新的DLL

再新建一个C#控制台程序

代码如下:

using System;

using System.Runtime.InteropServices;

namespace ConsoleApplication3

{

class Program

{

[DllImport("kernel32.dll")]

public static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("kernel32.dll")]

public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);

[DllImport("kernel32", EntryPoint = "FreeLibrary", SetLastError = true)]

public static extern bool FreeLibrary(IntPtr hModule);

//声明委托,这里的签名,需要跟C++库中的对应

delegate int GetYearDelegate();

static void Main(string[] args)

{

GetYearDelegate m_fGetYear;

IntPtr hModule = LoadLibrary("HelloWorldLib.dll");

if(hModule != IntPtr.Zero)

{

IntPtr hProc = GetProcAddress(hModule, "GetYear");

if(hProc != IntPtr.Zero)

{

m_fGetYear = (GetYearDelegate)Marshal.GetDelegateForFunctionPointer(hProc, typeof(GetYearDelegate));

//在这里可以调用

int year = m_fGetYear();

Console.WriteLine("年份是:" + year);

}

}

}

}

}

运行结果:

7fa5b5ec2c952e2de3faf5d33d0ee8a6.png

好的,前面函数里面涉及的都是简单数据类型,下面来介绍一下复杂数据类型。这里指的是结构体

在C++库中定义一个GetDate()的函数,代码如下。这里也要记得在导出文件中添加(Source.def)

struct MyDate

{

int year;

int month;

int day;

};

MyDate GetDate();

MyDate GetDate()

{

SYSTEMTIME tm;

GetLocalTime(&tm);

MyDate md;

md.day = tm.wDay;

md.month = tm.wMonth;

md.year = tm.wYear;

return md;

}

新建一个C#控制台程序,完整代码如下

using System;

using System.Runtime.InteropServices;

namespace ConsoleApplication3

{

struct MyDate

{

public int Year;

public int Month;

public int Day;

}

class Program

{

[DllImport("kernel32.dll")]

public static extern IntPtr LoadLibrary(string lpFileName);

[DllImport("kernel32.dll")]

public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);

[DllImport("kernel32", EntryPoint = "FreeLibrary", SetLastError = true)]

public static extern bool FreeLibrary(IntPtr hModule);

delegate IntPtr GetDateDelegate();

static void Main(string[] args)

{

GetDateDelegate m_fGetDate;

IntPtr hModule = LoadLibrary("HelloWorldLib.dll");

if (hModule != IntPtr.Zero)

{

IntPtr hProc = GetProcAddress(hModule, "GetDate");

if (hProc != IntPtr.Zero)

{

m_fGetDate = (GetDateDelegate)Marshal.GetDelegateForFunctionPointer(hProc, typeof(GetDateDelegate));

IntPtr ptr = m_fGetDate();

if(ptr != IntPtr.Zero)

{

MyDate md = (MyDate)Marshal.PtrToStructure(ptr, typeof(MyDate));

Console.WriteLine("{0}年-{1}月-{2}日",md.Year,md.Month,md.Day);

}

}

}

}

}

}

运行结果如下:

9dbed7bb57eb94b63a1f1f0453bf6fe1.png

C#与C++互操作,很重要的一个地方就是,要注意数据类型的对应。有时还需要加上一些限制,

关于C#与C++数据类型对应

可以参考以下链接:

大部分硬件厂商提供的SDK都是需要C++来调用的,有了上面的知识,使用C#来调用一些硬件的SDK就比较容易了。只需要使用C++再进行一次封装就行了。

二、C++调用C#库

这里用到是C++/CLI,就是如何用C++在·NET中编程。就是因为有这个东西的存在,C++才能调用C#的库

下面新建一个C#类库CSharpLib

0c6da372e6c63e34f9a9cf2166ef6823.png

以上就是全部知识点内容,感谢大家对脚本之家的支持。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值