|
Technical answers from the trenches |
|
ObjectPAL: Starting the User's Web Browser
| ||||
Posted: 24 August 2000 |
||||
(with input from Bertil Isberg and Bill Hannah)  |
Applies to: Paradox 7.32 and later |
|||
  |
Audience: Intermediate |
|||
Question: How can I start the user's web browser and have it load their preferred Start page?Answer: If you're using Paradox 9.0 or later, try this: method pushButton(var eventInfo Event) startWebBrowser( "" ) endMethod StartWebBrowser() is a System procedure that also lets you specify an alternate browser. For more information, see online Help. If you're using an older version of Paradox, there are several alternate approaches, including:
The rest of this article provides several examples demonstrating these techniques. Letting the system do it for youIf you're using Windows 95 OSR2 (or later) and want to open a specific URL (location), try this: method pushButton(var eventInfo Event) execute( "start www.example.com/targetpage.html" ) endMethod Some Windows 95 OSR2 users have reported problems with this; be sure to test in on your Windows 95 systems. Using Paradox's Internet Publishing FeaturesParadox 8 users can use a library provided with the Paradox Experts:
uses ObjectPAL
_LaunchBrowser( strFileName String, lWait Logical ) Logical
endUses
var
libWebPub Library ; Pointer to the publishing library.
strURL String ; Contains the target URL.
endVar
strURL = "www.corel.com"
if not libWebPub.open( expertsDir() + "\\HTMLIB01" ) then
errorShow( ERRTITLE, "Reason: Unable to load publishing " +
"library; see details..." )
else
if not libWebPub._launchBrowser( strURL, Yes ) then
errorShow( "Can't Preview HTML", "Reason: The " +
"browser failed to launch; see " +
"details..." )
endIf
endIf
For more information see the "Corel Paradox Internet Tools" Help file. Extracting the Browser from the RegistryIf you're using Paradox 7.32 or are designing an application for multiple Paradox versions, you may be able to extract the filename of the default browser from the user's Registry. As you're probably aware, the Registry maintains information about your system, the applications installed on it, and the configuration details needed to make everything work. You can use this to determine the fully qualified filename of the user's web browser, as shown in the following example:
method pushButton(var eventInfo Event)
var
str String ; name of registered browser
dyn Dynarray[] String ; holds elements of browser's name
si SmallInt ; position of command-line arguments
endVar
str = lower( getRegistryValue( "HTTP\\shell\\open\\command", "",
regKeyClassesRoot ) )
splitFullFilename( str, dyn )
si = search( dyn[ "NAME" ], " " )
if ( si > 0 ) then
dyn[ "NAME" ] = substr( dyn[ "NAME" ], 1, si - 1 )
endIf
str = dyn[ "DRIVE" ] + dyn[ "path" ] + dyn[ "NAME" ]
execute( str )
endMethod
Notice that the value returned from the Registry needs to be parsed before you can call execute(). This is necessary because the value returned by getRegistryValue() contains command-line switches used by DDE processes to load specific URL's into the browser. This can cause difficulties if you're simply trying to launch the browser. Other Registry keys also provide the same information. Here's another example:
method pushButton(var eventInfo Event)
var
strLevel String
exePos smallInt
endvar
; Tested in Paradox 7.32 and 8.0x on Win95 and NT4.0ws
strLevel = getRegistryValue( "HTMLFILE\\DEFAULTICON", "",
regKeyClassesRoot )
if strLevel <> "" then
exePos = search( upper( strLevel ), ".EXE" ) + 3
strLevel = strLevel.substr( 1, exePos ) +
" http://www.example.com/contact.html"
execute( strLevel )
else
msgInfo( "Browser Not Found",
"There is no Internet Browser (HTML) " +
"Registered with Windows!" )
endIf
endMethod
Please note that the Registry often returns long filenames. If these have caused problems for you in the past, test Registry-based approaches carefully. Because each major web browser uses different Registry locations, you may need to experiment to find an appropriate key. Much depends on the specific browser and version being used. Also, remember that the certain versions of Windows use profiles to control the activities of the end-user. Profiles can limit access to the Registry, causing these examples to fail. Again, test carefully. Because of these (and other) considerations, we typically avoid relying on the Registry in applications where the user's configuration is unknown or cannot be controlled. Finding the Associated ApplicationIn our experience, the most reliable and widely supported technique uses the Windows API to locate the application associated with HTML files. Specifically, FindExecutable() returns the name of the application associated with a specific file's extension, as shown in the following code sample:
uses SHELL32
FindExecutableA( lpFile CPTR, lpDirectory CPTR, lpResult CPTR ) CDOUBLE
endUses
method runBrowser( strURL String ) Logical
; -------------------------------------------------------------------------
; Attempts to start the user's registered browser. If successful, returns
; TRUE; otherwise, the return value is FALSE.
;
; StrURL is a string containing the fully qualified URL to load. To load
; the user's default start page, pass an empty string. For example:
;
; if runBrowser( "" ) then
; dosomething()
; endIf
;
; -------------------------------------------------------------------------
var
fs FileSystem ; used to delete the temp file.
loResult Logical ; value returned to calling process.
strFileName, ; Holds the name of the temp file.
strPathName, ; Holds location of the temp file.
strProgName String ; Holds the name of the registered browser
ts TextStream ; used to create the temp file.
endVar
const
MAXCHARS = 256; ; max length to accept back from the API
TESTFILE = "TESTFILE.HTML" ; name of the temp file.
endConst
; initialize local vars
strPathName = getAliasPath( ":PRIV:" )
strFileName = TESTFILE
strProgName = fill( " ", MAXCHARS ) ; very important!
loResult = FALSE ; assume failure to reduce code.
; create a dummy .HTML file.
ts.create( strPathName + "\\" + strFileName )
ts.writeLine( "<html><body>this is a test file.</body></html>" );
ts.commit()
ts.close()
; try to determine the registered browser
findExecutableA( strFileName, strPathName, strProgName );
if ( strProgName <> "" ) then
if ( strURL <> "" ) then
strProgName = strProgName + " " + strURL
endIf
if execute( strProgName ) then
loResult = TRUE
else
errorShow( "Browser Failed to Start", "Use [>>] for details..." )
endIf
else
msgStop( "Can't Start Browser", "Reason: You don't appear to " +
"have a web browser installed." )
endIf
; clean-up after ourselves.
fs.delete( strPathName + "\\" + strFileName )
return loResult
endMethod
While reviewing this code, keep the following points in mind:
Weighing the AlternativesGiven the subtleties between different versions of Paradox, Windows, and projects, compare the benefits and restrictions with each of these approaches. Experiment with each to find the one that best serves your specific needs. The number of solutions to this problem illustrates a very important point about Paradox and Windows in general; there are often many ways to solve a given problem. Because of this, there is no "one size fits all" approach. As with many other problems, the "recommended" solution is the one that best solves your problem. |
|||
|
||||||||
|
Copyright © 2000-2004, techtricks.com; All Rights Reserved. Acknowledgements, Disclaimers, Terms and Conditions. |
||||||||
|
Article last updated on 31 May 2003
|
||
|
|
||
|
[- End -]
|