• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 286
  • Last Modified:

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.
1 Solution
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

cowshedAuthor Commented:
Is there a way to remove a control from memory and update with a newer version, with out rebooting in between?
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now