C# - 写一个COM服务器 - 映射到方法属性属性、服务器、方法、COM

2023-09-04 02:19:16 作者:深情不及久伴

我们正在努力,以取代原来的VB6应用程序编写的COM服务器

We are trying to replace a COM server originally written for a VB6 application

我们必须源$ C ​​$ C进不去。

We have no access to source code.

由于某些原因,VB6的应用程序可以调用我们的构造,但它得到:

For some reason, the VB6 app can call our constructor, but then it gets:

系统错误和放大器; H80004002。没有这样的   接口的支持。

System Error &H80004002. No such interface supported.

我认为当它试图让与的QueryInterface接口。

I assume when it tries to get the interface with QueryInterface.

我们有我们的组件通过适当regasm / TLB和GACUTIL发,但后来我发现了一些奇怪的。我打开我们的组件产生.tlb文件regasm,并注意到在我的界面的所有属性显示为方法。

We have our assembly properly sent through regasm /tlb and gacutil, but I then noticed something strange. I opened the .tlb file regasm generated for our assembly, and noticed all the properties on my interface show up as methods.

这是不是对.Net工具在做什么?而这会导致我的问题?

Is this something the .Net tools are doing? And would this cause my issue?

下面是原始文件的完整TLB定义:

Here is the full tlb definition for the original file:

// Generated .IDL file (by the OLE/COM Object Viewer)
// 
// typelib filename: UtopiaKeyboard.tlb

[
  uuid(9B4E1840-FF65-11CF-AA2E-0020AFA49D5A),
  version(1.0),
  helpstring("OPOS POSKeyboard OLE Control")
]
library POSKEYBOARDLib
{
    // TLib :     // TLib : OLE Automation : {00020430-0000-0000-C000-000000000046}
    importlib("STDOLE2.TLB");

    // Forward declare all types defined in this typelib
    dispinterface _DPOSKeyboard;
    dispinterface _DPOSKeyboardEvents;

    [
      uuid(9B4E1841-FF65-11CF-AA2E-0020AFA49D5A),
      helpstring("Dispatch interface for OPOS POSKeyboard OLE Control"),
      hidden
    ]
    dispinterface _DPOSKeyboard {
        properties:
            [id(0x00000001)            
]
            BSTR CheckHealthText;
            [id(0x00000002)            
]
            VARIANT_BOOL Claimed;
            [id(0x00000003)            
]
            VARIANT_BOOL DataEventEnabled;
            [id(0x00000004)            
]
            VARIANT_BOOL DeviceEnabled;
            [id(0x00000005)            
]
            VARIANT_BOOL FreezeEvents;
            [id(0x00000006)            
]
            long ResultCode;
            [id(0x00000007)            
]
            long ResultCodeExtended;
            [id(0x00000008)            
]
            long State;
            [id(0x00000009)            
]
            BSTR ControlObjectDescription;
            [id(0x0000000a)            
]
            long ControlObjectVersion;
            [id(0x0000000b)            
]
            BSTR ServiceObjectDescription;
            [id(0x0000000c)            
]
            long ServiceObjectVersion;
            [id(0x0000000d)            
]
            BSTR DeviceDescription;
            [id(0x0000000e)            
]
            BSTR DeviceName;
            [id(0x0000000f)            
]
            long POSKeyData;
            [id(0x0000001a)            
]
            VARIANT_BOOL AutoDisable;
            [id(0x0000001b)            
]
            long BinaryConversion;
            [id(0x0000001c)            
]
            long DataCount;
            [id(0x0000001d)            
]
            VARIANT_BOOL CapKeyUp;
            [id(0x0000001e)            
]
            long EventTypes;
            [id(0x0000001f)            
]
            long POSKeyEventType;
            [id(0x00000021)            
]
            long CapPowerReporting;
            [id(0x00000022)            
]
            long PowerNotify;
            [id(0x00000023)            
]
            long PowerState;
        methods:
            [id(0x00000010)]
            long Open([in] BSTR DeviceName);
            [id(0x00000011)]
            long Close();
            [id(0x00000012)]
            long Claim([in] long Timeout);
            [id(0x00000013)]
            long Release();
            [id(0x00000014)]
            long CheckHealth([in] long Level);
            [id(0x00000015)]
            long ClearInput();
            [id(0x00000016)]
            long DirectIO(
                            [in] long Command, 
                            [in, out] long* pData, 
                            [in, out] BSTR* pString);
            [id(0x00000017)]
            void SOData([in] long Status);
            [id(0x00000018)]
            void SODirectIO(
                            [in] long EventNumber, 
                            [in, out] long* pData, 
                            [in, out] BSTR* pString);
            [id(0x00000019)]
            void SOError(
                            [in] long ResultCode, 
                            [in] long ResultCodeExtended, 
                            [in] long ErrorLocus, 
                            [in, out] long* pErrorResponse);
            [id(0x00000020)]
            long SOProcessID();
            [id(0x00000024)]
            void SOStatusUpdate([in] long Status);
    };

    [
      uuid(9B4E1842-FF65-11CF-AA2E-0020AFA49D5A),
      helpstring("Event interface for OPOS POSKeyboard OLE Control")
    ]
    dispinterface _DPOSKeyboardEvents {
        properties:
        methods:
            [id(0x00000001)]
            void DataEvent([in] long Status);
            [id(0x00000002)]
            void DirectIOEvent(
                            [in] long EventNumber, 
                            [in, out] long* pData, 
                            [in, out] BSTR* pString);
            [id(0x00000003)]
            void ErrorEvent(
                            [in] long ResultCode, 
                            [in] long ResultCodeExtended, 
                            [in] long ErrorLocus, 
                            [in, out] long* pErrorResponse);
            [id(0x00000004)]
            void StatusUpdateEvent([in] long Status);
    };

    [
      uuid(9B4E1843-FF65-11CF-AA2E-0020AFA49D5A),
      helpstring("OPOS POSKeyboard OLE Control"),
      control
    ]
    coclass POSKeyboard {
        [default] dispinterface _DPOSKeyboard;
        [default, source] dispinterface _DPOSKeyboardEvents;
    };
};

