Scott Hanselman

"Gotchas" around Primary Interop Assemblies and the GAC

June 10, '03 Comments [0] Posted in Web Services | Tools
Sponsored By

A couple of things to be aware about when working with PIAs, particularly when deploying them.  PIAs provide a unique type identity for COM Components.  They provide a single place for your .NET app to interop with existing COM objects.  They get marked with the PrimaryInteropAssembly attribute...if there's any question about an assembly, you can ILDASM it and see the PrimaryInteropAssembly attribute in the Manifest. 

You can create a "vanilla" PIA with TLBIMP.exe like this (note the /primary key):

TlbImp MyCom.tlb /primary /keyfile:MyKey.snk /out:MyPIALib.dll

If you want to create a custom PIA, perhaps to marshal types a specific way you'll apply the attributes from source:

[assembly:Guid("xxxxxxx-xxxx-xxx-xx-xxxx-xxxx"]
[assembly:PrimaryInteropAssembly(3, 1)] //Version 3.1

Of course the COM Object that you're referring to has to be registered, but the PIA has to be as well.  You'll need to call RegASM.exe to create a key in the registry under HKEY_CLASSES_ROOT\TypeLib\{yourclassid}\PrimaryInteropAssemblyName.  The value of this key should be the fully qualified Assembly name like: MYDLL, Version=7.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a.  Some folks call RegASM, others ship a .REG file or create this key during the installation procedure.

To recap:

  • Register COM Object (if not already)
  • Create InteropAssembly with TLBIMP.EXE and /primary (make sure it's strongly named)
  • RegASM the resulting InteropAssembly
  • Put the PIA in the GAC

Note also that when referencing your PIAs in Visual Studio.NET via "Add Reference..." you won't find your PIA in the .NET Tab as some may think.  Just select the original COM TLB/DLL reference from the COM Tab and Visual Studio will silently search for and use the PIA.  You can confirm this by looking at the added reference in the Properties Toolbox and confirming that it's strongly named.

Also check out Sam Gentile's article about Programmating Determining if a PIA in in the GAC and Mattias' page on the Fusion APIs.

About Scott

Scott Hanselman is a former professor, former Chief Architect in finance, now speaker, consultant, father, diabetic, and Microsoft employee. He is a failed stand-up comic, a cornrower, and a book author.

facebook twitter subscribe
About   Newsletter
Sponsored By
Hosting By
Dedicated Windows Server Hosting by SherWeb
Comments are closed.

Disclaimer: The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.