dynamically registering OCX's

Hi, I'm trying to write an upgrade tool for the applications we have.  What I need to be able to do is register OCX in code.  I know how to do this when the OCX name is hard coded, but how do I do it dynamically.  I cant shell regsvr32 either.  The idea is to be able to produce ocx's in house and deploy them at some latter point if needed, hence we don't know what they will be called until they are written.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

You can use the follow code to register an ActiveX component in your VB code. All you have to do is to provide the path for the OCX.


Private Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" _
   (ByVal lpLibFileName As String) As Long
Private Declare Function FreeLibrary Lib "kernel32" _
   (ByVal hLibModule As Long) 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, _
   lpStartAddress As Long, _
   lpParameter As Any, _
   ByVal dwCreationFlags As Long, _
   lpThreadID As Long) As Long
'                            (lpThreadAttributes As SECURITY_ATTRIBUTES, _

' dwCreationFlags param, call ResumeThread to
' wake the thread up, specify 0 for an alive thread
Private Const CREATE_SUSPENDED = &H4

Private Declare Function WaitForSingleObject Lib "kernel32" _
   (ByVal hHandle As Long, _
   ByVal dwMilliseconds As Long) As Long
' dwMilliseconds param, specify 0 for immediate return.
Private Const INFINITE = &HFFFFFFFF   ' Infinite timeout
' WaitForSingleObject rtn vals
Private Const STATUS_WAIT_0 = &H0
Private Const STATUS_ABANDONED_WAIT_0 = &H80
Private Const STATUS_TIMEOUT = &H102

' The state of the specified object is signaled (success)
Private Const WAIT_OBJECT_0 = ((STATUS_WAIT_0) + 0)

' Thread went away before the mutex got signaled

' dwMilliseconds timed out

Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
Private Declare Sub ExitThread Lib "kernel32" (ByVal dwExitCode As Long)
Private Declare Function ResumeThread Lib "kernel32" (ByVal hThread As Long) As Long
Private Declare Function GetExitCodeThread Lib "kernel32" _
   (ByVal hThread As Long, _
   lpExitCode As Long) As Long
Private Const STATUS_PENDING = &H103


' Example:
'         Dim retVal As Boolean
'             retVal = VBRegServer32("comctl32.ocx", False)
' Registers or unregisters the specified COM server.
'   sServerPath  - server's path, either explicit, or relative if the system can find it
'   fRegister    - optional flag indicating what operation to perform:
'                  True (defualt) registers the server, False unregisters it.
' Returns True on success, False otherwise.

Public Function VBRegServer32(ByVal sServerPath As String, Optional fRegister = True) As Boolean
   Dim hMod As Long            ' module handle
   Dim lpfn As Long            ' reg/unreg function address
   Dim lpThreadID As Long      ' dummy var that get's filled
   Dim hThread As Long         ' thread handle
   Dim fSuccess As Boolean     ' if things worked
   Dim dwExitCode As Long      ' thread's exit code if it doesn't finish
   ' Load the server into memeory
   hMod = LoadLibrary(sServerPath)
   ' Get the specified function's address and our msgbox string.
   If fRegister Then
      lpfn = GetProcAddress(hMod, "DllRegisterServer")
      lpfn = GetProcAddress(hMod, "DllUnregisterServer")
   End If
   ' If we got a function address...
   If lpfn Then
      ' Create an alive thread and execute the function.
      hThread = CreateThread(ByVal 0, 0, ByVal lpfn, ByVal 0, 0, lpThreadID)
      ' If we got the thread handle...
      If hThread Then
         ' Wait 10 secs for the thread to finish (the function may take a while...)
         fSuccess = (WaitForSingleObject(hThread, 10000) = WAIT_OBJECT_0)
         ' If it didn't finish in 5 seconds...
         If Not fSuccess Then
            ' Something unlikely happened, lose the thread.
            Call GetExitCodeThread(hThread, dwExitCode)
            Call ExitThread(dwExitCode)
         End If
         ' Lose the thread handle
         Call CloseHandle(hThread)
      End If   ' hThread
   End If   ' lpfn
   ' Free server if we loaded it.
   If hMod Then Call FreeLibrary(hMod)
   If fSuccess Then
      VBRegServer32 = True
      VBRegServer32 = False
   End If
End Function


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
cowshedAuthor Commented:
Is there a way to remove a control from memory and update with a newer version, with out rebooting in between?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic Classic

From novice to tech pro — start learning today.