在VB中注册控件

Private   Declare   Function LoadLibraryA Lib "kernel32" (ByVal lLibFileName As StringAs Long
Private Declare Function CreateThread Lib "kernel32" (lThreadAttributes As Any, ByVal lStackSize As LongByVal lStartAddress As LongByVal larameter As LongByVal lCreationFlags As Long, lThreadID As LongAs Long
Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As LongByVal lMilliseconds As LongAs Long
Private Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As LongByVal lProcName As StringAs Long
Private Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As LongAs Long
Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As LongAs Long
Private Declare Function GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, lExitCode As LongAs Long
Private Declare Sub ExitThread Lib "kernel32" (ByVal lExitCode As Long)
'输入 : sDllPath DLL/OCX 的全名
'
 bRegister 注册为true , 反注册为false
'
输出 : 如成功返回true
'
描述 : 注册/反注册控件(ocx /dll)

Function RegisterServer(ByVal sDllPath As StringOptional bRegister As Boolean = TrueAs 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

 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值