Option Explicit
Private Declare Function FindWindow Lib "user32.dll" Alias "FindWindowA" (ByVal lpClassName As Any, ByVal lpWindowName As Any) As Long
Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" _
  (ByVal lpLibFileName As String) As Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, _
    ByVal lpProcName As String) As Long
Private Declare Function CreateThread Lib "kernel32" (lpThreadAttributes As Any, _
   ByVal dwStackSize As Long, ByVal lpStartAddress As Long, ByVal lParameter As Long, _
   ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, _
    ByVal dwMilliseconds As Long) As Long
Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, _
    lpExitCode As Long) As Long
Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode As Long)
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As LongDim mresult
Dim Reg As String, Success As Boolean
Public Sub RegUnReg(ByVal inFileSpec As String, Optional inHandle As String = "")
    On Error Resume Next
    Dim lLib As Long                 ' Store handle of the control library
    Dim lpDLLEntryPoint As Long      ' Store the address of function called
    Dim lpThreadID As Long           ' Pointer that receives the thread identifier
    Dim lpExitCode As Long           ' Exit code of GetExitCodeThread
    Dim mThread
    
      ' Load the control DLL, i. e. map the specified DLL file into the
      ' address space of the calling process
    lLib = LoadLibrary(inFileSpec)
    If lLib = 0 Then
         ' e.g. file not exists or not a valid DLL file
        MsgBox "Failure loading control DLL"
        Exit Sub
    End If
    
      ' Find and store the DLL entry point, i.e. obtain the address of the
      ' 揇llRegisterServer?or "DllUnregisterServer" function (to register
      ' or deregister the server抯 components in the registry).
      '
    If inHandle = "" Then
        lpDLLEntryPoint = GetProcAddress(lLib, "DllRegisterServer")
    ElseIf inHandle = "U" Or inHandle = "u" Then
        lpDLLEntryPoint = GetProcAddress(lLib, "DllUnregisterServer")
    Else
        MsgBox "Unknown command handle"
        Exit Sub
    End If
    If lpDLLEntryPoint = vbNull Or lpDLLEntryPoint = 0 Then
        GoTo earlyExit1
    End If
    
    Screen.MousePointer = vbHourglass
    
      ' Create a thread to execute within the virtual address space of the calling process
    mThread = CreateThread(ByVal 0, 0, ByVal lpDLLEntryPoint, ByVal 0, 0, lpThreadID)
    If mThread = 0 Then
        GoTo earlyExit1
    End If
    
      ' Use WaitForSingleObject to check the return state (i) when the specified object
      ' is in the signaled state or (ii) when the time-out interval elapses.  This
      ' function can be used to test Process and Thread.
    mresult = WaitForSingleObject(mThread, 10000)
    If mresult <> 0 Then
        GoTo earlyExit2
    End If
    
      ' We don't call the dangerous TerminateThread(); after the last handle
      ' to an object is closed, the object is removed from the system.
    CloseHandle mThread
    FreeLibrary lLib
    
    Screen.MousePointer = vbDefault
    Success = True
    Exit Sub
    
    
earlyExit1:
    Screen.MousePointer = vbDefault
    MsgBox "Registration failed in obtaining entry point or creating thread for " & inFileSpec & ".", vbCritical, "Registration Failed!"
     ' Decrements the reference count of loaded DLL module before leaving
    FreeLibrary lLib
    Success = False
    Exit Sub
    
earlyExit2:
    Success = False
    Screen.MousePointer = vbDefault
    MsgBox "Registration failed in signaled state or time-out for " & inFileSpec & ".", vbCritical, "Registration Failed!"
    FreeLibrary lLib
     ' Terminate the thread to free up resources that are used by the thread
     ' NB Calling ExitThread for an application's primary thread will cause
     ' the application to terminate
    lpExitCode = GetExitCodeThread(mThread, lpExitCode)
    ExitThread lpExitCode
End Sub例子
RegUnReg DLL文件名 (注册)
RegUnReg DLL文件名,"U" (反注册)

解决方案 »

  1.   

    HOWTO: Register Your Custom ActiveX DLL from a Client (Q173407)
    http://support.microsoft.com/default.aspx?scid=kb;en-us;Q173407HOWTO: Programmatically Register and Unregister .OCX Files (Q173091)
    http://support.microsoft.com/default.aspx?scid=kb;en-us;Q173091 
     
      

  2.   

    HOWTO: Register Your Custom ActiveX DLL from a Client (Q173407)
    http://support.microsoft.com/default.aspx?scid=kb;en-us;Q173407HOWTO: Programmatically Register and Unregister .OCX Files (Q173091)
    http://support.microsoft.com/default.aspx?scid=kb;en-us;Q173091 
     
      

  3.   

    98下把dll copy到system目录下
    运行regsvr32 ***.dll 2000下把dll copy到system32目录下
    运行regsvr32 ***.dll我试过,可以的!
      

  4.   

    把regsvr32和你的DLL的路径都写完整,用Shell就可以了
      

  5.   

    Dim i  As Double
    i = Shell("regsvr32.exe C:\windows\system\MyNewTest.dll")
    if i=0 then '注册不成功endif
      

  6.   

    '在窗体中放两个按扭command1和command2,然后代码复制试下:
    Private Declare Function DllRegisterServer Lib "ComCtl32.OCX" () As Long
    Private Declare Function DllUnregisterServer Lib "ComCtl32.OCX" () As Long
    '注意:可以用任意的OCX或DLL来替换声明中的ComCtl32.ocxConst ERROR_SUCCESS = &H0Private Sub Command1_Click()If DllRegisterServer = ERROR_SUCCESS Then
        MsgBox "注册成功"
    Else
        MsgBox "注册失败"
    End If
    End SubPrivate Sub Command1_Click()If DllUnregisterServer = ERROR_SUCCESS Then
        MsgBox "反注册成功"
    Else
        MsgBox "反注册失败"
    End IfEnd Sub
      

  7.   

    上面代码的下面应为command2_click()
      

  8.   

    调用 SHELLAPI  ShellExecute 执行一个 regsrv32 XXXX.dll
      

  9.   

    将下面的文本粘贴到文本编辑器中,然后存为以.reg为扩展名的文件。双击后将信息添加到注册表中。然后用鼠标右键点击一个动态链接库或OCX控件,在弹出的环境菜单中你将会看到Register和Unregister的选项。运用这个方法的前提是你的计算机上必须有regsvr32.exe文件。
    REGEDIT4
    [HKEY_CLASSES_ROOT\.ocx]
    @="ocxfile"
    [HKEY_CLASSES_ROOT\ocxfile]
    @=”OCX”
    [HKEY_CLASSES_ROOT\ocxfile\shell\Register\command]
    @="RegSvr32 \"%1\""
    [HKEY_CLASSES_ROOT\ocxfile\shell\Unregister\command]
    @="RegSvr32 /u \"%1\"" 
    REGEDIT4
    [HKEY_CLASSES_ROOT\.dll]
    @="dllfile"
    [HKEY_CLASSES_ROOT\dllfile]
    @=”DLL”
    [HKEY_CLASSES_ROOT\dllfile\shell\Register\command]
    @="RegSvr32 \"%1\""
    [HKEY_CLASSES_ROOT\dllfile\shell\Unregister\command]
    @="RegSvr32 /u \"%1\""
      

  10.   

    根据一般的安装程序的进度显示得到的提示:
    你可以试试将该dll文件直接拷入windows\system的目录中,看看能不能使用。