下面是产生TLB我的.NET程序集:

Here is the generate tlb for my .Net assembly:

// Generated .IDL file (by the OLE/COM Object Viewer)
// 
// typelib filename: POSKEYBOARDLib.tlb

[
  uuid(9B4E1840-FF65-11CF-AA2E-0020AFA49D5A),
  version(1.0),
  custom(90883F05-3D28-11D2-8F17-00A0C9A6186D, "POSKEYBOARDLib, Version=1.0.0.0, Culture=neutral, PublicKeyToken=5824208931aaf68b")

]
library POSKEYBOARDLib
{
    // 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
    dispinterface _DPOSKeyboard;
    dispinterface _DPOSKeyboardEvents;

    [
      uuid(9B4E1841-FF65-11CF-AA2E-0020AFA49D5A),
      version(1.0),
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "POSKEYBOARDLib._DPOSKeyboard")    

    ]
    dispinterface _DPOSKeyboard {
        properties:
        methods:
            [id(0x00000001), propget]
            BSTR CheckHealthText();
            [id(0x00000001), propput]
            void CheckHealthText([in] BSTR rhs);
            [id(0x00000002), propget]
            VARIANT_BOOL Claimed();
            [id(0x00000002), propput]
            void Claimed([in] VARIANT_BOOL rhs);
            [id(0x00000003), propget]
            VARIANT_BOOL DataEventEnabled();
            [id(0x00000003), propput]
            void DataEventEnabled([in] VARIANT_BOOL rhs);
            [id(0x00000004), propget]
            VARIANT_BOOL DeviceEnabled();
            [id(0x00000004), propput]
            void DeviceEnabled([in] VARIANT_BOOL rhs);
            [id(0x00000005), propget]
            VARIANT_BOOL FreezeEvents();
            [id(0x00000005), propput]
            void FreezeEvents([in] VARIANT_BOOL rhs);
            [id(0x00000006), propget]
            long ResultCode();
            [id(0x00000006), propput]
            void ResultCode([in] long rhs);
            [id(0x00000007), propget]
            long ResultCodeExtended();
            [id(0x00000007), propput]
            void ResultCodeExtended([in] long rhs);
            [id(0x00000008), propget]
            long State();
            [id(0x00000008), propput]
            void State([in] long rhs);
            [id(0x00000009), propget]
            BSTR ControlObjectDescription();
            [id(0x00000009), propput]
            void ControlObjectDescription([in] BSTR rhs);
            [id(0x0000000a), propget]
            long ControlObjectVersion();
            [id(0x0000000a), propput]
            void ControlObjectVersion([in] long rhs);
            [id(0x0000000b), propget]
            BSTR ServiceObjectDescription();
            [id(0x0000000b), propput]
            void ServiceObjectDescription([in] BSTR rhs);
            [id(0x0000000c), propget]
            long ServiceObjectVersion();
            [id(0x0000000c), propput]
            void ServiceObjectVersion([in] long rhs);
            [id(0x0000000d), propget]
            BSTR DeviceDescription();
            [id(0x0000000d), propput]
            void DeviceDescription([in] BSTR rhs);
            [id(0x0000000e), propget]
            BSTR DeviceName();
            [id(0x0000000e), propput]
            void DeviceName([in] BSTR rhs);
            [id(0x0000000f), propget]
            long POSKeyData();
            [id(0x0000000f), propput]
            void POSKeyData([in] long rhs);
            [id(0x00000010)]
            long Open([in] BSTR DeviceName);
            [id(0x00000011)]
            long Close();
            [id(0x00000012)]
            long Claim([in] long Timeout);
            [id(0x00000013)]
            long Release();
            [id(0x00000014)]
            long CheckHealth([in] long Level);
            [id(0x00000015)]
            long ClearInput();
            [id(0x00000016)]
            long DirectIO(
                            [in] long Command, 
                            [in, out] long* pData, 
                            [in, out] BSTR* pString);
            [id(0x00000017)]
            void SOData([in] long Status);
            [id(0x00000018)]
            void SODirectIO(
                            [in] long EventNumber, 
                            [in, out] long* pData, 
                            [in, out] BSTR* pString);
            [id(0x00000019)]
            void SOError(
                            [in] long ResultCode, 
                            [in] long ResultCodeExtended, 
                            [in] long ErrorLocus, 
                            [in, out] long* pErrorResponse);
            [id(0x0000001a), propget]
            VARIANT_BOOL AutoDisable();
            [id(0x0000001a), propput]
            void AutoDisable([in] VARIANT_BOOL rhs);
            [id(0x0000001b), propget]
            long BinaryConversion();
            [id(0x0000001b), propput]
            void BinaryConversion([in] long rhs);
            [id(0x0000001c), propget]
            long DataCount();
            [id(0x0000001c), propput]
            void DataCount([in] long rhs);
            [id(0x0000001d), propget]
            VARIANT_BOOL CapKeyUp();
            [id(0x0000001d), propput]
            void CapKeyUp([in] VARIANT_BOOL rhs);
            [id(0x0000001e), propget]
            long EventTypes();
            [id(0x0000001e), propput]
            void EventTypes([in] long rhs);
            [id(0x0000001f), propget]
            long POSKeyEventType();
            [id(0x0000001f), propput]
            void POSKeyEventType([in] long rhs);
            [id(0x00000020)]
            long SOProcessID();
            [id(0x00000021), propget]
            long CapPowerReporting();
            [id(0x00000021), propput]
            void CapPowerReporting([in] long rhs);
            [id(0x00000022), propget]
            long PowerNotify();
            [id(0x00000022), propput]
            void PowerNotify([in] long rhs);
            [id(0x00000023), propget]
            long PowerState();
            [id(0x00000023), propput]
            void PowerState([in] long rhs);
            [id(0x00000024)]
            void SOStatusUpdate([in] long Status);
    };

    [
      uuid(9B4E1842-FF65-11CF-AA2E-0020AFA49D5A),
      version(1.0),
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "POSKEYBOARDLib._DPOSKeyboardEvents")    

    ]
    dispinterface _DPOSKeyboardEvents {
        properties:
        methods:
            [id(0x00000001)]
            void DataEvent([in] long Status);
            [id(0x00000002)]
            void DirectIOEvent(
                            [in] long EventNumber, 
                            [in, out] long* pData, 
                            [in, out] BSTR* pString);
            [id(0x00000003)]
            void ErrorEvent(
                            [in] long ResultCode, 
                            [in] long ResultCodeExtended, 
                            [in] long ErrorLocus, 
                            [in, out] long* pErrorResponse);
            [id(0x00000004)]
            void StatusUpdateEvent([in] long Status);
    };

    [
      uuid(9B4E1843-FF65-11CF-AA2E-0020AFA49D5A),
      version(1.0),
      custom(0F21F359-AB84-41E8-9A78-36D110E6D2F9, "POSKEYBOARDLib.POSKeyboard")
    ]
    coclass POSKeyboard {
        interface _Object;
        [default] dispinterface _DPOSKeyboard;
        [default, source] dispinterface _DPOSKeyboardEvents;
    };
};

