Monday, May 23, 2005

how to regsvr32 through code

Dynamic web pages with ActiveX and VBScript:

Can I register/unregister an ActiveX through a right-click in Windows Explorer?

Tip given here:

REGEDIT4

[HKEY_CLASSES_ROOT\.dll]
"Content Type"="application/x-msdownload"
@="dllfile"

[HKEY_CLASSES_ROOT\dllfile]
@="Application Extension"

[HKEY_CLASSES_ROOT\dllfile\Shell\Register\command]
@="regsvr32.exe \"%1\""

[HKEY_CLASSES_ROOT\dllfile\Shell\UnRegister\command]
@="regsvr32.exe /u \"%1\""

[HKEY_CLASSES_ROOT\.ocx]
@="ocxfile"

[HKEY_CLASSES_ROOT\ocxfile]
@="OCX"

[HKEY_CLASSES_ROOT\ocxfile\Shell\Register\command]
@="regsvr32.exe \"%1\""

[HKEY_CLASSES_ROOT\ocxfile\Shell\UnRegister\command]
@="regsvr32.exe /u \"%1\""

Registering an OCX through code

Your DLLs and OCXs have a built in DllRegisterServer function so you can register them thru code as follows:

Declare Function RegMyDll Lib "MyDll.dll" Alias "DllRegisterServer"() as Long
[...]
Dim lResult as Long
lResult = RegMyDll()

To find if an Active X is registered you must look up the Windows registry. The easiest way is to search for a file name: MyOCX.DLL. If it is registred the reference to it will be in the registry under HKEY_CLASSES_ROOT\TypeLib\{ITS GUID} . This is non-trivial. Different types of DLLs register somewhat differently, and the exact entries created are based in part on the OS version.
Loading an OCX through code

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 Sub Form_Click()
If IsDLLAvailable("SHDOCVW.dll") Then
MsgBox "OK"
Else
Beep
' MsgBox ApiErrorText(Err.LastDllError)
' for ApiErrorText, see:
http://www.mvps.org/vb/index2.html?tips/formatmessage.htm
End If
End Sub

Function IsDLLAvailable(ByVal DllFilename As String) As Boolean
Dim hModule As Long
' attempt to load the module
hModule = LoadLibrary(DllFilename)
If hModule > 32 Then
FreeLibrary hModule ' decrement the DLL usage counter
IsDLLAvailable = True
End If
End Function

Checking if an OCX is available

If you know the OCX's CLSID, you can use this code:

Public Type CLSID '// This is the same as a GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(0 To 7) As Byte
End Type

'// Possible errors
Public Const E_INVALIDARG As Long = &H80070057
Public Const E_UNEXPECTED As Long = &H8000FFFF
Public Const CO_E_CLASSSTRING As Long = &H800401F3
Public Const E_OUTOFMEMORY As Long = &H8007000E
Public Const REGDB_E_WRITEREGDB As Long = &H80040151

Public Declare Function CLSIDFromProgID Lib "OLE32" (ByVal lpszProgID As String, pclsid As CLSID) As Long

Public Function IsProgIDRegistered(ByVal strProgID As String) As Boolean
'// a ProgID goes something like this: "MSComctlLib.Slider". Passing a
'// bogus value or a non-registered class will return FALSE
Dim pclsid As CLSID
Dim hResult As Long

On Error Resume Next

hResult = CLSIDFromProgID(StrConv(strProgID, vbUnicode), pclsid)
IsProgIDRegistered = (hResult = 0)
End Function

Instead of peeking in the Registry, we'll try to load the OCX, and catch an error if it's not available:

Function TestReg (ByVal sProgID As String) As Boolean
On Error GoTo Trap
Dim oMyObject As Object
Set oMyObject = CreateObject(sProgID)
TestReg = True
Trap:
End Function

0 Comments:

Post a Comment

<< Home