. NET framework facilitates interaction with COM components, COM + services, external libraries, and many operating system services. There are differences in data types, method signatures, and error processing mechanisms between hosted and unmanaged object models. To simplify. NET Framework components and non-HostedCodeThe Common Language Runtime hides the differences between the two object models from the client and the server.
The Code executed under runtime control is called managed code. Instead, code running outside of Runtime is called unmanaged code. COM components, ActiveX interfaces, and Win32 API functions are examples of unmanaged code.
Interaction with unmanaged code
Net Framework provides many advanced technologies than other development platforms. However, few companies can afford to design and reimplement all the code. microsoft is aware of this, so it builds CLR to provide a mechanism to allow applicationsProgramIt can contain hosted code and unmanaged code. Specifically, CLR supports three interactive scenarios:
Managed code can call unmanaged functions in the DLL: The hosted code can easily call functions that can be included in the DLL. This is done by using a function called P/invoke (platform invoke). after all, many types of definitions in FCL call functions in kernel32.dll and user32.dll internally. manyProgramming LanguageA mechanism will be exposed so that the hosted code can easily call the unmanaged functions contained in the DLL. For example, a C # application can call the createsemaphore function in kernel32.dll.
Hosted code can use the COM component (server): Many companies have implemented many unmanaged COM components. Using the types in these components, you can create a hosted assembly that describes COM components, managed code can access the managed types in these assemblies, which is the same as accessing other managed types. references and.. NET Framework sdktogether with the latest tlbimp.exe tool. sometimes, you don't have a library. You want to get more control over what tlbimp.exe produces. In this way, you can manually build a type so that CLR can use this type to implement proper interoperability. for example, you can use the DirectX COM component in the C # application.
Unmanaged code can use managed types (servers): Many existing unmanaged code requires you to provide COM components to make the code work normally. it is easy to implement these components by hosting code, so that you can avoid the need for code to handle reference counts and interfaces. for example, you can use C # To create an ActiveX control or shell extension. for more information, see tlbexp.exeand regasm.exe.
In addition to these scenarios, Microsoft's c ++/CLI Compiler (version 14) supports a new/CLR command line switch, which tells the compiler to generate il code rather than native CPU commands. if you have a large number of existing C ++ code, you can use this new compilation switch to re-compile the Code. The new Code must be executed by CLR, you can modify the code to make full use of the unique features of CLR.
However, for the following methods, the/CLR switch cannot compile them into the Il code: including the inline assembly language (implemented by keyword _ ASM); method that accepts variable number parameters; call the setjmp method. It contains some internal programs (intrinsic routine) (for example, _ enable, _ disable, _ returnaddress, _ addressofreturnaddress. for a complete list of C ++/CLI compilers that cannot be compiled into Il, refer to the compiler documentation. when the compiler cannot compile it into Il, it will compile the method into x86 code so that the application can still run.
Remember that even though the generated il code is hosted, the data is not, that is, the data objects are not allocated from the managed stack, so they cannot use the garbage collection mechanism. in fact, the data type is not included in metadata, and the method names of these types still need to be converted (mangle) through C ++ signature encoding ).
The following C code calls the Standard C Runtime Library Function printf and also calls the system. console. writeline method, system. the console type is defined in FCL, So C/C ++ code can be used.
# Include <stdio. h> // for printf
# Using <mscorlib. dll> // For managed types defined in this Assembly
Using namespace system; // easily access system namespace types
// Implement a Normal C/C ++ Main Function
Void main (){
// Call the C Runtime Library's printf function.
Printf ("displayed by printf. \ r \ n ");
// Call the FCL's system. console's writeline method.
Console: writeline ("displayed by console: writeline .");
}
It is not easy to compile this code. If the code is in the managedcapp. cpp file, you will execute the following command to compile it:
CL/CLR managedcapp. cpp
The result is the managedcapp.exe Assembly file. If you run managedcapp.exe, you will see the following output:
C: \> managedcapp
Displayed by printf.
Displayed by console: writeline.
If you use ildasm.exe to check this file, you will see all the global functions and global fields defined in this Assembly. Obviously, the compiler automatically generates a lot of filling code. if you have two main functions, ildasm will display the Il code for you.
. Method Assembly static int32
Modopt ([mscorlib] system. runtime. compilerservices. callconvcdecl) Main () cel managed
{
. Vtentry 70: 1
// Code size 23 (0x17)
. Maxstack 1
Il_0000: ldsflda valuetype '<cppimplementationdetails>'. $ arraytype $ by0bh @ $ CBD
Modopt ([mscorlib] system. runtime. compilerservices. isconst)
'?? _ C @ _ 0bh @ gbhlfcof @ displayed? 5by? 5 printf? 4? $? 6? $ Aa @'
Il_0005: Call vararg int32
Modopt ([mscorlib] system. runtime. compilerservices. callconvcdecl)
Printf (
Int8
Modopt (
[Mscorlib] system. runtime. compilerservices. issignunspecifiedbyte)
Modopt (
[Mscorlib] system. runtime. compilerservices. isconst )*
)
Il_000a: Pop
Il_000b: ldstr "displayed by console: writeline"
Il_0010: Call void [mscorlib] system. Console: writeline (string)
Il_0015: LDC. i4.0
Il_0016: Ret
} // End of method 'Global functions': Main
This article from the csdn blog, reproduced please indicate the source: http://blog.csdn.net/libright/archive/2009/03/04/3957359.aspx