Interop Error "Specified cast is not valid"

Hello:
 
I just want to get some help from the MVP's here to clarify my perception is correct for this problem.
 
I am loading two c# addins on a COM server.
 
Both the addins are using two different PIA's (different version) of the com application s.
 
Now when the application starts, and it start to load addin1, addin1 creates a command  or rather in straight words adds another object to collection of existing objects, it works fine and loads this addin1 without any issues.
 
Now when my second addin, addin2, starts to load: it fails to load by throwing an error that says Specified cast is not valid
I know the reason why it happens:- when the addin2 is being loaded and when I loop over the command object (of the base com app) (to see if the command that I am creating is already existing) and since this addin2 is not able to find the metadata associated with the new command object (that addin1 added) QI fails and throws an error saying that the specific cast is invalid.  Even though at this time System.__comobject class knows the class type but its not able to find the metadata for the object type thats created during addin1 load.  So at this time instead of using the class I should rather be casting my objects to their specific interface to solve this problem.
 
Do you think that my perception of this problem and my solution is correct
 
Thanks in advance,
Vivek


Answer this question

Interop Error "Specified cast is not valid"

  • BLIS

    My perception was somehow somewhere correct but not fully. What happens is when there are two PIA's that I load from two seperate addins (and none of these pia's are in gac and not registered) .net framework creates two RCW's for it. The first addin loads fine because the first assembly(i mean pia) loads fine but as soon as the second addin loads it creates another RCW since this PIA is a diffrent version(well I dont exactly know why but this is what I have understood).

    So what I needed to do was to create a PIA for the latest version and then modify the IL to add the backward compatibility for the previous versions and then register it(using regasm) and then cache it using gacutil. And use these PIAs to reference in my addins instead of the PIA of their own.

    Read this article (and do not do the same mistake that I did ...there are lines here that I did not pay attention to and so kept on running around in loops) http://msdn.microsoft.com/library/default.asp url=/library/en-us/cpguide/html/cpcongeneratingprimaryinteropassemblies.asp

    After using all this just reference the assembly from the COM tab on the add reference dialog and rest VS takes care of referencing the assembly from gac(basically <your typelib guid>/<version>/PrimaryInteropAssemblyName in HKEY_CLASSES_ROOT\TypeLib

    There is more to it but all that info you can get from MSDN. I could not find any decent explaination of this problem so I thought let me pen it down so that someone else can benifit from this.


  • Interop Error "Specified cast is not valid"