在Word Interop中调用函数时,库中的类名被损坏

问题描述 投票:0回答:1

我的应用程序中有一个类库,我遇到了一些奇怪的问题。我们有几十个ComVisible类,最近我发现了一种情况,即通过COM公开的某个类的名称不再像以前那样出现过。

我能够在一个小型示例程序中复制该问题,该问题与Microsoft Word Interop中涉及的单行有关。具体来说,我有一个类窗口。通常这个类是ComVisible的'Window',但如果我在Word Interop中引用Document.ActiveWindow属性,我的类将变为ComVisible为'TestLibrary_Window'。在我的实际应用程序中,我有几百个我引用Window的地方,我不想将它们全部更改为TestLibrary_Window,而且我想通过引用一个属性改变我的库暴露的方式来理解可能会发生什么。本身。

我可以通过使用程序OleWoo(http://www.benf.org/other/olewoo/)查看TLB文件来轻松演示结果。请注意,在结果1中,您会看到coclass窗口的条目,但在结果2中,您会看到coclass TestLibrary_Window的条目。结果1是我期望TLB遇到的方式,如果我的代码中的故障行被评论,这就是我收到的。结果2是我取消注释故障线时得到的结果。

以下是重复我的问题的最小实现。如果TestClass中的注释行被注释而不是我没有问题,但是如果我取消注释该行我有问题。请注意,在我的示例代码中,我不需要Window类中的任何代码来演示该问题。

文件1:TestClass.vb

Imports System.Runtime.InteropServices

<ComVisible(True)>
Public Class TestClass
    Public Sub testFunction()
        Dim oWord As Microsoft.Office.Interop.Word.Application = CreateObject("Word.Application")
        Dim oDoc As Microsoft.Office.Interop.Word.Document = oWord.Documents.Open("c:\temp\test.docx")

        'trouble line
        'oDoc.ActiveWindow.View.TableGridlines = True

        oDoc.Save()
    End Sub

End Class

文件2:Window.vb

Imports System.Runtime.InteropServices

<ComVisible(True)>
Public Class Window

End Class

结果1:正确的TLB

// Generated .IDL file (by OleWoo)
[
  uuid(b2effb21-a565-4092-bc8f-b92aa429952a),
  version(1.0),
  custom(90883f05-3d28-11d2-8f17-00a0c9a6186d, "TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=974b55dd4adecdf1")
]
library TestLibrary
{
    // Forward declare all types defined in this typelib
    dispinterface _TestClass
    interface _TestClass
    dispinterface _Window
    interface _Window
    [
      uuid(eb22957e-07c0-34b2-b813-48d0e9376d35)
    ]
    coclass TestClass {
        [default] interface _TestClass#i;
        interface _Object#i;
    };

    [
      uuid(2266afaa-2145-3508-bb4b-9f8579112b14)
    ]
    coclass Window {
        [default] interface _Window#i;
        interface _Object#i;
    };

    [
      uuid(a13ff8b0-ac7c-33e5-b0f3-5366304512ac),
      hidden,
      dual,
      oleautomation
    ]
    interface _TestClass : IDispatch#i {

    };

    [
      uuid(b81f8ed9-9e71-3248-b3a9-b7a104b3a597),
      hidden,
      dual,
      oleautomation
    ]
    interface _Window : IDispatch#i {

    };

};

结果2:错误的TLB文件

// Generated .IDL file (by OleWoo)
[
  uuid(b2effb21-a565-4092-bc8f-b92aa429952a),
  version(1.0),
  custom(90883f05-3d28-11d2-8f17-00a0c9a6186d, "TestLibrary, Version=1.0.0.0, Culture=neutral, PublicKeyToken=974b55dd4adecdf1")
]
library TestLibrary
{
    // Forward declare all types defined in this typelib
    dispinterface _TestClass
    interface _TestClass
    dispinterface _TestLibrary_Window
    interface _TestLibrary_Window
    [
      uuid(eb22957e-07c0-34b2-b813-48d0e9376d35)
    ]
    coclass TestClass {
        [default] interface _TestClass#i;
        interface _Object#i;
    };

    [
      uuid(2266afaa-2145-3508-bb4b-9f8579112b14)
    ]
    coclass TestLibrary_Window {
        [default] interface _TestLibrary_Window#i;
        interface _Object#i;
    };

    [
      uuid(a13ff8b0-ac7c-33e5-b0f3-5366304512ac),
      hidden,
      dual,
      oleautomation
    ]
    interface _TestClass : IDispatch#i {

    };

    [
      uuid(b81f8ed9-9e71-3248-b3a9-b7a104b3a597),
      hidden,
      dual,
      oleautomation
    ]
    interface _TestLibrary_Window : IDispatch#i {

    };

};
vb.net visual-studio dll office-interop class-library
1个回答
0
投票

正如@TnTinMn在上面的评论中提到的,我遇到的问题确实与Microsoft.Office.Interop.Word的引用中的“嵌入互操作类型”有关。通过将此选项切换为false,我的TLB现在正按预期创建。

© www.soinside.com 2019 - 2024. All rights reserved.