如何在VBA中注册类型库 [英] How to register a type library in VBA

查看:379
本文介绍了如何在VBA中注册类型库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用两种通过Google找到的技术的变体(下面的Subs RegisterTypeLibrary和RegisterTypeLibrary2)从VBA代码以编程方式注册类型库.

I am trying to register a type library programatically from VBA code, using two variants of a technique found using Google (Subs RegisterTypeLibrary and RegisterTypeLibrary2 below).

以下代码在调用LoadTypeLib/LoadTypeLibEx时因访问冲突而崩溃.我究竟做错了什么?如果相关,类型库是使用tlbexp从.NET程序集生成的TLB文件.

The code below crashes with an access violation on the call to LoadTypeLib / LoadTypeLibEx. What am I doing wrong? In case it's relevant, the type library is a TLB file generated from a .NET assembly using tlbexp.

Private Enum RegKind
    RegKind_Default = 0
    RegKind_Register = 1
    RegKind_None = 2
End Enum

Private Declare Function LoadTypeLibEx Lib "oleaut32.dll" ( _
    pFileName As Byte, ByVal RegKind As RegKind, pptlib As Object) As Long
Private Declare Function LoadTypeLib Lib "oleaut32.dll" ( _
    pFileName As Byte, pptlib As Object) As Long
Private Declare Function RegisterTypeLib Lib "oleaut32.dll" ( _
    ByVal ptlib As Object, szFullPath As Byte, _
    szHelpFile As Byte) As Long

Private Sub RegisterTypeLibrary(FileName As String)

    Dim abNullTerminatedFileName() As Byte
    Dim objTypeLib As Object
    Dim lHResult As Long

    abNullTerminatedFileName = FileName & vbNullChar
    lHResult = LoadTypeLib(abNullTerminatedFileName(0), objTypeLib)
    If lHResult <> 0 Then
        Err.Raise lHResult, "LoadTypeLib", "Error registering type library " & FileName
    End If
    lHResult = RegisterTypeLib(objTypeLib, abNullTerminatedFileName(0), 0)
    If lHResult <> 0 Then
        Err.Raise lHResult, "RegisterTypeLib", "Error registering type library " & FileName
    End If
    Exit Sub

End Sub
Private Sub RegisterTypeLibrary2(FileName As String)
    Dim abNullTerminatedFileName() As Byte
    Dim objTypeLib As Object
    Dim lHResult As Long

    abNullTerminatedFileName = FileName & vbNullChar
    lHResult = LoadTypeLibEx(abNullTerminatedFileName(0), ByVal RegKind_Register, objTypeLib)
    If lHResult <> 0 Then
        Err.Raise lHResult, "LoadTypeLibEx", "Error registering type library " & FileName
    End If
End Sub

编辑

我怀疑这与我的类型库有关.我找到了一种解决方案,下面已将其发布为答案.

I suspect it is something specific about my type library. I've found a solution which I've posted as an answer below.

推荐答案

我已经找到了使用以下代码的解决方案.基本上,LoadTypeLibEx的第三个参数(在C/C ++中为ITypeLib **)被声明为stdole.IUnknown而不是Object.

I've found a solution, using the code below. Basically, the third parameter to LoadTypeLibEx (ITypeLib** in C/C++) is declared as stdole.IUnknown instead of as Object.

为此,我需要在VBA项目中添加对stdole32.tlb的引用.

To do so, I needed to add a reference to stdole32.tlb to the VBA project.

我怀疑我的类型库中有一些东西,这意味着它不能被声明为VB(后期绑定)对象.

I suspect there is something about my type library that means it can't be declared as a VB (late-bound) Object.

我也可以将第三个参数声明为Long,但是我不确定这不会导致引用计数出现问题.

I could also have declared the third parameter as Long, but I'm not sure that wouldn't lead to problems with reference counting.

Private Enum RegKind
    RegKind_Default = 0
    RegKind_Register = 1
    RegKind_None = 2
End Enum

Private Declare Function LoadTypeLibEx Lib "oleaut32.dll" ( _
    pFileName As Byte, ByVal RegKind As RegKind, pptlib As stdole.IUnknown) As Long

Public Sub RegisterTypeLibrary(FileName As String)
    Dim abNullTerminatedFileName() As Byte
    Dim objTypeLib As stdole.IUnknown
    Dim lHResult As Long

    abNullTerminatedFileName = FileName & vbNullChar
    lHResult = LoadTypeLibEx(abNullTerminatedFileName(0), ByVal RegKind_Register, objTypeLib)
    If lHResult <> 0 Then
        Err.Raise lHResult, "LoadTypeLibEx", "Error registering type library " & FileName
    End If
End Sub

这篇关于如何在VBA中注册类型库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