C#中COM客户端和服务器出现InvalidCastException

本文关键字:InvalidCastException 服务器 COM 客户端 | 更新日期: 2023-09-27 18:27:24

我知道这个话题有很多问题,但我相信我在过去的10天里都解决了,我找不到解决我所面临的错误的方法。

我在C#中有一个COM服务器dll和一个在C#中的COM客户端。全部在Windows 7中。我收到InvalidCastException,无法解决问题。我开始怀疑用C#创建COM服务器的可能性。

我在实例化COM对象时遇到了这个异常,在:中

Test.MyImplementation mi = new Test.MyImplementation();

System.InvalidCastException未处理HResult=-2147467262消息=无法将类型为"MyTest.MyImplementation"的对象强制转换为类型为"Test.MyImplementation"。Source=控制台应用程序3StackTrace:位于c:''Users''rkohn''Documents''Visual Studio 2013''Projects''ConsoleApplication3''ConsoleApplication3''Program.cs:line 48中的ConsoleAppCOM.Program.Main(String[]args)位于System.AppDomain_nExecuteAssembly(RuntimeAssembly程序集,String[]参数)位于Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()位于System.Threading.ExecutionContext.RunInternal(ExecutionContext ExecutionContext,ContextCallback回调,对象状态,布尔值preserveSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,Object state,Boolean preserveSyncCtx)在System.Threading.ExecutionContext.Run(ExecutionContext ExecutionContext,ContextCallback回调,对象状态)位于System.Threading.ThreadHelper.ThreadStart()内部异常:

这是COM服务器代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Runtime.InteropServices;

namespace MyTest
{
    [ComVisible(true)]
    [Guid("DBE0E8C4-DABA-41F3-B6A4-CAFE353D3D16")]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    public interface IPimcManager
    {
        void GetTabletCount(out UInt32 count);
    }
    [ComVisible(true)]
    [Guid("C6659361-DABA-4746-931C-CAFE4B146690")]
    [ProgId("FakeServer.MyImplementation")]
    [ClassInterface(ClassInterfaceType.None)]
    [ComDefaultInterface(typeof(IPimcManager))] //This to explicitly establish which is the default interface
    public class MyImplementation : IPimcManager
    {
        public MyImplementation() { }
        ~MyImplementation() { }
        public void GetTabletCount(out UInt32 count)
        {
            Console.WriteLine("GetTabletCount called!");
            count = 1;
        }
    }
}

这是客户端代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
namespace Test
{
    [ComImport]
    [Guid(PimcConstants.IPimcManagerIID)]
    [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
    interface IPimcManager
    {
        void GetTabletCount(out UInt32 count);
    }
    [ComImport]
    [Guid(PimcConstants.PimcManagerCLSID)]
    class MyImplementation
    {
    }
    //        void GetTabletCount(out UInt32 count);
    //void GetTablet(UInt32 tablet, out IPimcTablet IPimcTablet);
    internal static class PimcConstants
    {
        //internal const string PimcManagerCLSID = "e23b1ced-5e47-4fdb-af66-b20370261b5e";
        internal const string PimcManagerCLSID = "C6659361-DABA-4746-931C-CAFE4B146690";
        internal const string IPimcManagerIID = "DBE0E8C4-DABA-41F3-B6A4-CAFE353D3D16";
        //internal const string PimcManagerCLSID = "c6659361-daba-4746-931c-cafe4b146690";
        //internal const string IPimcManagerIID = "dbe0e8c4-daba-41f3-b6a4-cafe353d3d16";
        //internal const string IPimcManagerIID = "af44bf80-36dd-4118-b4cf-8b1e3f4fb9ce";
    }
}
namespace ConsoleAppCOM
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            Test.MyImplementation mi = new Test.MyImplementation();
            Test.IPimcManager pimcManager = ((Test.IPimcManager)mi);

            uint cTablets = 0;
            pimcManager.GetTabletCount(out cTablets);
            System.Console.WriteLine(DateTime.Now + "-VALUE OBTAINED from PimcManager.GetTabletCount: " + cTablets);
            //Thread.Sleep(5);
            System.Console.ReadLine();
        }
    }
}

我已经尝试了STAThread,检查了服务器项目属性中的"注册COM互操作",检查了客户端和服务器都以x64为目标。。。

