C#與C++與互操作實(shí)例講解
一、C#調(diào)用C++庫
1、創(chuàng)建C++庫
打開VisualStudio,創(chuàng)建一個(gè)C++工程,輸入項(xiàng)目名稱HelloWorldLib

確定,然后下一步。選擇應(yīng)用程序類型為DLL

單擊完成,我們就創(chuàng)建好了一個(gè)C++庫的項(xiàng)目。
這里為了方便,我們直接在HelloWorldLib.cpp里定義函數(shù)
C++庫導(dǎo)出有兩種方式
一、以C語言接口的方式導(dǎo)出
這種方法就是在函數(shù)前面加上 extern "C" __declspec(dllexport)
加上extern "C"后,會(huì)指示編譯器這部分代碼按C語言的進(jìn)行編譯,而不是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;
}
二、以模塊定義文件的方式導(dǎo)出
在工程上右鍵,選擇添加-》新建項(xiàng)

然后選擇代碼-》模塊定義文件

在Source.def中輸入
LIBRARY EXPORTS HelloWorld
EXPORTS下面就是要導(dǎo)出的函數(shù),這里不需要添加分號(hào)隔開,直接換行就行。
此時(shí),我們函數(shù)的定義如下
#include "stdafx.h"
#include<iostream>
void HelloWorld(char* name);
void HelloWorld(char* name)
{
std::cout <<"Hello World "<< name << std::endl;
}
編譯,生成dll。這里需要注意的是,如果生成是64位的庫,C#程序也要是64位的,否則會(huì)報(bào)錯(cuò)。
2、使用C#調(diào)用
接下來我們新建一個(gè)C#控制臺(tái)項(xiàng)目

打開前面C++庫生成的目錄,將HelloWorldLib.dll復(fù)制到C#工程的Debug目錄下。也可以不復(fù)制,只需在引用dll的時(shí)候?qū)懮贤暾窂骄托辛恕_@里我是直接復(fù)制到Debug目錄下
using System.Runtime.InteropServices;
namespace ConsoleApplication2
{
class Program
{
[DllImport("HelloWorldLib.dll")]
public static extern void HelloWorld(string name);
//可以通過EntryPoint特性指定函數(shù)入口,然后為函數(shù)定義別名
[DllImport("HelloWorldLib.dll", EntryPoint = "HelloWorld")]
public static extern void CustomName(string name);
static void Main(string[] args)
{
HelloWorld("LiLi");
//跟上面是一樣的
CustomName("QiQi");
}
}
}
運(yùn)行程序,結(jié)果如下:

這樣就成功創(chuàng)建了一個(gè)C#可以調(diào)用的C++庫
下面我們動(dòng)態(tài)調(diào)用C++庫,這里委托的作用就比較明顯了。把委托比喻為C++的函數(shù)指針,一點(diǎn)也不為過。
我們?cè)贑++庫中再新增一個(gè)函數(shù)GetYear(),用來獲取當(dāng)前年份。
int GetYear();
int GetYear()
{
SYSTEMTIME tm;
GetLocalTime(&tm);
return tm.wYear;
}
記得在導(dǎo)出文件中(Source.def)增加GetYear。編譯,生成新的DLL
再新建一個(gè)C#控制臺(tái)程序
代碼如下:
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++庫中的對(duì)應(yīng)
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));
//在這里可以調(diào)用
int year = m_fGetYear();
Console.WriteLine("年份是:" + year);
}
}
}
}
}
運(yùn)行結(jié)果:

好的,前面函數(shù)里面涉及的都是簡(jiǎn)單數(shù)據(jù)類型,下面來介紹一下復(fù)雜數(shù)據(jù)類型。這里指的是結(jié)構(gòu)體
在C++庫中定義一個(gè)GetDate()的函數(shù),代碼如下。這里也要記得在導(dǎo)出文件中添加(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;
}
新建一個(gè)C#控制臺(tái)程序,完整代碼如下
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);
}
}
}
}
}
}
運(yùn)行結(jié)果如下:

C#與C++互操作,很重要的一個(gè)地方就是,要注意數(shù)據(jù)類型的對(duì)應(yīng)。有時(shí)還需要加上一些限制,
關(guān)于C#與C++數(shù)據(jù)類型對(duì)應(yīng)
可以參考以下鏈接:
http://www.dbjr.com.cn/article/168509.htm
大部分硬件廠商提供的SDK都是需要C++來調(diào)用的,有了上面的知識(shí),使用C#來調(diào)用一些硬件的SDK就比較容易了。只需要使用C++再進(jìn)行一次封裝就行了。
二、C++調(diào)用C#庫
這里用到是C++/CLI,就是如何用C++在·NET中編程。就是因?yàn)橛羞@個(gè)東西的存在,C++才能調(diào)用C#的庫
下面新建一個(gè)C#類庫CSharpLib

以上就是全部知識(shí)點(diǎn)內(nèi)容,感謝大家對(duì)腳本之家的支持。
相關(guān)文章
C#緩存之SqlCacheDependency用法實(shí)例總結(jié)
這篇文章主要介紹了C#緩存之SqlCacheDependency用法,在C#程序設(shè)計(jì)中有一定的實(shí)用價(jià)值,需要的朋友可以參考下2014-08-08
c#的時(shí)間日期操作示例分享(c#獲取當(dāng)前日期)
這篇文章主要介紹了c#的時(shí)間日期操作示例,在給定時(shí)間戳返回指定的時(shí)間格式和獲取當(dāng)前時(shí)間方法,需要的朋友可以參考下2014-03-03
C#連接SQL Server的實(shí)現(xiàn)方法
這篇文章主要給大家介紹了關(guān)于C#連接SQL Server的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12
C#使用iCSharpcode進(jìn)行文件壓縮實(shí)現(xiàn)方法
這篇文章主要介紹了C#使用iCSharpcode進(jìn)行文件壓縮實(shí)現(xiàn)方法,末尾附有完整實(shí)例,有助于大家參考借鑒,需要的朋友可以參考下2014-08-08
C#中的Image控件用法詳解與實(shí)際應(yīng)用示例
在C#應(yīng)用程序開發(fā)中,圖像顯示是一個(gè)常見的需求,無論是創(chuàng)建圖形界面還是處理圖像數(shù)據(jù),System.Windows.Controls.Image控件都是實(shí)現(xiàn)這一目標(biāo)的重要工具,本文將詳細(xì)介紹Image控件的功能、用法、優(yōu)化技巧以及一些實(shí)際應(yīng)用示例,需要的朋友可以參考下2024-06-06
C#中一個(gè)高性能異步socket封裝庫的實(shí)現(xiàn)思路分享
下面小編就為大家分享一篇C#中一個(gè)高性能異步socket封裝庫的實(shí)現(xiàn)思路,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2017-11-11

