First, we learned from msdn that DllImport is usedPublish the characterization method from an unmanaged dynamic link library (DLL) as a static entry point.
From the preceding statement, we can understand three points: the unmanaged dll compiled by 1.c++ can be introduced to C # Through DllImport; 2. only the C ++ method (or function) can be introduced into C #, but not the data (or variable). 3. after being introduced into C #, it can only be declared as a static function; msdn link to the DllImport property class: http://msdn.microsoft.com/zh-cn/library/system.runtime.interopservices.dllimportattribute (v = VS.100 ). the format of aspxDllImport is as follows: [DllImport ("compute. dll ", EntryPoint =" FunName ", CharSet = CharSet. auto)] public static extern int FunName (type var); Note: static and extern are essential in the introduced format; Next, we will introduce the C ++ class to C #: 1 through dll through DllImport. the source code of the dll containing the C ++ class is as follows: computer. h: # pragma onceclass computer {computer (); public :__ declspec (dllexport) int sum (int mem1, int mem2); // calculate the sum of the two parameters: mem1 + mem2 _ declspec (dllexport) int sum (); // calculate the sum of the two member variables: computer: mem1 + computer: mem2 _ declspec (dllexport) int sub (int mem1, int mem2); // calculate the difference between two parameters: mem1-mem2 _ declspec (dllexport) int sub (); // calculate the difference between the two member variables: computer: mem1-computer: mem 2 _ declspec (dllexport) void setmember (int m1, int m2); // set the values of static variables mem1 and mem2 _ declspec (dllexport) int getmember (int index); // index = 1 or 2, respectively, read the values of mem1 and mem2 private: static int mem1; // only declared as static variables, can access and modify static int mem2;} in C #; int computer: mem1 = 8; // static member initialization int computer: mem2 = 9; the implementation code is not described here. Compile the dll. 2. place the generated dll in the debug \ bin directory of the C # project. 3. introduce the dll to the C # project and import the written classes to add a new class computer to the C # project, in the generated computer. add the following code to the cs file: using System. runtime. interopServices; // This statement ensures that you can call DllImport to write the computer class code as follows: class computer {[DllImport ("compute. dll ", EntryPoint = "? Getmember @ computer @ QAEHH @ Z ", CharSet = CharSet. Auto)] public static extern int getmember (int index); [DllImport (" compute. dll ", EntryPoint = "? Setmember @ computer @ QAEXHH @ Z ", CharSet = CharSet. auto)] public static extern void setMember (int m1, int m2); [DllImport ("compute. dll ", EntryPoint = "? Sum @ computer @ QAEHHH @ Z ", CharSet = CharSet. auto)] public static extern int sum (int mem1, int mem2); [DllImport ("compute. dll ", EntryPoint = "? Sum @ computer @ QAEHXZ ", CharSet = CharSet. Auto)] public static extern int sum (); [DllImport (" compute. dll ", EntryPoint = "? Sub @ computer @ QAEHHH @ Z ", CharSet = CharSet. auto)] public static extern int sub (int mem1, int mem2); [DllImport ("computer. dll ", EntryPoint = "? Sub @ computer @ QAEHXZ ", CharSet = CharSet. auto)] public static extern int sub ();} For details about the parameter attributes of DllImport, see the following link: computer. setMember (4, 5); Console. writeLine (computer. getmember (1); Console. writeLine (computer. getmember (2); Console. writeLine (computer. sum (); Console. writeLine (computer. sum (1, 2); Con Sole. WriteLine (computer. sub (3, 1); this method is feasible after verification. Conclusion: 1. unmanaged C ++ functions can be introduced to C # through dll through DllImport, but they become static. 2. the unmanaged C ++ class can also be introduced to C # Through the above method, but it is equivalent to being a static class and its usage is greatly restricted, you cannot define multiple objects;