COM Creation - Specified Cast is Not Valid

  • Thread starter Thread starter aljamala
  • Start date Start date
A

aljamala

Hello,

I have the following method to help me in installing some COM
components onto the machine...below is a snippet that is causing the
problem...

Type objType = null;
COMAdminCatalog objCatalog = null;
COMAdminCatalogCollection objApplicationsColl = null;
COMAdminCatalogObject objApp = null;
try
{
objType =
Type.GetTypeFromProgID("COMAdmin.COMAdminCatalog");
objCatalog =
(COMAdminCatalog)(System.Activator.CreateInstance(objType));

It is part of setup application...so when I run the setup on my
machine, it works fine....

However, when I try to test it on one of our servers.....I get a
Specified Cast is Invalid exception from this line objCatalog =
(COMAdminCatalog)(System.Activator.CreateInstance(objType));

I am guessing its a problem with either one of the last two
line....forgive me, im new to COM

Thanks in advance
 
Hi,

The most probable reason is that System.Activator.CreateInstance(objType) is
returning null

or alternatively the instance returned cannot be casted to COMAdminCatalog

do this:

object o = System.Activator.CreateInstance(objType);
//LOG if o is null or not
//log o.GetType().ToString(); //to get the type of the instance created
 
I actually managed to come up with a solution (I think :S)...

I changed objCatalog from COMAdminCatalog to ICOMAdminCatalog for the
following reasons:

Sometimes you may find that casting a variable fails when you think it
should succeed (the solution is often to declare variables as interface
Types and avoid the use of class types, for example, use IStyleGallery
rather than StyleGalleryClass)

When you create a new COM object in .NET via interop, you get a
reference to your object that is wrapped in a strongly typed runtime
callable wrapper (RCW). A RCW is a wrapper that can hold a reference to
a COM object inside a .NET application.

Even if you actually know the exact Type of class to which you have a
reference, the .NET runtime still does not have the metadata required
to cast the variable to a strongly typed RCW.

However, as the System.__ComObject class is specifically designed to
work with COM objects, it is always able to perform a QI to any COM
interfaces that are implemented by an object. Therefore, casting to
specific interfaces (as long as they are implemented on the object)
will be successful.
 
When dealing with COM, you should always work with the interface
definitions, and cast to that. COM is an interface-based framework, and it
only makes sense to work with interfaces.

When you see class types that represent COM objects, they really don't
honor the way that COM works, as it is an amalgam of all the interfaces that
the type implements. This is close to how VB works, and to be quite honest,
I don't recommend it.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top