Approaching COM Interop
-- Talking about PIA
Collation
I am very ashamed. for work reasons, only the second article in the series "approaching COM Interop" has been published so far. This article will introduce the content of PIA (Primary Interop assemblyia) using the strong name tool (sn.exe. I hope you will give me more advice.
1. What is PIA?
According to the definition, the MSDN official explanation is "the main Interop Assembly is a unique assembly provided by the supplier. It includes the type definition (as metadata) of the type implemented by COM ). Only one master Interop assembly can exist, and the Assembly must be signed by a strong name by the publisher of the COM-Type Library. A master Interop assembly can wrap multiple versions of the same type of library .".
Ii. why PIA?
For example. Two managed applications, ShapeTool and TextTool, both reference the COM component Graphic and use their own keys to generate an Interop Assembly for their own Graphic. The COM component Graphic provides an IGraphicMeta interface. A method in the ShapeTool must be referenced by this interface. If TextTool needs to call this method provided by ShapeTool, it needs to pass the self-generated Interop Assembly as a parameter. But this will be very dangerous, and it will introduce many serious problems. The reason should be obvious, because the two applications respectively use their own keys to sign the generated Interop Assembly, CLR considers them to be two completely different entities. Therefore, chaos may occur.
Next, we need to consider how to solve this problem.The solution lies in PIA.What is the difference between PIA and general Interop Assembly? I have observed that there seems to be another constructor named PrimaryInteropAssemblyAttribute. The constructor of this attribute has two parameters, the major version number and the minor version number of the Type Library of its main Interop assembly. See figure 1.
Figure 1
Today, many large software products have provided PIA for developers to perform secondary development. There are many such examples, such as Office and Crystal Report.
Iii. Practical drills
Here we will make a simple demonstration to generate the Interop. PIADemo. dll shown in section 1. The procedure is as follows:
1. Create an ActiveX Dll project in VB named PIADemo and the class named Test. Because of the length of the article, this is an alternative (see figure 2 ).
Figure 2
2. Generate a key
Secret: Use sn.exe to generate a key file.
C: \> sn-k common. snk
3. Generate PIA
C: \> tlbimp PIADemo. dll/primary/keyfile: common. snk/out: Interop. PIADemo. dll
TlbImp.exe can contain many parameters. For details, see the description in MSDN.
4. After step 4 is completed, PIA is generated. However, the component publisher should consider whether to deploy the generated Assembly to GAC (Global Assembly Cache. If you do not want to deploy it in GAC, we need to do some extra work. During the call, when we import a COM component, the caller (for example, Visual Studio. net) will go to the Registry to check whether any available PIA exists. If not found,. net will automatically create an Interop Assembly for this COM component. To do this, use regasm.exe of .netw.gov.cn to register the generated PIA. This tool is easy to use:
Regasm Interop. PIADemo. dll
In this way, we can find it in the Registry (see figure 3 and 4 ). Figure 3
Figure 4
Of course, this is not required if you deploy the generated Assembly to GAC (PIA will appear in the. Net Component list during import ). Deploy Assemblyto gacuse the. NET tool.
Gacutil-I Interop. PIADemo. dll
5. Compile the caller code
Here, I will 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! "));
}
}
}
So far, we have completed a simple PIA generation example.However, please note that if your Assembly references the type in a third-party COM-type library, you must first obtain a master Interop Assembly from the publisher to generate your master Interop assembly.
In addition, due to the time relationship, the content introduced by the COM-Type Library is programmed in the next article. Sorry.