In .Net Framework CLR provides execution of instruction targeted for CLR and the instructions that are not targeted for CLR. The instructions targeted for CLR are called as managed code and other type of code is called unmanaged code. After going through this topic you will know the following things mentioned below
Difference between Managed Method and Unmanaged Method.
Difference between Managed Type and Unmanaged Type.
How to call unmanaged methods in managed methods.
How to use unmanaged types.
When an application is launched for execution, first request is given to the Operating System, the OS will load the executable file in memory and starts executing the instruction from the entry point function in the executable file. Where in .NET executable file contains four main components the CLR header, the Metadata, the MSIL Code, and Native code.
CLR header will be used by the managed code in the module which will have the version number of the CLR on which the module is built and entry point method of the module in the executable.
Metadata describes the types used in the managed code, combination of CLR header and MSIL Code is the compiled format of a .Net language on a .Net Compiler, which will not have the instruction in targeted machine instruction format, which will again get compiled by the JIT Compiler
Native Code contains the machine instruction, which will be directly executed by the OS. Not all the .NET PE will have the Native code. PE of type EXE’s will be having a native method like main() called as “unmanaged stub” which will be an entry point for the OS to execute code, that function will jump to _CorExeMain function located in MSCoree.dll which will be executed by the OS to initialize CLR and attach the running .NET module to CLR. Once CLR is initialized and loaded CLR will start executing the assembly by executing the managed entry point function specified in the CLR header of the file.
Machine instructions in MSIL format and located in Assemblies will be executed by the CLR, will have the following intrinsic advantages,
Memory management to prevent memory leaks in the program code,
- Thread execution,
- Code safety verification,
- Compilation,
Managed methods will be marked as “cil” in MSIL code. Methods marked with “cil” will be compiled by mscorjit.dll before execution. C# and VB.NET will generate only managed code. (Managed C++ can generate managed code by specifying “#pragma managed”)
Unmanaged codes are the instructions, which are targeted for specific platforms.
Unmanaged code will exist in any of the format,
A code instructions in managed C++ with “#pragma unmanaged”
COM/COM+ Components
Win32 Dlls/System Dlls
Unmanaged code will exist in any of the format,
A code instructions in managed C++ with “#pragma unmanaged”
COM/COM+ Components
Win32 Dlls/System Dlls
As these codes are in native format of OS, these instructions will be executed faster compared with JIT compilation and execution of Managed code.
As we get more benefits from managed code, still we may need to use unmanaged code. .Net provides many ways to access unmanaged code in managed code. Managed- Unmanaged transitions are achieved in .Net by set of services called Platform Invocation Services (P/Invoke) and IJW(It Just Works).
P/Invoke services are targeted for unmanaged code, which exists as COM/COM+ components and Win32 DLLs. COM/COM+ components will accessed by the concept called COM Interop - is a mechanism in which existing COM components will be accessed through a wrapper class called COM Callable Wrapper (CCW) in managed code without modifying the existing COM components. Using P/Invoke mechanism we can call Windows DLL functions in managed code.
IJW(It Just Works) targets code instructions built on C++ managed extensions, This mechanism is only for the code in Managed C++. In this way we can call the unmanaged methods directly by the managed code.
For example following code calls the MessageBox function in User32.dll(VB.NET).