by Shannon O'Donnell
If you're an RPG programmer and you're just now getting into writing HTML, chances are you've discovered a lack of functionality that you used to have in RPG--specifically, the ability to execute commands or call other programs from HTML. Because HTML runs in a browser, and because of the inherent, built-in security of browsers, it's difficult to call other programs from within HTML code. But sometimes you need to. What's the solution? Java Script and SHELL!
The SHELL Command
Visual Basic and C++ programmers have known about the SHELL command for years. SHELL is a Microsoft Win32 API that allows Windows programmers to execute commands from a Shell or command line. The SHELL API, like all Win32 APIs, is not easy to use--at least not for non-Windows programmers. However, you don't have to understand all of its syntax and features in order to use it. You just have to know how to use it.
In this article, I'll show you how to use the SHELL API from within HTML to perform several common functions. Since you want to know more about how to actually use it from your own HTML applications, and probably couldn't care less about the syntax and background of the SHELL command, I'm going to skip the history lesson. If you want to learn more about the syntax of the SHELL command, take a look at the Microsoft's Web site. Just search on SHELLEXECUTE, and you'll find pages and pages about the SHELL API, its syntax, and how to use it.
To start, open up Microsoft Notepad.exe and create a new HTML document.
<HTML> <HEAD> </HEAD> <BODY> <FORM name="Form1"> <CENTER> <BR><BR> <H1>Execute PC Commands From HTML </H1> <BR><BR> <File Name to Open:> <Input type="text" name="filename"/> <BR><BR> <Input type="Button" name="Button1" value="Run Notepad.exe" /> <BR><BR> <Input type="Button" name="Button2" value="Run Notepad.exe with Parameters" /> </CENTER> </BODY> </FORM> </HTML>
Save this document with a name like DemoSHELLAPI.htm and then open it in a browser. It should look something like the one shown here:
At this point, your HTML code doesn't do anything, because you haven't added any code behind it. Let's do that now.
Adding the Power
Modify the DemoSHELLAPI.htm file to add the following code between the opening and closing HEAD tags:
What That Does
Next, if the user clicked the button to run NotePad.exe and to pass it a parameter of the name of a file to open, then the variable inputparms will contain the value hasParms, which is defined in the "onClick" event of the second button on this Form. If inputparms is not blank, the script will retrieve the value the user entered in the input field on the form and set the variable commandParms equal to that value.
Finally, we'll execute the command to run, Notepad.exe, by calling the ShellExecute method of the SHELL API. We'll pass the ShellExecute method several parameters, each individually enclosed within double quotation marks ("). The first parameter is the command to be executed, in this case Notepad.exe. The second parameters are the input parameters, if any, to the command. The other parameters are not important to this discussion, but if you're interested in using them, be sure to check out the SHELL documentation on Microsoft's Web site.
Save this file and reopen it in your browser and try it out.
Using ActiveX objects from within HTML gives you the power and flexibility you need to code professional and useful applications. Explore the SHELL API and some of the other Win32 APIs and see how you can expand on them to add even more power to your applications.
Contact the Editors
Last Updated: 5/22/02
Copyright © 1996-2008 Guild Companies, Inc. All Rights Reserved.