代码之家  ›  专栏  ›  技术社区  ›  Ben Reisner

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

  •  0
  • Ben Reisner  · 技术社区  · 6 年前

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

    通过使用OleWoo程序查看TLB文件,我可以很容易地演示结果( http://www.benf.org/other/olewoo/ ). 注意,在结果1中,您会看到coclass窗口的条目,但在结果2中,您会看到coclass TestLibrary\u窗口的条目。结果1是我期望TLB遇到的情况,如果代码中的故障行被注释,这就是我收到的结果。结果2是当我取消注释故障行时得到的结果。

    下面是一个与我的问题重复的最小实现。如果TestClass中的注释行被留下注释,那么我就没有问题,但是如果我取消注释该行,那么我就有问题。注意,在我的示例代码中,我不需要Window类中的任何代码来演示这个问题。

    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:窗口.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 {
    
        };
    
    };
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Ben Reisner    6 年前

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