Talking about COM Interop - Talking about PIA
The order is very embarrassing, because of the reasons for work, "Going to the COM Interop" article has only a second article. This article will introduce PIA (Primary Interop Assembly), which will be inserted with some of the contents of the Strong Name Tool (sn.exe). I hope you will advise you. First, what is PIA? Its definition, the official interpretation of MSDN is "The main Interop assembly is a unique assembly provided by the vendor. It contains type definitions for types implemented with COM (as metadata). Can only have one main interop assembly, And the assembly must be signed with a strong name by the issuer of the COM type library. One master interface can be packaged in multiple versions of the same type of library. ". Second, why need PIA? For example. There are 2 hosted applications ShapeTool and TextTool reference COM component Graphic, and each uses their own key to generate an interop assembly for the GRAPHIC used. A IGRAPHICMETA interface is provided in the COM component Graphic. There is a method in ShapeTool that needs to pass this interface reference. If TextTool needs to call ShapeTool's provided this method, it needs to pass itself to the interop assembly as a parameter. But this will be very dangerous, it will introduce a lot of serious problems. The reasons should be obvious, because these two applications use their own key to sign the INTEROP Assembly, while CLR will think they are 2 completely different entities. So, this will appear confusion. Then, what we need to think about how to solve this problem. Solve the way is in the PIA. What is the difference between PIA with a general interop askSembly? I observed, it seems to be more than one named PrimaryInteroPassemblyingTribute. This property has 2 parameters, which are the main version numbers of the type library of the primary interop assembly. The secondary version number. See Figure 1. Today, many large software have provided PIA to develop secondary development. There are many examples, such as: Office, Crystal Report, and the like. Third, a practical drill here we do a simple demo, which will generate interop.piademo.dll as shown in Figure 1. The specific steps are as follows: 1. Create an ActiveX DLL project in VB, named PiaDemo, class named Test. Because of the reasons of the space, here are taken in the chamfer (see Figure 2). Figure 2. Generating a key We need to use Sn.exe to generate a key file. C: /> Sn -k common.snk3. Generate PIA C: /> TLBIMP PIADEMO.DLL / PRIMARY /KEYFILE: Common.snk /out:interop.piademo.dll Tlbimp.exe can bring a lot of parameters, please refer to MSDN Related instructions in. 4. After the third step is completed, PIA has been generated. However, next, the publisher of the component needs to consider whether to deploy the generated assembly in the GAC (Global Assembly Cache). If you don't plan to deploy it into the GAC, then we need to do some extra things. Because when we import a COM component, the caller (such as: Visual Studio.NET) will go to the registry to exist if there is available PIA, if not found, vs.net will automatically create one of this COM component. Interop assembly. To do this, we need to register this PIA we generated using .NET.
This tool is very simple: regaSM interop.piademo.dll This, we can find it in the registry (see Figure 3, 4). Figure 3 Figure 4 is equally, if you deploy the generated assembly to the GAC, you don't need this (when this PIA will appear in the .NET component list). Deploy assembly to the GAC to use the Gacutil.exe tool provided by .NET. Gacutil -i interop.piademo.dll5. Write the call end code here, I give a simple example. Using
System;
Using
Interop.PiaDemo;
Namespace
DefaultNamespace
{Class Mainclass {public static void main (string [] args) {testclass test = new testclass (); console.writeline (TEST.FORMAT ("Go!"));}}}
At this point, we have completed a simple example of generating PIA. However, please note that if your program set references the type in the third party COM type library, you must first get a master Interop assembly from the issuer to generate your main intero. In addition, due to the time relationship, the contents of the COM type library introduced in programming will be described in the next article. Please excuse me.