The project team was prepared to bring people to the encapsulation SDK and wanted to find a good tutorial to learn from him. However, I was not satisfied with the online searching for a long time. I wrote one myself.
If you have any questions, try to raise them.
This document describesHow. NetCall C ++ under the FrameworkWritten DLL.
1,. NetTarget code andC ++Difference and connection of the target code
The so-called target code is a binary code generated by the source code compilation Link (generated). In vs, use the "generate" command to generate the source code as the target code, the target code is usually in the form of an executable file (*. EXE) and dynamic link library (*. DLL.
To understand. net and C ++. First, you need to understand the differences in the running stages of the executable modules generated by them (the C ++ mentioned in this article is not. the C ++ language of the. NET platform corresponds to Win32 applications in Vs, rather than C ++.. net, the same below ).
For. NET applications, they do not run directly on the CPU, but on the. NET runtime. This is similar to the relationship between Java bytecode and Java virtual machine. Here, I usually refer to the. NET runtime as A. Net Virtual Machine. It provides a platform for. Net to generate a target code to run. net. Next, when. NET is running, it converts what the program is going to do into machine commands and sends them to the CPU for execution. Microsoft called. Net runtime as "Common Language Runtime (CLR )". The public language here refers. net program target code is an intermediate language (IL) (compared with the Java bytecode), in. NET platform, whether your program is written in C #, VB or other languages, it will become il after generation. The actual program code running on. NET is the Il code. Vs comes with an il dasm tool that decomassembles EXE or DLL on the. NET platform into an il code.
For C ++ applications, the machine commands generated after compilation are based on the local CPU platform. Therefore, these commands are also called "local code", "Local Code", and "platform code, native code. Run the command directly on the CPU.
We set. net target code is called "managed" code because it runs in. the target code of C ++ is called the "unmanaged" code because it runs directly on the CPU.
The above analysis also shows that ,. net is less efficient than C ++ because it is required during running. net, which takes a certain amount of time. However,. Net code is significantly safer than C ++ code because it is hosted by. Net runtime and does not directly control the CPU and underlying hardware.
2,C ++OfDLLWhat is inside?
There are two main types of C ++ DLL, one is similar to C (using extern "c" to export functions), and the other is completely C ++. The difference between the two is that, because function Overloading is allowed in C ++, the name of the function entry point in the compiled DLL is not necessarily the declared name of the function. Here we mainly introduce the DLL of C, so the DLL mentioned below refers to the DLL of C.
What is the internal DLL? Dll contains common functions encapsulated into one function. In fact, the dll contains a series of functions. You can use the dumpbin tool provided by Vs to view these functions. These functions have another name called "entrypoint", which refers to the entry of the calling program. Generally, the main function is used as the entry point in the EXE program. Every function in the DLL that can be called is an entry point, which means that the program can enter the DLL from this function. These functions, also known as "export functions" as the entry points, define the functions exported by DLL for external use.
3,In. NetPlatform usageDLLExport Function
Calling C ++ DLL in A. net program is not the essence of calling the export function of C ++ DLL directly in the. NET target file. According to the previous analysis ,. net target code is running in. net runtime, while the C ++ DLL runs directly on the CPU ,. net Code cannot break through. net runtime, so it is impossible to directly call. Microsoft provides a way to call C ++ DLL through. Net runtime support, which is called "InterOP (InterOP: inter-operation )". In "interoperability", it is essentially.. net source code. net runtime, this is the work of programmers.
The above analysis shows that the programmer needs to tell the. NET runtime how to call the C ++ DLL. This requires you to declare the export function of C ++ DLL in a way that you can understand when running. net. Because the hosted environment is very different from the non-hosted environment, the programmer needs to tell the. NET runtime what function signature looks like in the unmanaged environment. The function signature includes the function parameters and return values, as well as the parameter sending order.
The following is an example:
In this example, the data type char * of C ++ corresponds to string in. net, and the Data Type int of C ++ corresponds to int32 in. net. The ing between data types in C ++ and C # can be found online. The user is. if you need to use the GetUserName function of the C ++ DLL in the net code, call the GetUserName function. On the surface, it seems that the user is calling the function in the C ++ DLL, the user calls the Declaration in C.. Net runtime .. Net receives the call request, converts the int32 submitted by the user to a local int.. net, and call the function GetUserName of C ++. net receives the return value during runtime, converts the type from char * to string, and then returns it to the user. In this way, the user receives the return value of the function.
In the process of using C ++ DLL export functions on the. NET platform, there are several concepts that are very important:
L Module name: Usually The Name Of The DLL file;
L entry point: Usually the name of a function, but sometimes there are exceptions (......);
L function signature: the parameters and return values of the function, in. the hosted function signature declared in. NET must be completely consistent with the function signature in the non-hosted local DLL. Otherwise, an error occurs during the call.
L delivery: the real parameters for calling.. NET is responsible for sending messages to unmanaged DLL during runtime. The returned values of local code execution are. net Runtime is responsible for sending back to the managed code.