1.9 interoperability with unmanaged code and unmanagedCodeInteroperability
. NetThe Framework provides advantages that many other platforms do not have.However,Few companies will redesign and re-implement their code. Microsoft also recognized this issue and passedCLRTo provide some mechanisms to allowProgramMy colleagues include hosted and unmanaged code. Specifically,CLRSupports three types of interoperability scenarios.
■The managed code canDLLCall the unmanaged MethodManaged code can use a mechanism called P/invoke (platform invoke) to easily call functions contained in the DLL. In short, many types defined in FCL must internally call functions exported from kernel32.dll, user32.dll, and so on. ManyProgramming LanguageBoth provide a mechanism to allow the hosted code to conveniently call the unmanaged functions contained in the DLL. For example, the C # application can call the createsemaphore function exported from kernel32.dll.
■Managed code can use existing COMControls (Server)Many companies have implemented a large number of unmanaged COM components. Using the Type Library from these components, you can create a managed assembly to describe COM components. Managed code can access types in managed assembly like any other managed type. For more information, see the tlbimp.exe tool configured with. NET Framework sdk. Sometimes, you may not have a library. Instead, you want to control more content generated by tlbimp.exe. In this case, you canSource codeManually construct a type so that the CLR can use it to achieve correct interoperability. For example, you can use the DirectX COM component from a C # application.
■ hosting type (server) can be used for unmanaged code many existing unmanaged codes require a COM component, this ensures that the code works correctly. With managed code, these components can be implemented more simply, avoiding all Code having to deal with reference counting and interfaces. For example, you can use C # To create an ActiveX control or a shell extension. For more information, see the tlbexp.exeand regasm.exe tools configured with. NET Framework sdk.