PC Review


Reply
Thread Tools Rate Thread

COM Creation - Specified Cast is Not Valid

 
 
aljamala@gmail.com
Guest
Posts: n/a
 
      1st Sep 2006
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

 
Reply With Quote
 
 
 
 
Ignacio Machin \( .NET/ C# MVP \)
Guest
Posts: n/a
 
      1st Sep 2006
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


--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation



<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
aljamala@gmail.com
Guest
Posts: n/a
 
      1st Sep 2006
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.

 
Reply With Quote
 
Nicholas Paldino [.NET/C# MVP]
Guest
Posts: n/a
 
      4th Sep 2006
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.

--
- Nicholas Paldino [.NET/C# MVP]
- (E-Mail Removed)

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
explicit operator cast --> Specified cast is not valid Peter Microsoft C# .NET 1 24th Aug 2011 09:06 AM
specified cast is not valid planet Microsoft ASP .NET 1 22nd Sep 2006 06:43 PM
checking for valid cast before cast is called? =?Utf-8?B?d2dpbGxpbg==?= Microsoft Dot NET Framework 1 27th Jun 2005 10:29 PM
Specified cast is not valid Ruslan Shlain Microsoft ADO .NET 7 10th Feb 2004 08:01 PM
Specified cast is not valid - please help Tyro Microsoft ASP .NET 4 31st Oct 2003 01:07 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 09:21 PM.