by Mark Trescowthick - GUI Computing
Some many moons ago, I wrote a piece (in fact, two) on using Autorun. Bizarrely enough, those two pieces still generate more email than most everything else - it seems that Autorun is not well documented or understood, even by those of us who are doomed to use it.
By far the most common question of recent date is "How do I make Autorun show an HTML file?". This sounds easy but, in fact, is a bit of a pain - Autorun does not honour file associations! Which, of course, means that you must first find the user's default browser, then use that to display the HTML. Not complex, but certainly a nuisance.
So, having answered the question a dozen or so times in the abstract, I decided to write a tiny VB5 app to do it for me. I've called it HTMLRunner.
The first problem, of course, was that discovering the default browser meant retrieving a Registry Key. And that in turn meant fiddling with the API until I got that happening… unless of course, I could beg borrow or steal a thin wrapper for the particular API calls I needed from somewhere. I naturally turned to Jim Karabatsos (a funny place to search for a "thin" anything, some might uncharitably suggest!) and borrowed his ReadRegValue Function. This is pretty self-explanatory, and is included below :
Declare Function RegOpenKeyEx Lib "advapi32.dll" Alias "RegOpenKeyExA" _ ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, _ ByVal samDesired As Long, phkResult As Long) As Long Declare Function RegQueryValueEx Lib "advapi32.dll" Alias "RegQueryValueExA" _ ByVal hKey As Long, ByVal lpValueName As String, _ ByVal lpReserved As Long, lpType As Long, ByVal lpData As String, _ lpcbData As Long) As Long Declare Function RegCloseKey Lib "advapi32.dll" (ByVal hKey As Long) As Long Public Const HKEY_CURRENT_USER = &H80000001 Public Const HKEY_LOCAL_MACHINE = &H80000002 Public Const HKEY_CLASSES_ROOT = &H80000000 Public Const HKEY_CURRENT_CONFIG = &H80000005 Public Const HKEY_DYN_DATA = &H80000006 Public Const REG_SZ = 1 Public Const ERROR_SUCCESS = 0& Public Const KEY_QUERY_VALUE = &H1 Public Function ReadRegValue(ByVal hKey As Long, _ ByVal SubKey As String, _ ByVal KeyName As String, _ ByVal Default As String) _ As String Dim nhReg As Long Dim nRegType As Long Dim sResult As String Dim nLenResult As Long sResult = String$(500, Chr$(0)) nLenResult = 500 nRegType = REG_SZ ReadRegValue = Default If RegOpenKeyEx(hKey, SubKey, 0, KEY_QUERY_VALUE, nhReg) = ERROR_SUCCESS Then If RegQueryValueEx(nhReg, KeyName, 0&, nRegType, sResult, nLenResult) = ERROR_SUCCESS Then If nRegType = REG_SZ Then ReadRegValue = Left$(sResult, nLenResult - 1) End If End If RegCloseKey nhReg End If End Function
Having done that, it remained to discover just where this particular varmint of a Registry Key lived. A bit of fiddling with RegEdit (why is it that that appears almost at the top of the Run… list for every developer I know??) and I discovered that the key in question is HKEY_CLASSES_ROOT\ HTTP\shell\open\command.
I also discovered, unfortunately, that Netscape appends a "%1%" to the string it stores there - presumably a throwback to the "good" old days of Win 3.1! So I had to strip that from the end of the string.
Otherwise, it couldn't be more straightforward. I simply take a command line, which I assume to be a valid HTML file in the same directory as the application, discover the user's default browser and use that to launch the HTML.
Sub Main() Dim sCmdLine As String Dim sBrowserStartup As String Dim rval As Variant sBrowserStartup = String$(500, Chr$(0)) sCmdLine = Command() If Len(sCmdLine) = 0 Then MsgBox "No Command Line Specified", vbCritical, "HTML Runner" Exit Sub End If If Right(App.Path, 1) = "\" Then sCmdLine = App.Path & sCmdLine Else sCmdLine = App.Path & "\" & sCmdLine End If sBrowserStartup = ReadRegValue(HKEY_CLASSES_ROOT, "HTTP\shell\open\command", vbNullString, vbNullString) If InStr(1, sBrowserStartup, """%1""") Then sBrowserStartup = Mid(sBrowserStartup, 1, InStr(1, sBrowserStartup, """%1""") - 1) End If Shell sBrowserStartup & sCmdLine, vbMaximizedFocus End Sub
Easy, but handy… I've already used it a couple of times. But be warned, my extensive testing included only IE3 and Communicator. Other browsers may fill this key differently, and you would need to make allowance for that.
A .ZIP of the source and executable is available for for download.