First of all, I want to complain about the advertisement on the right side of the blog homepage. Recently, there have always been terrible face-lifting advertisements. It's really frightening. =. = Do you share the same feelings?
A long time ago, a wide discussion of language emerged in the blog Park. In fact, language disputes have never stopped in the entire programmer circle. In my opinion, language is just a tool. When you need to write the underlying layer, you can use C ++. When you need to write dazzling desktop applications, you can use C # WPF, when you need web development for large websites, you can select JAVA. Of course there are many other languages. This is just a choice for different projects. each language has a stage that it is good at, and what remains unchanged for years is data structures and algorithms. The words of the family are light. The following is the question:
[Xiao JIU's school is dedicated to describing extraordinary technologies in ordinary languages. If you want to reprint it, please indicate the source: xiaojiu's school. Cnblogs.com/xfuture]
Language barriers: C ++/CLI calls C #
When C # projects need to reference C ++ dll, you can directly use DLLIMPORT for calling. In reverse mode, C ++ projects cannot simply reference C # DLL. The default configuration of the C ++ project is not supported by the public Language Runtime Library. Therefore, we need to modify some configurations to call C # dll. C ++/cli to call c # dll is as follows:
[Thank you for adding: This example is only used by developers for research. c ++ clr conflicts with some c ++ compilation options, some external links to the source of mfc are not supported.]
First, complete C # dll development:
namespace Csharp{ public class Class1 { public string Name = string.Empty; public Class1() { Name = "We can use C#!!"; } }}
Compile to obtain the Csharp dll.
SecondC ++Configuration of project properties:Support for/clr (c ++/cli) when adding a public Language Runtime)
Next stepAdd a new reference and select the Csharp. dll compiled in step 1.
Use# UsingAndUsing namespaceTo call the dll. Pay attention to the usage when allocating memoryGcnew, Gc is required to allocate memory to it. The hosted object must be declared using ^.
#include "stdafx.h"#using "Csharp.dll"using namespace Csharp;int _tmain(int argc, _TCHAR* argv[]){ Class1 ^a = gcnew Class1(); printf("%s/n", a->Name); return 0;}
The result is as follows:
In this way, C ++ can call C # dll.