First, I would like to share my personal experience

I created a C++ win32 dynamic library project, ready to call C# class library.

// Do not attempt to run managed code inside DllMain or image initialization functions, as doing so will cause the application to hang. // Solution: BOOL APIENTRY dllmain //2. // (1) CLR/CLI linker failed, error LNK2022 – custom attribute inconsistent // (2) software release, Windows7 64-bit pure edition, software startup, KERNELBASE. DLL error // cause and solution: // The.NET version does not match. Please install the one you need. // how visual studio can modify the.net framework version of a c++ project // modify the project file // in solution explorer in visual studio, open the shortcut menu for the project, Then select Uninstall Project. // This will uninstall the project file (.vcxproj) for your project. // On the menu bar, select File, Open, and File. In the Open File dialog box, navigate to the project folder, // then open the project file (.vcxproj). In the project file, find the entry for the target Framework version. // For example, if your project is designed to use. The.net Framework 4.5. Found in element // Please see

v4.5
element // if

element does not exist, add it and change the corresponding version number. // C++ platform tool set if MSVC2015, then. NET framework at least version 4.0 //VC++ platform tool set if MSVC2013, then. The NET framework can be version 3.5 // considering the pure version of Windows 7. NET version is 3.5, so it is recommended that C++ projects use MSVC2013 compilation; // Because the customer’s Windows 7 computer may not have it. NET4.0, so that we can save ourselves every time the equipment shipped installation. NET4.0 framework

 

Second, look at the experience summary of online excerpts

Programs written in C++ are unmanaged code, while programs written in C# are managed code. Managed code offers many advantages that other development platforms do not, but because many previous systems and historical versions used programs written in unmanaged code, the CLR provides mechanisms to include both managed and unmanaged code in applications. Specifically, it can be divided into the following three types:

Managed code can call unmanaged functions in DLLS. Invoke functions in DLLS, such as Kernel32.dll, through P/Invoke(Platform Invoke) mechanism.

Managed code can use existing COM components (servers). Many companies have implemented a number of unmanaged COM components. Using type libraries from these components, a managed assembly can be created to describe COM components. Managed code can access a type in a managed assembly like any other type.

Unmanaged code can use managed types (servers). Much of the existing unmanaged code requires COM components to ensure that the code works correctly. Using managed code makes it easier to implement these components without all the code having to deal with reference counts and interfaces. For example, C++ calls a DLL developed by C#.

How does C++ call libraries written in C#? Checked the information on the Internet, now know two cases, one is to use C++/CLI (general language interface), the other is to use COM.

Conclusion: 2) to reference such libraries in C++, use #using references to DLLS written in C# instead of #include. MathDLL 3) create C# objects with gcnew; 4) C++ compilation Settings must be set to support common language runtime support (/ CLR), use C++/ CLR syntax, and access managed objects in the correct way, i.e. use the cap “^” instead of “*”. 5) own C++ classes must be defined with ref class.

6) # using “.. \\TestDLL\\bin\ Debug\ TestDLL. DLL “this is a method used to call a.net DLL in VC++ managed code. If it is native C++ code, it can not be called in this way.

7) CSDN Teacher Zhao 4 suggested:

Don’t make A useless attempt to change A language code to B language code. And don’t do something as complicated and error-prone as calling A B code base directly from A code. Simply redirect the input and output of A and B code to A text file, or modify the input and output of A and B code to A text file. It is convenient for A and B to work in harmony with each other. Such as: TXT file b. TXT file b. TXT file B. TXT file B. TXT file B. TXT file B. TXT file B. TXT file B. TXT file B. TXT file B. TXT file B. TXT file B. TXT file A. TXT A can be replaced with any development language or environment. B can be replaced with any development language or environment that is different from A. Unless A or B does not support file existence, file read and write, and file rename. But who can name a development language or environment that doesn’t support the ability to determine whether files exist, read and write files, and rename files? Temporary files can be placed on RamDisk to improve efficiency and reduce disk wear. Sharing temporary text files between processes has many advantages over other methods. Here are just a few that I can think of right now: · Loose coupling between processes · Processes can be on the same machine, across machines, across operating systems, across hardware platforms, or even across borders. · Easy to debug and monitor by simply letting a third party or a human view the temporary text file. · Easy to switch on and off the service online, simply delete or create the temporary text file. · Convenient implementation of distributed and load balancing. · Convenient to queue services, and it is almost impossible to queue full (unless hard disk space is full) ·… “Across languages, across computers, across operating systems, across hardware platforms, across borders, across * *” boundless sea of pain, turning back is “using shared plain text files for information exchange” shore!

 

Three, reprint online article “N ways to use C++ call C#.NET library”

Normal method 1:COM registers the managed class as COM using C#, registers the output assembly with regasm.exe, and then calls the type inside the assembly using C++ as COM. Advantages: write code simple, easy to call Weakness: the need to register the output, release enough simple reference: www.codeproject.com/KB/cs/Manag… Conventional method 2:CLR C# writes classes conventionally, produces assemblies, and C++ uses CLR compilation to reference managed classes directly. Advantages: write code simple, easy to call Disadvantages: need to know c + + the CLR grammar (neither like c + +, and unlike c #, in all strange) reference: www.codeproject.com/KB/mcpp/cpp…

Msdn.microsoft.com/en-us/libra… General method 3:API C# writes classes normally, produces assemblies, and C++ calls them using the CLR unmanaged interface (CLRCreateInstance) provided by the SDK. Advantages: traditional C# programming, traditional C++ programming disadvantages: requirements. The.net Framework 4.0 above reference: nport.codeplex.com/SourceContr…

Msdn.microsoft.com/en-us/libra… Workaround: 1. Wrap existing managed classes in C#/VB and register them as Windows services to expose SOAP web services. VC2005 can add reference Web services using unmanaged code. 2. Wrap existing managed classes with C#/VB and register them as Windows services. C++ leverages Windows message and service communication. 3. Wrap existing managed classes with C#/VB and register them as Windows services. C++ leverages Windows shared memory and service communication. In fact, using the method of two-process communication, you can evolve a variety of call ideas. You are smart enough to use your imagination and write your own invocation patterns.

 

References:

www.cnblogs.com/bylikai/p/4… CLR

www.cnblogs.com/huangmianwu… COM

Blog.csdn.net/shq88625896… both

Blog.csdn.net/sudazf/arti… This is good

From C++ to C++/CLI

Docs.microsoft.com/zh-cn/previ…

Docs.microsoft.com/en-us/previ…

Docs.microsoft.com/zh-cn/cpp/d…