不知怎的,我的属性被列为方法......即使我添加preserveSig,但仍写使它们的属性2种方法吧。

Somehow my properties are listed as methods... Even if I add PreserveSig, it still writes 2 methods instead of making them properties.

我应该尝试什么样的变化,使其与原始TLB?或者至少使其发挥作用。

What changes should I try to make it identical to the original tlb? Or at least make it work.

更新:发现在那里我没有DispIdAttribute在我的事件接口。修正了,但VB6应用程序仍然失败,出现同样的错误。

UPDATE: Found where I did not have DispIdAttribute on my events interface. Fixed that, but the VB6 app still fails with the same error.

推荐答案

嗯,我想它了...

我反编译与目标应用程序VB6的反编译器,并导出为一个VB6项目。

I decompiled with a vb6 decompiler on the target app and exported as a vb6 project.

2008年VS打开一看,升级为VB .NET。

Opened it with VS 2008, upgraded to VB .Net.

运行应用程序,看到错误无法转换为的IOleObject。

Ran the application and saw the error "cannot cast to IOleObject".

做了谷歌搜索,发现了 System.Windows.Forms.Control的实现了这个接口,从控制等等继承固定我的问题。

Did a google search, found out that System.Windows.Forms.Control implements this interface, so inheriting from Control fixed my issue.