李晓亮的博客

导航

vb中动态注册ocx控件

(1)每一个ActiveX控件都有两个输出函数:DllRegisterServer和DllUnRegisterServer。顾名思义,通过这两个函数就可以注册和反注册控件了。
比如要注册以下控件

   C:\WINDOWS\system32\ComCtl32.OCX

    首先在Form中加入两个CommandButton,不要改变它们的属性。然后在Form中
加入如下代码:
Private Declare Function RegComCtl32 Lib "ComCtl32.OCX"  Alias "DllRegisterServer" () As Long
Private Declare Function UnRegComCtl32 Lib "ComCtl32.OCX"   Alias "DllUnregisterServer" () As Long
Private Declare Function FormatMessage Lib "kernel32" _
        Alias "FormatMessageA" (ByVal dwFlags As Long, _
        lpSource As Any, ByVal dwMessageId As Long, _
        ByVal dwLanguageId As Long, ByVal lpBuffer _
        As String, ByVal nSize As Long, Arguments As _
        Long) As Long
Private Declare Function GetLastError Lib "kernel32" () As Long
Const ERROR_SUCCESS = &H0

Private Sub Command1_Click()
    Dim astr As String
    
    '反注册ComCtl32.Ocx
    If RegComCtl32 = ERROR_SUCCESS Then
        MsgBox "注册成功"
    Else
        astr = String$(256, 20)
        FormatMessage FORMAT_MESSAGE_FROM_SYSTEM Or _
            FORMAT_MESSAGE_IGNORE_INSERTS, 0&, GetLastError, _
            0&, astr, Len(astr), ByVal 0
        MsgBox astr
    End If
End Sub

Private Sub Command2_Click()
    Dim astr As String

    '反注册ComCtl32.Ocx    
    If UnRegComCtl32 = ERROR_SUCCESS Then
        MsgBox "反注册成功"
    Else
        astr = String$(256, 20)
        FormatMessage FORMAT_MESSAGE_FROM_SYSTEM Or _
            FORMAT_MESSAGE_IGNORE_INSERTS, 0&, GetLastError, _
            0&, astr, Len(astr), ByVal 0
        MsgBox astr
    End If
End Sub
    运行程序,点击Command2反注册ComCtl32.Ocx控件,在VB菜单中选 Project|components
或按Ctrl+T,在控件列表框中可以看到已经没有ComCtl32.Ocx了。再运行程序,点击Command1
重新注册控件。

(2) 参考模块

Private Declare Function FreeLibrary Lib "kernel32" (ByVal hModule As Long) As Long
Private Declare Function LoadLibraryA Lib "kernel32" (ByVal lpFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function CallWindowProcA Lib "user32" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

'注册。
Public Function DllRegister(ByVal FileName As String) As Boolean
    Dim hModule As Long
    Dim pAddress As Long
    hModule = LoadLibraryA(FileName)
    If hModule Then
        pAddress = GetProcAddress(hModule, "DllRegisterServer")
        If pAddress Then
            If CallWindowProcA(pAddress, 0, 0, 0, 0) Then DllRegister = True
        End If
        Call FreeLibrary(hModule)
    End If
End Function

'反注册。
Public Function DllUnregister(ByVal FileName As String) As Boolean
    Dim hModule As Long
    Dim pAddress As Long
    hModule = LoadLibraryA(FileName)
    If hModule Then
        pAddress = GetProcAddress(hModule, "DllUnregisterServer")
        If pAddress Then
            If CallWindowProcA(pAddress, 0, 0, 0, 0) Then DllUnregister = True
        End If
        Call FreeLibrary(hModule)
    End If

End Function

(3) 直接使用regsvr32
注册    regsvr32 vsflex8u.ocx /s

反注册 regsvr32 vsflex8u.ocx /u /s

posted on 2011-07-30 20:47  LeeXiaoLiang  阅读(1147)  评论(0)    收藏  举报