C#與C++與互操作實例講解
一、C#調(diào)用C++庫
1、創(chuàng)建C++庫
打開VisualStudio,創(chuàng)建一個C++工程,輸入項目名稱HelloWorldLib
確定,然后下一步。選擇應(yīng)用程序類型為DLL
單擊完成,我們就創(chuàng)建好了一個C++庫的項目。
這里為了方便,我們直接在HelloWorldLib.cpp里定義函數(shù)
C++庫導(dǎo)出有兩種方式
一、以C語言接口的方式導(dǎo)出
這種方法就是在函數(shù)前面加上 extern "C" __declspec(dllexport)
加上extern "C"后,會指示編譯器這部分代碼按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)出
在工程上右鍵,選擇添加-》新建項
然后選擇代碼-》模塊定義文件
在Source.def中輸入
LIBRARY EXPORTS HelloWorld
EXPORTS下面就是要導(dǎ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位的,否則會報錯。
2、使用C#調(diào)用
接下來我們新建一個C#控制臺項目
打開前面C++庫生成的目錄,將HelloWorldLib.dll復(fù)制到C#工程的Debug目錄下。也可以不復(fù)制,只需在引用dll的時候?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)建了一個C#可以調(diào)用的C++庫
下面我們動態(tài)調(diào)用C++庫,這里委托的作用就比較明顯了。把委托比喻為C++的函數(shù)指針,一點也不為過。
我們在C++庫中再新增一個函數(shù)GetYear(),用來獲取當(dāng)前年份。
int GetYear(); int GetYear() { SYSTEMTIME tm; GetLocalTime(&tm); return tm.wYear; }
記得在導(dǎo)出文件中(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++庫中的對應(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ù)里面涉及的都是簡單數(shù)據(jù)類型,下面來介紹一下復(fù)雜數(shù)據(jù)類型。這里指的是結(jié)構(gòu)體
在C++庫中定義一個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; }
新建一個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); } } } } } }
運(yùn)行結(jié)果如下:
C#與C++互操作,很重要的一個地方就是,要注意數(shù)據(jù)類型的對應(yīng)。有時還需要加上一些限制,
關(guān)于C#與C++數(shù)據(jù)類型對應(yīng)
可以參考以下鏈接:
http://www.dbjr.com.cn/article/168509.htm
大部分硬件廠商提供的SDK都是需要C++來調(diào)用的,有了上面的知識,使用C#來調(diào)用一些硬件的SDK就比較容易了。只需要使用C++再進(jìn)行一次封裝就行了。
二、C++調(diào)用C#庫
這里用到是C++/CLI,就是如何用C++在·NET中編程。就是因為有這個東西的存在,C++才能調(diào)用C#的庫
下面新建一個C#類庫CSharpLib
以上就是全部知識點內(nèi)容,感謝大家對腳本之家的支持。
相關(guān)文章
C#緩存之SqlCacheDependency用法實例總結(jié)
這篇文章主要介紹了C#緩存之SqlCacheDependency用法,在C#程序設(shè)計中有一定的實用價值,需要的朋友可以參考下2014-08-08C#使用iCSharpcode進(jìn)行文件壓縮實現(xiàn)方法
這篇文章主要介紹了C#使用iCSharpcode進(jìn)行文件壓縮實現(xiàn)方法,末尾附有完整實例,有助于大家參考借鑒,需要的朋友可以參考下2014-08-08C#中一個高性能異步socket封裝庫的實現(xiàn)思路分享
下面小編就為大家分享一篇C#中一個高性能異步socket封裝庫的實現(xiàn)思路,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-11-11