当前位置: 移动技术网 > IT编程>开发语言>c# > C#与C++与互操作实例讲解

C#与C++与互操作实例讲解

2019年09月09日  | 移动技术网IT编程  | 我要评论
一、c#调用c++库 1、创建c++库 打开visualstudio,创建一个c++工程,输入项目名称helloworldlib 确定,然后下一步。选择应用程序类

一、c#调用c++库

1、创建c++库

打开visualstudio,创建一个c++工程,输入项目名称helloworldlib

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

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

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

c++库导出有两种方式

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

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

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

#include "stdafx.h"
#include<iostream>

extern "c" __declspec(dllexport) void helloworld(char* name);


extern "c" __declspec(dllexport) void helloworld(char* name)
{
 std::cout << "hello world " << name << std::endl;
}

 

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

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

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

在source.def中输入

library

exports
helloworld

 

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

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

#include "stdafx.h"
#include<iostream>

void helloworld(char* name);


void helloworld(char* name)
{
 std::cout <<"hello world "<< name << std::endl;
}

 

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

2、使用c#调用

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

打开前面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");
  }
 }
}

 

运行程序,结果如下:

这样就成功创建了一个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);
    }
   }
  }
 }
}

 

运行结果:

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

在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);
     }
    }
   }
  }
 }
}

运行结果如下:

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

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

可以参考以下链接:

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

二、c++调用c#库

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

下面新建一个c#类库csharplib

 以上就是全部知识点内容,感谢大家对移动技术网的支持。

如您对本文有疑问或者有任何想说的,请点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网