C#與C++與互操作實例講解

在本篇文章裏小編給大家整理了關於C#與C++與互操作實例以及相關內容,需要的朋友們可以學習下。

一、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://www.jb51.net/article/168509.htm

大部分硬件廠商提供的SDK都是需要C++來調用的,有了上面的知識,使用C#來調用一些硬件的SDK就比較容易了。只需要使用C++再進行一次封裝就行了。

二、C++調用C#庫

這裏用到是C++/CLI,就是如何用C++在·NET中編程。就是因爲有這個東西的存在,C++才能調用C#的庫

下面新建一個C#類庫CSharpLib

 以上就是全部知識點內容,感謝大家對神馬文庫的支持。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章