本文將介紹C++中通過DLL來調用C#代碼。 首先建立C#的“類庫”工程CShapeDLL。 然後輸入如下代碼: [csharp] //C++通過DLL調用C#代碼 //http://blog.csdn.net/morewindows/article/details/8678431 //By MoreWindows( http://blog.csdn.net/MoreWindows ) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CShapeDLL { public class CMyAddClass { private int m_nNumber1; private int m_nNumber2; public int Number1 { set { m_nNumber1 = value; } get { return m_nNumber1; } } public int Number2 { set { m_nNumber2 = value; } get { return m_nNumber2; } } public int AddFunc() { return m_nNumber1 + m_nNumber2; } } public class CMyWriteLine { private string m_strText; public string Text { set { m_strText = value; } get { return Text; } } public void WriteLineFunc() { Console.WriteLine(m_strText); } } } // By MoreWindows( http://blog.csdn.net/MoreWindows ) //C++通過DLL調用C#代碼 //http://blog.csdn.net/morewindows/article/details/8678431 //By MoreWindows( http://blog.csdn.net/MoreWindows ) using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace CShapeDLL { public class CMyAddClass { private int m_nNumber1; private int m_nNumber2; public int Number1 { set { m_nNumber1 = value; } get { return m_nNumber1; } } public int Number2 { set { m_nNumber2 = value; } get { return m_nNumber2; } } public int AddFunc() { return m_nNumber1 + m_nNumber2; } } public class CMyWriteLine { private string m_strText; public string Text { set { m_strText = value; } get { return Text; } } public void WriteLineFunc() { Console.WriteLine(m_strText); } } } // By MoreWindows( http://blog.csdn.net/MoreWindows )這裡有兩個類,一個是MyAddClass類,是用來做加法運算的,另一個是CMyWriteLine,用來輸出文本的。 然後以C++控制台程序為例,C++代碼如下: [cpp] //C++通過DLL調用C#代碼 //http://blog.csdn.net/morewindows/article/details/8678431 #using "CShapeDLL\\CShapeDLL\\bin\\Debug\\CShapeDLL.dll" //#using "CShapeDLL\\CShapeDLL\\bin\\Release\\CShapeDLL.dll" #include <stdio.h> #include <conio.h> using namespace CShapeDLL; int main() { printf(" C++通過DLL調用C#代碼\n"); printf(" - By MoreWindows( http://blog.csdn.net/morewindows/article/details/8678431 ) -\n\n"); CMyWriteLine ^ writeLineClass = gcnew CMyWriteLine; writeLineClass->Text = "使用C# 的CMyWriteLine示范"; writeLineClass->WriteLineFunc(); writeLineClass->Text = "By MoreWindows (http://blog.csdn.com/MoreWindows)"; writeLineClass->WriteLineFunc(); writeLineClass->Text = "http://blog.csdn.net/morewindows/article/details/8678431"; writeLineClass->WriteLineFunc(); printf("\n ---------------------------------- \n"); CMyAddClass ^ addClass = gcnew CMyAddClass; addClass->Number1 = 3; addClass->Number2 = 5; printf("使用C# 的CMyAddClass示范\n"); printf("%d + %d = %d\n", addClass->Number1, addClass->Number2, addClass->AddFunc()); getch(); return 0; } //By MoreWindows( http://blog.csdn.net/MoreWindows ) //C++通過DLL調用C#代碼 //http://blog.csdn.net/morewindows/article/details/8678431 #using "CShapeDLL\\CShapeDLL\\bin\\Debug\\CShapeDLL.dll" //#using "CShapeDLL\\CShapeDLL\\bin\\Release\\CShapeDLL.dll" #include <stdio.h> #include <conio.h> using namespace CShapeDLL; int main() { printf(" C++通過DLL調用C#代碼\n"); printf(" - By MoreWindows( http://blog.csdn.net/morewindows/article/details/8678431 ) -\n\n"); CMyWriteLine ^ writeLineClass = gcnew CMyWriteLine; writeLineClass->Text = "使用C# 的CMyWriteLine示范"; writeLineClass->WriteLineFunc(); writeLineClass->Text = "By MoreWindows (http://blog.csdn.com/MoreWindows)"; writeLineClass->WriteLineFunc(); writeLineClass->Text = "http://blog.csdn.net/morewindows/article/details/8678431"; writeLineClass->WriteLineFunc(); printf("\n ---------------------------------- \n"); CMyAddClass ^ addClass = gcnew CMyAddClass; addClass->Number1 = 3; addClass->Number2 = 5; printf("使用C# 的CMyAddClass示范\n"); printf("%d + %d = %d\n", addClass->Number1, addClass->Number2, addClass->AddFunc()); getch(); return 0; } //By MoreWindows( http://blog.csdn.net/MoreWindows ) 編譯,出錯。提示如下: fatal error C1190: 托管目標代碼需要“/clr”選項 好吧,修改下,在“屬性”->“配置屬性”->“常規”->“公共語言運行庫支持”中選取“公共語言運行庫支持(/clr)”。如下圖所示 再編譯,又出錯!提示如下: 1>正在編譯... 1>cl: 命令行error D8016 :“/MTd”和“/clr”命令行選項不兼容 1>項目: error PRJ0002 : 錯誤的結果2 (從“e:\Program Files\Microsoft Visual Studio 9.0\VC\bin\cl.exe”返回)。 好吧,再修改下,“屬性”->“配置屬性”->“C/C++”->“代碼生成”->“運行時庫”中選取“多線程 DLL (/MD)”。如下圖所示 再編譯,成功了。運行下,又出錯了——“應用程序發生異常未知的軟件異常(0xe0434f4d),位置為 0x7c812fd3“。如下圖所示(圖片訪問不了?請訪問 這個怎麼解決了,很簡單,這是因為EXE程序沒能加載到DLL文件導致的,將CShapeDLL.dll拷貝到EXE程序所在目錄下,再運行,成功了。結果如下圖所示(WinXP及Win7均可以運行):