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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

  2021-04-03 我要评论

一、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++数据类型对应

可以参考以下链接:

https:

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

二、C++调用C#库

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

下面新建一个C#类库CSharpLib

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

您可能感兴趣的文章:

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们