關於 ActiveX (OLE) 元件登錄註冊
如何 登錄註冊 Actvie X DLL , OCX , EXE (OLE) 元件
關於ActiveX動態連結程式庫DLL、ActiveX控制項OCX、ActiveX執行檔EXE
及物件連結與嵌入OLE 控制項 ( Object Linking and Embedding )
皆需要登錄註冊後方可使用,在此介紹幾種元件註冊方法 :
1. 使用 RegSvr32.exe 工具 ( 命令列 DOS 模式下註冊工具 )
語法 :
regsvr32 [/u] [/s] [/n] [/i[:cmdline]] dllname
參數 :
/u : Unregisters server.
/s : Specifies regsvr32 to run silently and to not display any message boxes.
/n : Specifies not to call DllRegisterServer. You must use this option with /i.
/i:cmdline : Calls DllInstall passing it an optional [cmdline]. When used with /u, it calls dll uninstall.
dllname : Specifies the name of the dll file that will be registered.
/? : Displays help at the command prompt.
例 :
RegSvr32 C:\123.dll 註冊
RegSvr32 /u C:\123.dll 反註冊
RegSvr32.exe的依存檔案 :
Kernel32.dll、User32.dll、Ole32.dll及Windows NT相關OS平台中Msvcrt.dll、Advapi32.dll檔案。
Regsvr32.exe 會將您嘗試登錄或解除登錄的檔案,連同所有相依檔案一起載入。
如果必要的檔案遺失或損壞了,程序可能無法成功。
(可用Depends.exe 來查看依存檔案;該工具有附在 M$ Windows XX Resource Kit 支援工具或其他開發工具中。
================================================================
2. 呼叫 API DLLSelfRegister ( 使用 vb6stkit.dll )
宣告
Private Declare Function DLLSelfRegister Lib "vb6stkit.dll" _
(ByVal lpDllName As String) As Integer
使用
ReturnCode = DLLSelfRegister("元件完整路徑加檔名,例:C:\123.dll或C:\123.ocx")
回傳值 :
0 - OK
2 - 無法初始化 OLE 來註冊檔案
3 - 註冊檔案時,LoadLibrary() 函數執行失敗!
4 - 在檔案中無法找到 DllRegisterServer() 的進入點!
5 - 檔案中之 DllRegisterServer() 函數執行失敗!
Else - 註冊檔案時,發生無法預期的錯誤!
================================================================
3. 呼叫 API DllRegisterServer
宣告
Private Declare Function DllRegisterServer Lib "元件完整路徑加檔名,例:C:\123.dll或C:\123.ocx " () As Long
使用
If DllRegisterServer = &H0 Then
MsgBox "註冊成功 !"
Else
MsgBox "註冊失敗 !"
End If
PS: 此方法有先天限制 , 一次只能宣告一個 , 因此也只能註冊一個元件
================================================================
4. 呼叫API LoadLibrary 、GetProcAddress、CallWindowProc
宣告
Private Declare Function FreeLibrary Lib "kernel32" _
(ByVal hLibModule As Long) 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 CallWindowProc Lib "user32" Alias "CallWindowProcA" _
(ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal Msg As Any, _
ByVal wParam As Any, ByVal lParam As Any) As Long
撰寫函數
Public Function Register(strFile As String, blnRegister As Boolean) As Boolean
Dim lngLB As Long, lngPA As Long, lngHwnd As Long
lngHwnd = Screen.ActiveForm.hWnd
lngLB = LoadLibrary(strFile)
If lngLB > 0 Then
lngPA = GetProcAddress(lngLB, IIf(blnRegister, "DllRegisterServer", "DllUnregisterServer"))
If lngPA > 0 Then Register = CallWindowProc(lngPA, lngHwnd, ByVal 0&, ByVal 0&, ByVal 0&) = &H0
End If
FreeLibrary lngLB
End Function
參數說明 :
strFile : 元件檔名
blnRegister : 布林值 , True 為登錄註冊元件 , False 為反註冊元件 !
回傳值 : 布林值, True為登錄註冊成功 , False 則為失敗 !
使用
If Register("元件完整路徑加檔名,例:C:\123.dll或C:\123.ocx ", True) Then
MsgBox "成功 !"
Else
MsgBox "失敗 !"
End If
================================================================
5. 呼叫API LoadLibrary 、GetProcAddress、CreateThread、WaitForSingleObject
宣告
Private Declare Function FreeLibrary Lib "kernel32" _
(ByVal hLibModule As Long) 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 CloseHandle Lib "kernel32" (ByVal hObject 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)
撰寫函數
Public Function Register(strFile As String, blnRegister As Boolean) As Boolean
Dim lngLB As Long, lngPA As Long, lngThread As Long, lngExitCode As Long
lngLB = LoadLibrary(strFile)
If lngLB > 0 Then
lngPA = GetProcAddress(lngLB, IIf(blnRegister, "DllRegisterServer", "DllUnregisterServer"))
If lngPA > 0 Then
lngThread = CreateThread(ByVal 0&, 0&, ByVal lngPA, ByVal 0&, 0&, lngThread)
If lngThread Then
Register = (WaitForSingleObject(lngThread, 5000) = 0)
If Not Register Then
GetExitCodeThread lngThread, lngExitCode
ExitThread lngExitCode
End If
CloseHandle lngThread
End If
End If
End If
FreeLibrary lngLB
End Function
參數說明 :
strFile : 元件檔名
blnRegister : 布林值 , True 為登錄註冊元件 , False 為反註冊元件 !
回傳值 : 布林值, True為登錄註冊成功 , False 則為失敗 !
使用
If Register("元件完整路徑加檔名,例:C:\123.dll或C:\123.ocx ", True) Then
MsgBox "成功 !"
Else
MsgBox "失敗 !"
End If
================================================================
6. 關於 ActiveX EXE
登錄註冊 : 只要執行該 EXE執行檔 , 即完成註冊
反註冊 : 執行該 EXE 執行檔 , 後面加 /UNREGSERVER 參數即可