在VB中可以注册控件吗?

hwyqy 2003-10-19 07:16:21
我编了一个程序,用到了一个控件,但要注册,可我不想做安装程序,想在程序运行时察看控件有无注册,如果没有注册就注册。请问能够做到吗?
...全文
130 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
abc10 2003-10-20
  • 打赏
  • 举报
回复

调用:
call fun_RegServer(filename),注册
call fun_unRegServer(filename),反注册

你应该在加载 用到了该控件的窗体前调用。
abc10 2003-10-20
  • 打赏
  • 举报
回复
当然可以。
*****'注册、反注册OCX、DLL文件
Private Declare Function LoadLibraryRegister Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Private Declare Function CreateThreadForRegister Lib "kernel32" Alias "CreateThread" (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 GetProcAddressRegister Lib "kernel32" Alias "GetProcAddress" (ByVal hModule As Long, ByVal lpProcName As String) As Long
Private Declare Function FreeLibraryRegister Lib "kernel32" Alias "FreeLibrary" (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, lpExitCode As Long) As Long
Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode As Long)
Public Function fun_RegServer(ByVal filename As String) As Boolean
'注册
fun_RegServer = RegSvr32(filename, False)
End Function

Public Function fun_UnRegServer(ByVal filename As String) As Boolean
'反注册
fun_UnRegServer = RegSvr32(filename, True)
End Function

Private Function RegSvr32(ByVal filename As String, bUnReg As Boolean) As Boolean
Dim lLib As Long
Dim lProcAddress As Long
Dim lThreadID As Long
Dim lSuccess As Long
Dim lExitCode As Long
Dim lThread As Long
Dim bAns As Boolean
Dim sPurpose As String
sPurpose = IIf(bUnReg, "DllUnregisterServer", "DllRegisterServer")
If Dir(filename) = "" Then Exit Function
lLib = LoadLibraryRegister(filename)
'载入文件
If lLib = 0 Then Exit Function
lProcAddress = GetProcAddressRegister(lLib, sPurpose)
If lProcAddress = 0 Then
'不是ActiveX控件
FreeLibraryRegister lLib
Exit Function
Else
lThread = CreateThreadForRegister(ByVal 0&, 0&, ByVal lProcAddress, ByVal 0&, 0&, lThread)
If lThread Then
lSuccess = (WaitForSingleObject(lThread, 10000) = 0)
If Not lSuccess Then
Call GetExitCodeThread(lThread, lExitCode)
Call ExitThread(lExitCode)
bAns = False
FreeLibraryRegister lLib
Exit Function
Else
bAns = True
End If
CloseHandle lThread
FreeLibraryRegister lLib
Else
FreeLibraryRegister lLib
End If
End If
RegSvr32 = bAns
End Function
前调用。
守城小轩 2003-10-20
  • 打赏
  • 举报
回复
'以ComCtl32.ocx为例,其它换相应的名字
Declare Function RegComCtl32 Lib "ComCtl32.ocx" Alias "DllRegisterServer" () As Long

Declare Function UnRegComCtl32 Lib "ComCtl32.ocx" Alias "DllUnRegisterServer" () As Long

Const Error_SUCCESS = &H0
If regconctl32 = Error_SUCCESS Then
MsgBox "注册成功"
Else
MsgBox "注册失败"
End If
If UnRegComCtl32 = Error_SUCCESS Then
MsgBox "注销成功"
Else
MsgBox "注销失败"
End If
XmagicX 2003-10-19
  • 打赏
  • 举报
回复



一般要交费注册的那种控件都有在程序中注册的命令
kmzs 2003-10-19
  • 打赏
  • 举报
回复
也可以用API声明与Regsvr32相关的就行了
hhjjhjhj 2003-10-19
  • 打赏
  • 举报
回复
可以啊
注册:Shell "Regsvr32.exe /s 控件名"
可能要重新打开你的程序

1,486

社区成员

发帖
与我相关
我的任务
社区描述
VB API
社区管理员
  • API
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