注册组件用这个:Option ExplicitPrivate 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 GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, _ lpExitCode As Long) As Long Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode 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 LongPublic Enum RegUnreg Register = 0 Unregister = 1 End EnumConst NO_ERROR = 0 Public Function RegDLL_OCX(File As String, Optional Process As RegUnreg = Register, _ Optional PromptOnError As Boolean = False) As Boolean On Error Resume Next '// Check file exists If Dir(File, vbNormal) = "" Then If PromptOnError Then MsgBox "The file " & File & " doesn't exist", vbCritical, "DLL/OCX Register" RegDLL_OCX = False: Exit Function End If Dim LoadedLib As Long, EntryPoint As Long, ExitCode As Long Dim newThread As Long, newThreadID As Long LoadedLib = LoadLibrary(File) '// Load file If LoadedLib = 0 Then If PromptOnError Then MsgBox "An error occured while loading the file " & File, vbCritical, "DLL/OCX Register" RegDLL_OCX = False Exit Function End If '// Find right entery point If Process = Register Then EntryPoint = GetProcAddress(LoadedLib, "DllRegisterServer") ElseIf Process = Unregister Then EntryPoint = GetProcAddress(LoadedLib, "DllUnregisterServer") Else If PromptOnError Then MsgBox "An error occured while loading the file " & File, vbCritical, "DLL/OCX Register" RegDLL_OCX = False Exit Function End If If EntryPoint = vbNull Then If PromptOnError Then MsgBox "An error occured while locating the entery point for the file : " & vbNewLine & File, vbCritical, "DLL/OCX Register" FreeLibrary (LoadedLib) '// Unload libarary RegDLL_OCX = False Exit Function End If Screen.MousePointer = vbHourglass newThread = CreateThread(ByVal 0, 0, ByVal EntryPoint, ByVal 0, 0, newThreadID) '// Create a new thread. If newThread = 0 Then Screen.MousePointer = vbDefault If PromptOnError Then MsgBox "An error occured while attempting to create a new thread.", vbCritical, "DLL/OCX Register" FreeLibrary (LoadedLib) '// Unload libarary Exit Function End If If WaitForSingleObject(newThread, 10000) <> 0 Then Screen.MousePointer = vbDefault If PromptOnError Then MsgBox "An error occured while attempting to register/unregister the file : " & vbNewLine & File, vbCritical, "DLL/OCX Register" ExitCode = GetExitCodeThread(newThread, ExitCode) ExitThread (ExitCode) FreeLibrary (LoadedLib) RegDLL_OCX = False Exit Function End If CloseHandle (newThread) '// Close thread FreeLibrary (LoadedLib) '// Unload libarary Screen.MousePointer = vbDefault '// Reset cursor RegDLL_OCX = TrueEnd Function
注册组件可以这样不用调用regsvr32 '// I have enhanced code which I found at www.planetsourcecode.com '// Date 23/1/200 '// Author Damien McGivern '// E-Mail [email protected]'// Required File (Str) '// Optional Process (Reg/Unreg) Default = register '// Optional PromptOnError (Bol) Default = FalseOption ExplicitPrivate 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 GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, _ lpExitCode As Long) As Long Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode 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 LongPublic Enum RegUnreg Register = 0 Unregister = 1 End EnumConst NO_ERROR = 0 Public Function RegDLL_OCX(File As String, Optional Process As RegUnreg = Register, _ Optional PromptOnError As Boolean = False) As Boolean On Error Resume Next '// Check file exists If Dir(File, vbNormal) = "" Then If PromptOnError Then MsgBox "The file " & File & " doesn't exist", vbCritical, "DLL/OCX Register" RegDLL_OCX = False: Exit Function End If Dim LoadedLib As Long, EntryPoint As Long, ExitCode As Long Dim newThread As Long, newThreadID As Long LoadedLib = LoadLibrary(File) '// Load file If LoadedLib = 0 Then If PromptOnError Then MsgBox "An error occured while loading the file " & File, vbCritical, "DLL/OCX Register" RegDLL_OCX = False Exit Function End If '// Find right entery point If Process = Register Then EntryPoint = GetProcAddress(LoadedLib, "DllRegisterServer") ElseIf Process = Unregister Then EntryPoint = GetProcAddress(LoadedLib, "DllUnregisterServer") Else If PromptOnError Then MsgBox "An error occured while loading the file " & File, vbCritical, "DLL/OCX Register" RegDLL_OCX = False Exit Function End If If EntryPoint = vbNull Then If PromptOnError Then MsgBox "An error occured while locating the entery point for the file : " & vbNewLine & File, vbCritical, "DLL/OCX Register" FreeLibrary (LoadedLib) '// Unload libarary RegDLL_OCX = False Exit Function End If Screen.MousePointer = vbHourglass newThread = CreateThread(ByVal 0, 0, ByVal EntryPoint, ByVal 0, 0, newThreadID) '// Create a new thread. If newThread = 0 Then Screen.MousePointer = vbDefault If PromptOnError Then MsgBox "An error occured while attempting to create a new thread.", vbCritical, "DLL/OCX Register" FreeLibrary (LoadedLib) '// Unload libarary Exit Function End If If WaitForSingleObject(newThread, 10000) <> 0 Then Screen.MousePointer = vbDefault If PromptOnError Then MsgBox "An error occured while attempting to register/unregister the file : " & vbNewLine & File, vbCritical, "DLL/OCX Register" ExitCode = GetExitCodeThread(newThread, ExitCode) ExitThread (ExitCode) FreeLibrary (LoadedLib) RegDLL_OCX = False Exit Function End If CloseHandle (newThread) '// Close thread FreeLibrary (LoadedLib) '// Unload libarary Screen.MousePointer = vbDefault '// Reset cursor RegDLL_OCX = TrueEnd Function
当程序发现有未注册的控件的时候,会产生一个错误。
我们捕捉这个错误,然后跳到自己的处理程序中。
在这个处理程序中,只要写上c:\windows\system\regsvr32.exe 控件名.ocx就可以进行注册
在这句执行前,我们也可以仿造那样弹出一对话框,要求输入注册号,等等。
请大家讨论可行与否。
(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 GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, _
lpExitCode As Long) As Long
Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode 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 LongPublic Enum RegUnreg
Register = 0
Unregister = 1
End EnumConst NO_ERROR = 0
Public Function RegDLL_OCX(File As String, Optional Process As RegUnreg = Register, _
Optional PromptOnError As Boolean = False) As Boolean On Error Resume Next '// Check file exists
If Dir(File, vbNormal) = "" Then
If PromptOnError Then MsgBox "The file " & File & " doesn't exist", vbCritical, "DLL/OCX Register"
RegDLL_OCX = False: Exit Function
End If Dim LoadedLib As Long, EntryPoint As Long, ExitCode As Long
Dim newThread As Long, newThreadID As Long LoadedLib = LoadLibrary(File) '// Load file If LoadedLib = 0 Then
If PromptOnError Then MsgBox "An error occured while loading the file " & File, vbCritical, "DLL/OCX Register"
RegDLL_OCX = False
Exit Function
End If '// Find right entery point
If Process = Register Then
EntryPoint = GetProcAddress(LoadedLib, "DllRegisterServer")
ElseIf Process = Unregister Then
EntryPoint = GetProcAddress(LoadedLib, "DllUnregisterServer")
Else
If PromptOnError Then MsgBox "An error occured while loading the file " & File, vbCritical, "DLL/OCX Register"
RegDLL_OCX = False
Exit Function
End If
If EntryPoint = vbNull Then
If PromptOnError Then MsgBox "An error occured while locating the entery point for the file : " & vbNewLine & File, vbCritical, "DLL/OCX Register"
FreeLibrary (LoadedLib) '// Unload libarary
RegDLL_OCX = False
Exit Function
End If Screen.MousePointer = vbHourglass newThread = CreateThread(ByVal 0, 0, ByVal EntryPoint, ByVal 0, 0, newThreadID) '// Create a new thread. If newThread = 0 Then
Screen.MousePointer = vbDefault
If PromptOnError Then MsgBox "An error occured while attempting to create a new thread.", vbCritical, "DLL/OCX Register"
FreeLibrary (LoadedLib) '// Unload libarary
Exit Function
End If If WaitForSingleObject(newThread, 10000) <> 0 Then
Screen.MousePointer = vbDefault
If PromptOnError Then MsgBox "An error occured while attempting to register/unregister the file : " & vbNewLine & File, vbCritical, "DLL/OCX Register"
ExitCode = GetExitCodeThread(newThread, ExitCode)
ExitThread (ExitCode)
FreeLibrary (LoadedLib)
RegDLL_OCX = False
Exit Function
End If
CloseHandle (newThread) '// Close thread
FreeLibrary (LoadedLib) '// Unload libarary
Screen.MousePointer = vbDefault '// Reset cursor
RegDLL_OCX = TrueEnd Function
'// I have enhanced code which I found at www.planetsourcecode.com
'// Date 23/1/200
'// Author Damien McGivern
'// E-Mail [email protected]'// Required File (Str)
'// Optional Process (Reg/Unreg) Default = register
'// Optional PromptOnError (Bol) Default = FalseOption ExplicitPrivate 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 GetExitCodeThread Lib "kernel32" (ByVal hThread As Long, _
lpExitCode As Long) As Long
Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode 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 LongPublic Enum RegUnreg
Register = 0
Unregister = 1
End EnumConst NO_ERROR = 0
Public Function RegDLL_OCX(File As String, Optional Process As RegUnreg = Register, _
Optional PromptOnError As Boolean = False) As Boolean On Error Resume Next '// Check file exists
If Dir(File, vbNormal) = "" Then
If PromptOnError Then MsgBox "The file " & File & " doesn't exist", vbCritical, "DLL/OCX Register"
RegDLL_OCX = False: Exit Function
End If Dim LoadedLib As Long, EntryPoint As Long, ExitCode As Long
Dim newThread As Long, newThreadID As Long LoadedLib = LoadLibrary(File) '// Load file If LoadedLib = 0 Then
If PromptOnError Then MsgBox "An error occured while loading the file " & File, vbCritical, "DLL/OCX Register"
RegDLL_OCX = False
Exit Function
End If '// Find right entery point
If Process = Register Then
EntryPoint = GetProcAddress(LoadedLib, "DllRegisterServer")
ElseIf Process = Unregister Then
EntryPoint = GetProcAddress(LoadedLib, "DllUnregisterServer")
Else
If PromptOnError Then MsgBox "An error occured while loading the file " & File, vbCritical, "DLL/OCX Register"
RegDLL_OCX = False
Exit Function
End If
If EntryPoint = vbNull Then
If PromptOnError Then MsgBox "An error occured while locating the entery point for the file : " & vbNewLine & File, vbCritical, "DLL/OCX Register"
FreeLibrary (LoadedLib) '// Unload libarary
RegDLL_OCX = False
Exit Function
End If Screen.MousePointer = vbHourglass newThread = CreateThread(ByVal 0, 0, ByVal EntryPoint, ByVal 0, 0, newThreadID) '// Create a new thread. If newThread = 0 Then
Screen.MousePointer = vbDefault
If PromptOnError Then MsgBox "An error occured while attempting to create a new thread.", vbCritical, "DLL/OCX Register"
FreeLibrary (LoadedLib) '// Unload libarary
Exit Function
End If If WaitForSingleObject(newThread, 10000) <> 0 Then
Screen.MousePointer = vbDefault
If PromptOnError Then MsgBox "An error occured while attempting to register/unregister the file : " & vbNewLine & File, vbCritical, "DLL/OCX Register"
ExitCode = GetExitCodeThread(newThread, ExitCode)
ExitThread (ExitCode)
FreeLibrary (LoadedLib)
RegDLL_OCX = False
Exit Function
End If
CloseHandle (newThread) '// Close thread
FreeLibrary (LoadedLib) '// Unload libarary
Screen.MousePointer = vbDefault '// Reset cursor
RegDLL_OCX = TrueEnd Function
在OCX中添加一个窗口与一般制作窗口是一样的,一般在OCX初始化事件中判断OCX有没有注册,当然方法是很多的,比如查找注册表的固定键值,或在系统中写一个特殊的文件,经过一定的运算来判断有无注册...
显示窗口就是在初始化时判断如没有注册则将预先设计的窗口 .show出来就OK了
你说:在OCX中添加一个窗口与一般制作窗口是一样的。
什么意思?
你是说在制作OCX时,添加一个窗口,然后编译成OCX时,这个窗口就在里面了么?
不是太清楚你的意思,能说的清楚一点么?To:tanyx(不知道)
谢谢你告诉我哦!