C # needs to call C + + things, but if you do not want to make COM, you have to first export the function processing in the class.
cannot be called directly, the function must be exported separately
Reference: http://blog.csdn.net/cartzhang/article/details/9097043
C # An example of invoking a C + + export class
Reference: http://blog.csdn.net/huiyouyongdeyu2011/article/details/6547931
This example has not been tested
The following is an example of VS2010 C # calling C + + DLL files
background
During the project, sometimes you need to invoke DLL files that are not written in C #, especially when using some third-party communication components, when developing applications through C #, you need to use the DllImport feature to make method calls. This article will guide you through the process of making this call fast.
Steps
1. Create a csharpinvokecpp solution:
2. Create a C + + Dynamic Library project:
3. In the application settings, select "DLL" and others follow the default options:
Finally click Finish to get the project:
We can see that there are some files here, where dllmain.cpp as the entry point for defining the DLL application, and its function is the same as the EXE file has a main or WinMain entry function, it is a DLL as an entry function, in fact, it is an optional file. It is called when both LoadLibrary and FreeLibrary are called at static or dynamic links. For more information, refer to (http://blog.csdn.net/benkaoya/archive/2008/06/02/2504781.aspx).
4. Now we open the CSharpInvokeCPP.CPPDemo.cpp file:
Now let's add the following:
1//CSharpInvokeCPP.CPPDemo.cpp: Defines an export function for a DLL application.
2//
3
4 #include "stdafx.h"
5
6 extern "C" __declspec (dllexport) int Add (int x, int y)
7 {
8 return x + y;
9}
Ten extern "C" __declspec (dllexport) int Sub (int x, int y)
11 {
return x-y;
13}
extern "C" __declspec (dllexport) int Multiply (int x, int y)
15 {
return x * y;
17}
+ extern "C" __declspec (dllexport) int Divide (int x, int y)
19 {
return x/y;
21}
extern "C" contains a double meaning, literally: First, the object it modifies is "extern", and secondly, the target it modifies is "C". The variables and functions modified by extern "C" are compiled and concatenated in the C language way.
The purpose of __declspec (dllexport) is to put the corresponding function into the DLL dynamic library.
The extern "C" __declspec (dllexport) is added in order to invoke DLL files of unmanaged C + + using DllImport. Because using DllImport, you can only invoke DLLs made from C-language functions.
5. Compile the project program and finally generate CSharpInvokeCPP.CPPDemo.dll and CSharpInvokeCPP.CPPDemo.lib in the debug directory
We use the Anti-compilation tool PE Explorer to view the methods inside the DLL:
It can be found that these four kinds of "subtraction" methods are included in external public functions.
6. Now to demonstrate how to use a C # project to invoke an unmanaged C + + DLL, first create a C # Console application:
7. Create a new Cppdll class on the Csharpinvokecsharp.csharpdemo project and write the following code:
1 public class Cppdll
2 {
3 [DllImport ("CSharpInvokeCPP.CPPDemo.dll")]
4 public static extern int Add (int x, int y);
5
6 [DllImport ("CSharpInvokeCPP.CPPDemo.dll")]
7 public static extern int Sub (int x, int y);
8
9 [DllImport ("CSharpInvokeCPP.CPPDemo.dll")]
Ten public static extern int Multiply (int x, int y);
11
[DllImport ("CSharpInvokeCPP.CPPDemo.dll")]
public static extern int Divide (int x, int y);
14}
DllImport is used as the import entry feature for C + + DLL classes in C # and corresponds to extern "C" through static extern.
8. Also, remember to copy the DLL files generated in the Cppdemo to the bin directory of the Csharpdemo, or you can set the output directory in general, project properties, configuration properties:
Once the project is compiled, the resulting file is automatically output to Csharpdemo.
9. Then write the test code at the main entrance:
1 static void Main (string[] args)
2 {
3 int result = Cppdll. ADD (10, 20);
4 Console.WriteLine ("Ten + = {0}", result);
5
6 result = Cppdll. Sub (30, 12);
7 Console.WriteLine ("30-12 = {0}", result);
8
9 result = Cppdll. Multiply (5, 4);
Ten Console.WriteLine ("5 * 4 = {0}", result);
11
result = Cppdll. Divide (30, 5);
Console.WriteLine ("30/5 = {0}", result);
14
Console.ReadLine ();
16}
Operation Result:
Method gets called.
10. The above method can only be called by a static method for a function in C + +. So how do you invoke a method in a class object in C + + with a static method? Now I add a header file to the Cppdemo project userinfo.h:
1 class UserInfo {
2 Private:
3 char* M_name;
4 int m_age;
5 Public:
6 UserInfo (char* name, int age)
7 {
8 m_name = Name;
9 M_age = age;
10}
Virtual ~userinfo () {}
int Getage () {return m_age;}
char* GetName () {return m_name;}
14};
In CSharpInvokeCPP.CPPDemo.cpp, add some code:
1#include "Malloc.h"
2#include "Userinfo.h"
3
4typedef struct {
5 Char name[32];
6 int age;
7} User;
8
9userinfo* UserInfo;
10
11extern "C" __declspec (dllexport) user* Create (char* name, int age)
12{
user* User = (user*) malloc (sizeof (user));
14
UserInfo = new UserInfo (name, age);
strcpy (User->name, Userinfo->getname ());
User->age = Userinfo->getage ();
18
return user;
20}
This declares a structure, including name and age, that is used to map the structure of the C # aspect.
Note: user* in the code is a pointer, and returning is also an object pointer, in order to prevent the release of local variables after the end of the method scope.
strcpy is a function that replicates a char array.
11. Supplement the code in the Cppdll class in the Csharpdemo project:
1 [DllImport ("CSharpInvokeCPP.CPPDemo.dll")]
2 public static extern IntPtr Create (string name, int age);
3
4 [StructLayout (layoutkind.sequential)]
5 public struct User
6 {
7 [MarshalAs (UnmanagedType.ByValTStr, SizeConst = 32)]
8 public string Name;
9
Ten public int age;
11}
Where the struct user corresponds to the user in C + +.
12. Supplement the code in Program.cs:
1 IntPtr ptr = Cppdll. Create ("Li Ping", 27);
2 Cppdll. User user = (cppdll. User) Marshal.PtrToStructure (PTR, typeof (Cppdll. User));
3 Console.WriteLine ("Name: {0}, Age: {1}", user. Name, user. Age);
Note: Here the struct pointer is first converted to a IntPtr handle and then converted to the structure you need by marshal.ptrtostructrue.
Operation Result:
C # calls the export function of a C + + class