我有如下一段代码:
Private Sub cmdPlugIn_Click()
    Dim strPath As String
    
    'Dll的路径
    strPath = "E:\MyDll.dll"     
    '注册Dll
    If Register(Me.hwnd, strPath, True) = -1 Then
        MsgBox "The Dll registe failed."
        Exit Sub
    End If
    '创建Dll中的一个对象
    Set g_PlugIn = CreateObject("MyDll.PlugIn")
End Sub当MyDll.dll放在应用程序的根路径时,没问题;可是当换一个路径时出“Activex componet can't create object”错误信息。为什么?

解决方案 »

  1.   

    可以放到 %windir%\system32 system(9x)目录,但需要regsrv32注册下
      

  2.   

    Register(Me.hwnd, strPath, True)这个函数就是注册这个dll。除了system32,可不可以放在任意路径中?
      

  3.   

    除了system32,可不可以放在任意路径中?
    可以,但是要注册
      

  4.   

    我不想用regsrv32注册,我想在程序中注册,我的注册函数Register代码如下:
    Private Function Register(hwnd As Long, DllServerPath As String, bRegister As Boolean) As Long
    'hwnd ?用的窗体
    'DllServerPath Dll文件全路径名称
    'bRegister 注册还是注销
    On Error Resume Next    Dim lb As Long
        Dim pa As Long
        
        lb = LoadLibrary(DllServerPath)    If lb = 0 Then
            Register = -1
            Exit Function
        End If
        If bRegister Then
            pa = GetProcAddress(lb, "DllRegisterServer")
        Else
            pa = GetProcAddress(lb, "DllUnregisterServer")
        End If
        If pa = 0 Then
            Register = -1
            Exit Function
        End If
        If CallWindowProc(pa, hwnd, ByVal 0&, ByVal 0&, ByVal 0&) = ERROR_SUCCESS Then
            Register = 0
        Else
            Register = -1
        End If
    '    unmap the library's address
        FreeLibrary lb
    End Function
      

  5.   

    在程序中用api注册也可以,一个例子:
    Option Explicit
    Private Declare Function LoadLibraryA Lib "kernel32" (ByVal lLibFileName As String) As Long
    Private Declare Function CreateThread Lib "kernel32" (lThreadAttributes As Any, ByVal lStackSize As Long, ByVal lStartAddress As Long, ByVal larameter As Long, ByVal lCreationFlags As Long, lThreadID As Long) As Long
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal lMilliseconds As Long) As Long
    Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lProcName As String) 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 Long
    Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lExitCode As Long) As Long
    Private Declare Sub ExitThread Lib "kernel32" (ByVal lExitCode As Long)
    'Purpose   :    This function registers and Unregisters OLE components
    'Inputs    :    sDllPath                        The path to the DLL/OCX
    '               bRegister                       If True Registers the control, else unregisters control
    'Outputs   :    Returns True if successful
    'Author    :    Andrewb
    'Date      :    04/09/2000
    'Notes     :    This effectively replaces RegSvr32.exe by loading the library and
    '               calling the register or unregister functions exposed by all OLE components.
    'Revisions :
    Function RegisterServer(ByVal sDllPath As String, Optional bRegister As Boolean = True) As Boolean
    Dim lLibAddress As Long, lProcAddress As Long, lThreadID As Long, lSuccess As Long, lExitCode As Long, lThread As Long
    Dim sRegister As String
    Const clMaxTimeWait As Long = 20000     'Wait 20 secs for register to
    complete
    On Error GoTo ExitFunc
    If Len(sDllPath) > 0 And Len(Dir(sDllPath)) > 0 Then
    'File exists
    If bRegister Then
    sRegister = "DllRegisterServer"
    Else
    sRegister = "DllUnregisterServer"
    End If
    'Load library into current process
    lLibAddress = LoadLibraryA(sDllPath)
    If lLibAddress Then
    'Get address of the DLL function
    lProcAddress = GetProcAddress(lLibAddress, sRegister)
    If lProcAddress Then
    'Found interface, make call to component
    lThread = CreateThread(ByVal 0&, 0&, ByVal lProcAddress, ByVal 0&, 0&, lThread)
    If lThread Then
    'Created thread
    lSuccess = (WaitForSingleObject(lThread, clMaxTimeWait) = 0)
    If Not lSuccess Then
    'Failed to register, close thread
    Call GetExitCodeThread(lThread, lExitCode)
    Call ExitThread(lExitCode)
    RegisterServer = False
    Else
    'Register control
    RegisterServer = True
    Call CloseHandle(lThread)
    End If
    End If
    Else
    'Object doesn't expose OLE interface
    FreeLibrary lLibAddress
    End If
    Call FreeLibrary(lLibAddress)
    End If
    End If
    ExitFunc:
    On Error GoTo 0
    End Function