我用C++创建了一个COM服务器,同一个客户端与C++COM服务器完美配合。无InvalidCastException。

这是从C#FakeServer的类型库中获得的IDL:

// Generated .IDL file (by the OLE/COM Object Viewer)
// 
// typelib filename: FakeServer.tlb
[
  uuid(A3CFF4E2-8724-461F-AFD4-D74583E89513),
  version(1.0),
  custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, "FakeServer, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null")
]
library FakeServer
{
    // TLib :     // TLib : mscorlib.dll : {BED7F4EA-1A96-11D2-8F08-00A0C9A6186D}
    importlib("mscorlib.tlb");
    // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("stdole2.tlb");
    // Forward declare all types defined in this typelib
    interface IPimcManager;
    [
      odl,
      uuid(DBE0E8C4-DABA-41F3-B6A4-CAFE353D3D16),
      version(1.0),
      oleautomation,
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "MyTest.IPimcManager")    
    ]
    interface IPimcManager : IUnknown {
        HRESULT _stdcall GetTabletCount([out] unsigned long* count);
    };
    [
      uuid(C6659361-DABA-4746-931C-CAFE4B146690),
      version(1.0),
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "MyTest.MyImplementation")
    ]
    coclass MyImplementation {
        interface _Object;
        [default] interface IPimcManager;
    };
};

这是从C++COM服务器的类型库中获得的IDL(请不要考虑添加到接口中的Name方法,我在测试期间就这样做了):

// Generated .IDL file (by the OLE/COM Object Viewer)
// 
// typelib filename: simplecomserver.tlb
[
  uuid(6F818C55-E6AD-488B-9EB6-511C0CCC0612),
  version(1.0),
  custom(DE77BA64-517C-11D1-A2DA-0000F8773CE9, 134218331),
  custom(DE77BA63-517C-11D1-A2DA-0000F8773CE9, 1413900762),
  custom(DE77BA65-517C-11D1-A2DA-0000F8773CE9, "Created by MIDL version 8.00.0603 at Tue Oct 21 11:12:41 2014
")
]
library LibCOMServer
{
    // TLib :     // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("stdole2.tlb");
    // Forward declare all types defined in this typelib
    interface ICOMServer;
    [
      odl,
      uuid(7F24AABF-C822-4C18-9432-21433208F4DC),
      oleautomation
    ]
    interface ICOMServer : IUnknown {
        HRESULT _stdcall Name([out, retval] BSTR* objectname);
        HRESULT _stdcall GetTabletCount([out] unsigned long* pcTablets);
    };
    [
      uuid(6AE24C34-1466-482E-9407-90B98798A712),
      helpstring("COMServer object")
    ]
    coclass CoCOMServer {
        [default] interface ICOMServer;
    };
};

是否与从接口_Object;继承的C#类有任何关系;?

coclass MyImplementation {
    interface _Object;
    [default] interface IPimcManager;
};

感谢您的帮助。

C#中COM客户端和服务器出现InvalidCastException

也许MyImplementation需要实现IPimcManager(就像在服务器代码中一样)。尝试:

class MyImplementation : IPimcManager
{
    public void GetTabletCount(out UInt32 count)
    {
        // implement your code here
    }
}

希望这能有所帮助。

请尝试以下操作:

在客户端类声明中,而不是

[ComImport]
[Guid(PimcConstants.PimcManagerCLSID)]
class MyImplementation
{
}

将其更改为接口并使用父接口GUID:

[ComImport]
[Guid(PimcConstants.IPimcManagerIID)]
interface MyImplementation:IPimcManager
    {
    }

希望它能帮助

每当COM对象实例被转换为另一种接口类型时,InvalidCastException就会出现在.NET互操作代码中,就像在C++中调用QueryInterface一样,但由于您从IUnknown派生了接口,因此您还需要提供封送处理支持,这不是一项容易的任务,您所要做的就是从IDispath派生接口,内置的COM Marshaller oleaut32.dll可以满足您的封送处理需求,因为您是从IUnknown派生的,那么您还需要编写自己的自定义封送处理代码,更简单的做法是用IDispath 替换IUnknown

相关文章:
  • 没有找到相关文章