Class 在 Word Interop 中调用函数时库中的名称被破坏

Class name in a Library being mangled when calling functions in Word Interop

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

我能够在一个小示例程序中重现该问题,该问题与 Microsoft Word Interop 中涉及的一行有关。具体来说,我有一个 class Window。通常这个 class 是 ComVisible 作为 'Window',但是如果我在 Word Interop 中引用 Document.ActiveWindow 属性 我的 class 变成 ComVisible 作为 'TestLibrary_Window'。在我的实际应用程序中,我有 100 多个地方引用 Window,我不想将它们全部更改为 TestLibrary_Window,而且我想通过引用了解可能发生的情况到一个 属性 改变我的图书馆展示自己的方式。

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

以下是重复我的问题的最小实现。如果 TestClass 中的注释行留下注释,那么我没有问题,但是如果我取消注释该行,我就会遇到问题。请注意,在我的示例代码中,我不需要 Window class 中的任何代码来演示该问题。

文件 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 {

    };

};

正如@TnTinMn 在上面的评论中提到的,我遇到的问题确实与 Microsoft.Office.Interop.Word 中的 'Embed Interop Types' 有关。通过将此选项切换为 false,我的 TLB 现在正在按预期创建。