C # series of articles on interoperability:
- C # Introduction to interoperability (I): C # Introduction to interoperability
- C # Introduction to interoperability series (II): use the platform to call Win32 Functions
- C # Introduction to interoperability series (III): data sending and processing in Platform calls
- C # Introduction to interoperability series (4): Calling COM components in C #
Summary of this topic:
- Introduction
- Platform call
- C ++ InterOP (interoperability)
- Com InterOP)
I. Introduction
This series is a series left behind in C # basic knowledge, because one of the new features in C #4.0 is the improvement of COM interoperability. However, com interoperability is. NET platform, to help you better understand. this series is available only because of the Interoperability Technology under the. NET platform. However, some may have the question: "Why do we need to master the Interoperability Technology ?" The explanation for this problem is that the Interoperability Technology on the. NET platform can help us call Unmanaged DLL and COM components in. net .. Net is a development framework built on the operating system.. Net class library classes are also abstract encapsulation of Windows APIs, however. the. NET class library cannot encapsulate all windows APIs. net does not implement a function of the class, but this function is implemented in Windows API, at this time we do not have to go in. you can call functions in Windows API to implement a custom class. In this case, the interaction between managed code and non-managed code is involved, in this case, you need to use the Interoperability Technology to achieve better interaction between hosted code and unmanaged code .. NET platform provides three types of interoperability technologies:
- Platform Invoke (P/invoke) is called by the platform. It is mainly used to call C-library functions and Windows APIs.
- C ++ introp is mainly used to call the C ++ class library in managed C ++ (managed C ++ ).
- Com InterOP is mainly used to call COM components in. NET and use. Net assembly in COM.
The following describes the three technologies respectively.
2. Platform call
The Platform calling technology allows you to call the unmanaged functions implemented in the dynamic link library (DLL) in the managed code, such as Win32 DLL and the DLL created in C/C ++. Here, some friends may have questions-Where can we use the platform call technology to call the unmanaged functions in the dynamic link library?
This is just as mentioned in the previous introduction ,. net class library does not provide related APIs. However, when Win32 API provides related function implementation, you can consider using the platform calling Technology in.. NET applications call functions in Win32 APIs;
However, another use case is that the efficiency of managed code is not as high as that of unmanaged code. To improve the efficiency, you can also consider calling C library functions in managed code.
2.1 steps to call unmanaged code through platform calls in managed code
(1). Obtain information about unmanaged functions, such as the DLL name and the name of the unmanaged functions to be called.
(2). Declare the unmanaged functions in the managed code, and attach the required properties of the platform call.
(3). directly call the managed function declared in step 2 in the managed code
2.2 platform call Process
(1) Find the DLL that contains the function. When you need to call a function, of course, the first step is to know the location of the DLL that contains the function, therefore, the first step for calling the Platform is to find the DLL,In fact, the process of calling an unmanaged code in a hosted code can be imagined as calling someone to do things. First, we need to find out where the person is (that is, the dll process of the function ), find the person and tell him what to do (equivalent to loading the DLL to the memory and passing in parameters ), finally, let him do what needs to be done (equivalent to letting the unmanaged function execute the task ).
(2) load the DLL to the memory.
(3) find the address of the function in the memory and push its parameters into the stack to seal the required data. The CLR searches for and loads the DLL and finds the address of the function in the memory only when it calls the function for the first time. After a function is called once, the CLR caches the function address. The CLR mechanism can improve the platform call efficiency. The DLL found remains in the memory until the application domain is detached.
(4) execute an unmanaged function.
The Platform call process can be better understood through:
C ++ InterOP
The second part mainly introduces the first interoperability technology, and then we can use the C ++ InterOP technology to implement interaction with non-hosted code. However, the C ++ InterOP method is different from platform calling, that is, C ++ InterOP allows hosted and unmanaged code to exist in one assembly or even the same file. C ++ InterOP directly links the source code and compiles the unmanaged code to implement interoperability with the unmanaged code, platform calls load the compiled Unmanaged DLL and find the function entry address to implement interoperability with unmanaged functions.C ++ InterOP uses hosted C ++ to wrap unmanaged C ++ code, compile and generate an assembly, and then host the code to reference the Assembly to implement interoperability with unmanaged code.. For more information about the specific usage and comparison with platform calls, I will introduce them in the following topics.
4. com InterOP
COM (Component Object Model, Component Object Model) is a development technology recommended by Microsoft before. As Microsoft has developed a large number of COM components over the past decade, it is impossible to use them. NET technology to rewrite the functions implemented by these COM components.. net ,. NET platform provides com InterOP, that is, com interoperation technology. com InterOP not only supports using COM components in managed code, but also supports hosting objects with the CMO component function. The following describes the two types of support.
4.1 Use COM components in. net
There are three main methods to use COM objects in. Net:
-
-
- Use the tlbimp tool to create an interoperability assembly for the COM component to bind the early COM object, so that you can add the interoperability assembly in the program to call the COM Object
- Bind COM objects through reflection later
- Use P/invoke to create a COM object or use C ++ InterOP to compile a packaging class for the COM Object
However, we often use method 1. The following describes how to use COM objects in. Net:
- Locate the COM component to be used and register it. Use regsvr32.exe to register or deregister com DLL.
- Add a reference to the COM component or Type Library in the project.
When a reference is added, Visual Studio will use the tlbimp.exe(library import program], and the tlbimp.exe program will generate a. NET Framework interoperability assembly.This Assembly is also called the runtime callable packaging (RCW), which contains classes and interfaces in the encapsulated COM component. Visual Studio adds a reference to the generated component to the project.
3. Create an instance of the class in RCW so that the COM object can be used like the hosted object.
The following figure shows how to use COM components in. Net:
4.2 Use. Net assembly in COM
When running the. NET public language, you can use com callable wrapper (CCW) to complete interaction with com-type libraries. CCW can make the COM Client think that it is interacting with a common com type, and make the. NET Component think that it is interacting with the hosted application. Here, CCW is a non-hosted proxy between the COM Client and the managed object. CCW can maintain the lifecycle of managed objects and convert data types between COM and. net. The basic steps for using the. Net Type in COM are as follows:
1. Add interoperability features to the C # project
You can modify the C # project properties to make the Assembly visible to com. Right-click solution, select Properties, select assembly information in application tag, and select make assembly com visible in the pop-up dialog box, as shown in:
2. Generate a com-Type Library and register it for use by the COM Client.
In the "generate" tab, select the "register for com interoperability" option, such:
After the "register for com" option is selected, Visual Studio calls the library export tool (tlbexp.exe .net generate a comlibrary and then use the registry tool (regasm.exe) to complete the operation.. Net program set and the generated com-Type Library are registered, so that the COM Client can use the CCW service. net object.
V. Summary
The content of this topic is over. NET provides a general summary of the interoperability technology. In the subsequent topics, we will introduce the specific technology in detail and give some simple examples.