我有如下一段代码:
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”错误信息。为什么?
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”错误信息。为什么?
解决方案 »
- VB动态数组的例子不明白,向各位请教!
- printform求助,在线等,高分!!!
- 怎么用代码使datagrid的某一行被选中
- VB6中如何进行MPEG4/DivX编码?
- 把一个字符串前边和后边的空格的函数叫什么来着?
- ??? 加急电报
- 怎么判断一个对象是否为nothing?
- 各位四爷,小的有事请教各位。
- 关于web service的问题,怎么访问再有安全限制的api接口?
- 为了感谢vb论坛的兄弟姐妹们---特别是现在还在线的夜猫子。http://www.csdn.net/expert/topic/356/356033.shtm--这个帖子再加分!!
- 问一个非常土的问题,等于散分
- 如何实现打印纸连续问题?(打印过连号发票的朋友请进。)
可以,但是要注册
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
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