简体   繁体   English

InvalidCastException:无法将类型x的COM对象转换为接口类型y

[英]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. 我使用Visual C#Express 2010在计算机#1上编写了一些C#代码。该代码取决于某些已注册DLL的COM对象。 It compiles and runs fine on machine #1. 它可以在#1机器上编译并正常运行。 The COM objects are installed from a program called BostonWorkstation, which is installed on machines #1 and #2. COM对象是从名为BostonWorkstation的程序安装的,该程序安装在#1和#2机器上。

  • Machine 1: 机器1:
    • Code written on. 编写代码。
    • Visual C# 2010 Express installed. 已安装Visual C#2010 Express。
    • The "BWS" DLLs are registered. 已注册“ BWS” DLL。
    • The program runs just fine. 该程序运行正常。
    • Has .NET versions: v4.0, v4, v3.5, v3.0, v2.0 拥有.NET版本:v4.0,v4,v3.5,v3.0,v2.0
    • 32bit 32位


  • Machine 2: 机器2:
    • The "BWS" DLLs are registered. 已注册“ BWS” DLL。
    • The program errors with System.InvalidCastException (below). System.InvalidCastException出现程序错误(如下)。
    • Visual C# 2010 Express not installed. 未安装Visual C#2010 Express。
    • Has .NET versions: v4, v3.5, v3.0, v2.0, v1.1 拥有.NET版本:v4,v3.5,v3.0,v2.0,v1.1
    • 32bit 32位


  • Machine 3: 机器3:
    • The "BWS" DLLs are not registered. 未注册“ BWS” DLL。
    • Visual C# 2010 Express installed. 已安装Visual C#2010 Express。
    • 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 (...) 由于以下错误,程序错误(按预期): System.Runtime.InteropServices.COMException (...): Retrieving the COM class factory for component with CLSID {...} failed due to the following error: 80040154 Class not registered (...)
    • 32bit 32位

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. 当我尝试在还具有相同确切注册的DLL的计算机#2上运行该程序(在计算机1上编译)时,但是该程序无法运行。 I also tried running it on machine #3, which doesn't even have the DLLs and that gets a different error as expected. 我还尝试在3号机上运行它,该3号机甚至没有DLL,并且按预期会出现不同的错误。

The runtime error on machine 2: 机器2上的运行时错误:

1 Unable to load BostonWorkstation, error: System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'BostonWorkStation70.BostonWorkStation'. 1无法加载BostonWorkstation,错误:System.InvalidCastException:无法将类型为“ System .__ ComObject”的COM对象转换为接口类型为“ 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)). 此操作失败,因为对IID为“ {8C8144EF-ADB7-48FD-A5BB-6E55B8382B3E}”的接口的COM组件上的QueryInterface调用由于以下错误而失败:不支持此类接口(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 在C:\\ Documents and Settings \\ user \\ My Documents \\ Visual Studio 2010 \\ Projects \\ project1 \\ TestBWS \\ CodeFile1.cs中的TestBWS.LoginForm()中:第67行

(The Visual Studio folder reference is just the compile time path) (Visual Studio文件夹引用只是编译时间路径)

Checking the registry on machine 1 I found this: 检查计算机1上的注册表,我发现了这一点:

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

with a default REG_SZ value of BostonWorkStation REG_SZ的默认值为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. 我曾尝试运行ProcMon来比较机器的进程日志,但是仅此程序就有成千上万的条目,而且我看不到任何丢失的引用。 I ran a compare, but a successful Process log has 5x as many lines. 我进行了比较,但成功的Process日志的行数是原来的5倍。

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. 据我了解,在机器2上运行程序可以将BWS对象创建为通用System.__ComObject (而不是BostonWorkStation70.BostonWorkStation ),我也不知道为什么。 The DLLs etc are definitely correct on machine 2, otherwise I'd be getting that Class not registered error on runtime. DLL等绝对在机器2上是正确的,否则我将在运行时得到Class not registered错误。

Why will the compiled program not run on machine 2? 为什么编译后的程序不能在计算机2上运行? All the dependent COMs/DLLs are on it. 所有相关的COM / DLL都在上面。

if that error message is coming from machine #2, the one you said "Visual C# 2010 Express not installed." 如果该错误消息来自2号计算机,则您说“未安装Visual C#2010 Express”。

why is it running from the default visual studio projects folder? 为什么要从默认的Visual Studio项目文件夹运行?

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

You may have other problems.... 您可能还有其他问题。

Have you registered your COM object class from the library using regsvr32.exe on second machine? 您是否已在第二台计算机上使用regsvr32.exe从库中注册了COM对象类?

To check that I'd search the registry on first and second machines looking for all possible {8C8144EF-ADB7-48FD-A5BB-6E55B8382B3E} string entries. 要检查是否在第一台和第二台计算机上搜索注册表,以查找所有可能的{8C8144EF-ADB7-48FD-A5BB-6E55B8382B3E}字符串条目。 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. 如果BostonWorkstation组件不受管理,请在依赖关系查看器中打开它们,以确保存在所有其依赖的dll。

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. 可能是它们是不受管理的组件,并使用了MSVCRT10。在这种情况下,您将需要在计算机2上安装VS 2010 CRT

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 System.InvalidCastException:无法将类型为x的对象强制转换为y - System.InvalidCastException: Unable to cast object of type x to type y 无法将 x 类型的 object 转换为 y 类型 - Unable to cast object of type x to type y System.InvalidCastException:无法将类型为“System .__ ComObject”的COM对象强制转换为接口类型“System.Collections.IEnumerable” - System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'System.Collections.IEnumerable' System.InvalidCastException:无法转换类型的对象 - System.InvalidCastException: Unable to cast object of type 使用ExeCOMServer时,无法将类型为“...”的COM对象转换为接口类型“...” - Unable to cast COM object of type '…' to interface type '…' while using an ExeCOMServer :'无法将类型为'System .__ ComObject'的COM对象转换为接口类型 - : 'Unable to cast COM object of type 'System.__ComObject' to interface type InvalidCastException-无法转换类型为“ Microsoft.Office.Interop.Outlook.ApplicationClass”的COM对象 - InvalidCastException - Unable to cast COM object of type 'Microsoft.Office.Interop.Outlook.ApplicationClass' System.InvalidCastException: '无法将类型为 'ClassName' 的对象转换为类型 InterfaceName'。 - System.InvalidCastException: 'Unable to cast object of type 'ClassName' to type InterfaceName'.' 无法将“X”类型的对象强制转换为“X” - Unable to cast object of type 'X' to type 'X' 无法将类型'CommunicatorAPI.MessengerClass'的COM对象转换为接口类型'CommunicatorAPI.IMessengerAdvanced' - Unable to cast COM object of type 'CommunicatorAPI.MessengerClass' to interface type 'CommunicatorAPI.IMessengerAdvanced'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM