Link to home
Start Free TrialLog in
Avatar of dbrckovi
dbrckoviFlag for Croatia

asked on

Registering a dll from VB6 code

Hi!

I would just like to know, how can I register a dll, ocx, etc. from VB6 code.

I know I can run "Regsvr32 . . ." but I wonder if there is a better way.

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Ryan Chong
Ryan Chong
Flag of Singapore image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
open setup1.vbp that you fill in the visual studio directory. it is the project of the VB installer. You have the code to register components in it.
In VB it could be done using API calls like this:

Dim ExitCode As Long

ExitCode = getExitCodeProcess("REGSVR32 -s mydll.dll")

'Using then -s runs it in silent mode.

If ExitCode <> 0 Then
    MsgBox "It didn't work!"
End If


Private Declare Function OpenProcess Lib "kernel32" _
        (ByVal dwAccess As Long, ByVal fInherit As Integer, _
        ByVal hObject As Long) As Long
Private Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess As Long, lpExitCode As Long) As Long

Function RunCommandGetExitCode(CommandString As String) As Long

Const STILL_ACTIVE = &H103
Const PROCESS_QUERY_INFORMATION = &H400

Dim hProcess As Long
Dim RetVal As Long
hProcess = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(CommandString, vbHide))
Do
    GetExitCodeProcess hProcess, RetVal
    DoEvents: Sleep 100
Loop While RetVal = STILL_ACTIVE

RunCommandGetExitCode = RetVal

End Function
Wow look at that! He waits nearly an hour for a response then gets 3 within a flash.
Avatar of dbrckovi

ASKER

:)

Thanks, I'll try.

Where can I see if a file is registered?  ( Doesn't have to be from VB, I just need it for testing purposes )
Is just use shell?


ie shell "regsvr32 /s yourfullpath.dll"
Avatar of ___XXX_X_XXX___
___XXX_X_XXX___

Same as "Comment from ryancys" but short variant:

Public Declare Function FreeLibrary Lib "kernel32" (ByVal hLibModule As Long) As Long
Public Declare Function LoadLibrary Lib "kernel32" Alias "LoadLibraryA" (ByVal lpLibFileName As String) As Long
Public Declare Function GetProcAddress Lib "kernel32" (ByVal hModule As Long, ByVal lpProcName As String) As Long


Public Sub Sub_RegisterServer(lngHwnd As Long, strDllServerPath As String, boolRegister As Boolean)

 ' lngHwnd is hWnd of some form in your project.

On Error Resume Next

Dim lngResult As Long
Dim lngProcAddress As Long
dim strProc As String

 ' Check if file have length > 0
If FileLen(strDllServerPath) > 0 Then
    ' Load file for use in our app
   lngResult = LoadLibrary(strDllServerPath)
    ' If result differs from 0 then loading was successful
   If lngResult <> 0 Then
       ' If we must register
      If boolRegister = True Then
         strProc="DllRegisterServer"
      Else ' If we must unregister
         strProc="DllUnregisterServer"
      End If
       ' Get procedure address
      lngProcAddress = GetProcAddress(lngResult, strProc)
       ' Call the appropriate procedure in DLL file (DllRegisterServer or DllUnregisterServer)
       ' It's address is in lngProcAddress variable
      CallWindowProc lngProcAddress, lngHwnd, ByVal 0&, ByVal 0&, ByVal 0&
       ' Unload file
      FreeLibrary lngResult
   End If
End If

End Function


To register DLL use this:
Sub_RegisterServer Form1.hWnd,"C:\Windows\System\Mydll.dll",True

To unregister DLL use this:
Sub_RegisterServer Form1.hWnd,"C:\Windows\System\Mydll.dll",False



Best regards,
Georgi Ganchev
there is a simple way to register via code

put a declaration in the code as follows

Public function DllRegisterServer Lib "yourdll" Alias "DllRegisterServer"() as Long

bcos every component implements the DllRegisterServer and DllUnRegisterServer functions
so just give a alias name and call the function, this will register the component
I forgot to accept this answer before. It worked on the first try.

Thanks ryancys.

Thanks to everyone else for responding!