This article describes how to call C # code through DLL in C ++.
First, create the C # "class library" project cshapedll.
Enter the following code:
// C ++ call C # code through DLL // 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)
There are two classes, one is myaddclass class, which is used for addition operations, and the other is cmywriteline, which is used to output text.
Take the C ++ console program as an example. The C ++ code is as follows:
// C ++ call C # code through DLL // http://blog.csdn.net/morewindows/article/details/8678431#using "cshapedll \ bin \ debug \ cshapedll. DLL "// # using" cshapedll \ bin \ release \ cshapedll. DLL "# include <stdio. h> # include <conio. h> using namespace cshapedll; int main () {printf ("C ++ calls C # code \ n" through DLL); printf ("-by morewindows (http://blog.csdn.net/morewindows/article/details/8678431) -\ n "); cmywriteline ^ writelineclass = gcnew cmywriteline; writelineclass-> text =" cmywriteline demonstration using C # "; 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 ("use C # cmyaddclass demonstration \ n"); printf ("% d + % d = % d \ n", addclass-> number1, addclass-> number2, addclass-> addfunc (); getch (); Return 0;} // By morewindows (http://blog.csdn.net/MoreWindows)
Compilation, error. The prompt is as follows:
Fatal error c1190: the "/CLR" option is required for hosting the target code.
Okay, modify it, choose "attributes"> "configuration attributes"> "general"> "Support for public language runtime libraries" and select "Support for public language runtime libraries (/CLR )". As shown in (Images cannot be accessed? Please visit http://blog.csdn.net/morewindows/article/details/8678431 ).
Compile again, and an error occurred! The prompt is as follows:
1> compiling...
1> Cl: Command Line Error d8016: "/MTD" and "/CLR" command line options are not compatible
1> Project: Error prj0002: Error result 2 (returned from "E: \ Program Files \ Microsoft Visual Studio 9.0 \ Vc \ bin \ cl.exe ).
Okay, modify it again, "properties"-> "configuration properties"-> "C/C ++"-> "code generation"-> "Runtime Library", select "multi-thread DLL (/MD) ". As shown in (Images cannot be accessed? Please visit http://blog.csdn.net/morewindows/article/details/8678431 ).
Compile again. An error occurred while running -- "application exception unknown software exception (0xe0434f4d), Location: 0x7c812fd3". As shown in (Images cannot be accessed? Please visit http://blog.csdn.net/morewindows/article/details/8678431 ).
The solution is very simple. This is because the EXE program failed to load the DLL file. Copy cshapedll. DLL to the directory where the EXE program is located and run it again. The result is shown in (WINXP and win7 can both run ):
Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8678431
Welcome to Weibo: http://weibo.com/MoreWindows