简体   繁体   中英

InvalidCastException: Unable to cast COM object of type x to interface type y

I have some C# code written on machine #1 using Visual C# Express 2010. The code is dependent on some COM objects that are registered DLLs. It compiles and runs fine on machine #1. The COM objects are installed from a program called BostonWorkstation, which is installed on machines #1 and #2.

  • Machine 1:
    • Code written on.
    • Visual C# 2010 Express installed.
    • The "BWS" DLLs are registered.
    • The program runs just fine.
    • Has .NET versions: v4.0, v4, v3.5, v3.0, v2.0
    • 32bit


  • Machine 2:
    • The "BWS" DLLs are registered.
    • The program errors with System.InvalidCastException (below).
    • Visual C# 2010 Express not installed.
    • Has .NET versions: v4, v3.5, v3.0, v2.0, v1.1
    • 32bit


  • Machine 3:
    • The "BWS" DLLs are not registered.
    • Visual C# 2010 Express installed.
    • The program errors (as expected) with: System.Runtime.InteropServices.COMException (...): Retrieving the COM class factory for component with CLSID {...} failed due to the following error: 80040154 Class not registered (...)
    • 32bit

When I try to run that program (compiled on machine 1) on machine #2 which also has the same exact registered DLLs, but the program fails to run. I also tried running it on machine #3, which doesn't even have the DLLs and that gets a different error as expected.

The runtime error on machine 2:

1 Unable to load BostonWorkstation, error: System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'BostonWorkStation70.BostonWorkStation'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{8C8144EF-ADB7-48FD-A5BB-6E55B8382B3E}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)). at TestBWS.LoginForm() in C:\\Documents and Settings\\user\\My Documents\\Visual Studio 2010\\Projects\\project1\\TestBWS\\CodeFile1.cs:line 67

(The Visual Studio folder reference is just the compile time path)

Checking the registry on machine 1 I found this:

HKEY_CLASSES_ROOT\\Interface{8C8144EF-ADB7-48FD-A5BB-6E55B8382B3E}

with a default REG_SZ value of BostonWorkStation

The code:

    using BostonWorkStation70; //line 1

    ...

    try
    {
        bwsLogin = new BostonWorkStation(); //line 67
    }
    catch (Exception e)
    {
        Console.WriteLine("1 Unable to load BostonWorkstation, error: " + e);
        System.Console.ReadLine(); //pause the debug window
        return false;
    }

I have tried running ProcMon to compare the machines Process logs, but there are thousands of entries for just this program and I'm not seeing any missing references offhand. I ran a compare, but a successful Process log has 5x as many lines.

From what I understand, running the program on machine 2 has it creating the BWS object as a generic System.__ComObject (instead of BostonWorkStation70.BostonWorkStation ) and I don't know why. The DLLs etc are definitely correct on machine 2, otherwise I'd be getting that Class not registered error on runtime.

Why will the compiled program not run on machine 2? All the dependent COMs/DLLs are on it.

if that error message is coming from machine #2, the one you said "Visual C# 2010 Express not installed."

why is it running from the default visual studio projects folder?

C:\\Documents and Settings\\user\\My Documents\\Visual Studio 2010\\Projects\\project1\\TestBWS\\CodeFile1.cs:line 67

You may have other problems....

Have you registered your COM object class from the library using regsvr32.exe on second machine?

To check that I'd search the registry on first and second machines looking for all possible {8C8144EF-ADB7-48FD-A5BB-6E55B8382B3E} string entries. If search results are different then it's probably the case.

If the BostonWorkstation components are unmanaged - open them up in dependency walker to make sure that all their dependent dlls are present.

Odds are they are unmanaged components and make use of the MSVCRT 10. In which case you will need to install the VS 2010 CRT on machine 2.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM