4

Its possibly im just missing something here but, when I write some code for Excel interop, here is how it goes.

  • I add a reference to the Excel Com libraries.
  • VS creates a PIA - Microsoft.Office.Interop.Excel....(via tlbimp right?).
  • I copy the exe and the interop(PIA) dll to any machine (with .net) and it works?

Is there a scenario where I would have to deploy/register the PIA? Or have I got something wrong here, because it seems to me embedding the PIA into the main assembly doesn't seem like a great big feature?

Please excuse my ignorance, if any.


Update:
So I did some tests, I wrote an app that opens excel adds "hello" in a cell and saves the file.

I built it on my Win7 Dev machine with Office 2003 installed(So I referenced 2003 libs). Interestingly, without embedded PIA's the app is 9KB (The 3 PIA's total upto 1.32MB). With embedded PIA's the exe is 13KB.

Secondly, with embedded PIA, the app worked on a machine with Office 2007 and 2010. And without embedded PIA, on WinXP+Office2007 it failed only when the PIA's were not in the exe's directory.

So I guess whatever method, there is some kind of dynamic resolution? And then why did it work on a Win7 without the PIA's in the exe directory, but on WinXP it failed (only when the PIA's were not in the exe's dir), did the Win7 box have the PIA's prolly deployed globally or something?

Thanks
Gideon

gideon
  • 19,329
  • 11
  • 72
  • 113
  • I only looked on this question to see what PIA stood for. Pain In The Ass in my book. The question reads well that way ;-). – PeteT Dec 10 '10 at 16:20
  • Looking back at your question, you never once used the PIA. The 2nd bullet created the interop library. What I described in my answer. – Hans Passant Dec 11 '10 at 08:09
  • @hans not sure here? PIA==InteropLib right? VS created the PIA and I used it in the exe. (Like I said, problem I encountered was without embedding, on one particular machine it complains it could not load the interop assembly) – gideon Dec 11 '10 at 09:16
  • 1
    No, a PIA is different. You have to download it from Microsoft. Embed and forget about it. – Hans Passant Dec 11 '10 at 09:21

3 Answers3

14

It's not that common to actually need a PIA. You have to have one if you expose any interop types from the Excel type library in one of your public classes. This goes wrong when other code uses your class and doesn't use the same interop library. A type in .NET is only identical when they came from the same assembly. You'd get a difficult to interpret error message like "Cannot cast Application to Application". The PIA ensures that everybody is using the same type. As long as everybody is using the same PIA version, which in itself is a difficult problem. Deploying your own interop DLL along with your app is fine if you can avoid this. Which is not difficult in most scenarios.

This problem was solved in .NET 4.0 through a feature called 'type equivalence'. It is specific to COM interface types, the CLR considers them compatible when they have the same [Guid] and the same declaration, regardless what assembly contains them. This was then taken advantage of with the 'embed interop types' feature (same as 'no pia'), the compiler embeds the interop types in the metadata of your assembly. Only the ones you actually use.

So you don't have to ship the interop library anymore and don't need the PIA. And it is a lot smaller since you only pay for the types you actually use. That's a lot of bang for the buck, strongly recommended.

Hans Passant
  • 922,412
  • 146
  • 1,693
  • 2,536
5

I haven't done much interop myself, but I believe that:

  • Sometimes the PIA can be quite large; if the app itself is quite small, the PIA can dwarf it
  • The no-PIA approach is more flexible with respect to versioning: so long as you only use the members provided by the version of the COM object which is actually provided, you're fine... whereas I think with the PIA approach, you need to use the PIA for the same version of the COM object as the one on the target machine
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • =) Respectfully, are you sure about `with the PIA approach, you need to use the PIA for the same version of the COM object as the one on the target machine` because Cindy Meister says _But if you have a reference to the 2003 PIAs when your application loads on a machine with 2007 PIAs, the references should be automatically re-routed_ from here : [http://social.msdn.microsoft.com/Forums/en-US/exceldev/thread/7e33adda-2bed-478f-979b-914447d1ce8d/#dd7dde5f-f172-41e4-84fa-4858c486878f] – gideon Dec 10 '10 at 15:54
  • @giddy: Nope, I'm not sure. But that's only one way round - what if you have a reference to the 2007 PIA, and there's only 2003 on the machine? I believe that would work with no-PIA so long as you haven't used any new features. – Jon Skeet Dec 10 '10 at 17:05
4

One of the key things to understand about NoPIA is that it does not embedd the PIA into your assembly but instead only embeds the portion of the PIA that your application uses. It does this in a very fine grained manner (all the way down to the method level). The result is usually a very significant reduction in the deployment size of your application.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454