Now that you know how to find the IP address you need, learn how to call a remote application.
Integrating your IBM i with Windows allows you to provide complete and comprehensive solutions to your users. But there always seems to be a point where the user has to manually switch from the IBM i environment over to the Windows world, which may seem overly burdensome to the user. To assist with the transition from the green-screen to a Windows application, you can have the RPG program call the PC application, which makes the switch less involved for the user.
In order for RPG to be able to call a PC application, you need to be able to do three things:
- Identify the IP address of a computer from within an RPG program (previously discussed here)
- Call the remote application from within an RPG program
- Allow commands to be executed on the remote computer
Sending PC Commands from RPG
Suppose you have an RPG program that is preparing data on the IFS, perhaps using a PDF file or an Excel spreadsheet. When the RPG program has completed preparing the data, you would like to have the file open automatically in the appropriate application on the user's computer. The RPG program already has the file location information, so if you were to place the file into a location on the IFS that is accessible to the user, you could pass the file location to the client and have it open the file automatically.
Using STRPCO and STRPCCMD
If you are using iSeries Access as your 5250 emulator and you are only interested in executing commands on the client that is running the program, you could simply execute two commands to start up the application on the client: STRPCO and STRPCCMD.
If you know that the client is using a Windows operating system, you could execute the following commands on the command line to start the Notepad application on the client.
STRPCO
STRPCCMD PCCMD('NOTEPAD.EXE') PAUSE(*NO)
If you specify PAUSE(*YES), the RPG program will not continue until you exit the Notepad application on the Windows client. If you specify PAUSE(*NO), the RPG program will launch the application and continue on.
The Notepad application is a good proof-of-concept example and does not require that you specify the pathname of the application. But what about other applications, such as MS Word? You would need to know which version of Word you are using to determine the location of the executable. Or the users may not even be using a Microsoft product; they could be using an alternative, such as OpenOffice. Or you may be supporting multiple operating systems. Here are a few possibilities for Windows:
- Microsoft Office Word 2007: C:Program FilesMicrosoft OfficeOffice12WinWord.exe
- Microsoft Office Word 2003: C:Program FilesMicrosoft OfficeOffice11WinWord.exe
- OpenOffice.org 2.3: C:Program FileOpenOffice.org 2.3programswriter.exe
Opening the Appropriate Application Automatically
Fortunately, you can ignore these details for files that have applications associated with them by using the following commands to call the appropriate application that is associated with the files:
Operating System |
Command |
Windows |
start |
Linux |
xdg-open |
Mac |
open |
When you use these commands, the application that is associated with the file will automatically be used to open the file. So, to open a PDF file in a publicly accessible folder of the IFS on an IBM i with an IP address of 10.10.10.1 on a Windows client, you simply execute the following command at the command line of the Windows computer:
start 10.10.10.1Publichello.pdf
Here is the RPG code that will open the PDF on the IFS using STRPCO and STRPCCMD:
D commandString S 700A varying
D pcCommand S 700A varying
D ipAddress S 15A varying
D displayString S 52A
D* Prototype for QCMDEXC API
D ExecuteCommand...
D PR extPgm('QCMDEXC')
D argInCommand 65535A const options(*varsize)
D argInLength 15P 5 const
D**********************************************************************
/free
// To Automatically open the file with the associated application:
// LINUX: xdg-open
// MAC: open
// WINDOWS: start
commandString = 'STRPCO';
monitor;
ExecuteCommand(%trim(commandString):%len(%trim(commandString)));
on-error;
displayString = 'ERROR occurred on STRPCO using QCMDEXC!';
DSPLY displayString;
endmon;
// Build the PC Command to be executed.
pcCommand = 'start 10.10.10.1Publichello.pdf';
// STRPCCMD to open a PDF
commandString = 'STRPCCMD PCCMD('''
+ %trim(pcCommand)
+ ''') PAUSE(*NO)';
monitor;
ExecuteCommand(%trim(commandString):%len(%trim(commandString)));
on-error;
displayString = 'ERROR occurred on STRPCCMD using QCMDEXC!';
DSPLY displayString;
endmon;
*inLr = *ON;
/end-free
This requires the 5250 emulator to be installed on the client and will work only for the currently signed-on client, because you do not specify the device or network address. You may also notice that it does not ask for a user name or password.
In this simple example, the STRPCO command is executed prior to the STRPCCMD, but the STRPCO command only needs to be executed once for the job. If you were to call this program a second time within the same job, it would throw an exception, but because it is monitored, the program would still complete. You may wish to put a call to STRPCO in an application to be called during the sign-in process to call it only once.
STRPCO/STRPCCMD Advantages
- No additional client software is required for iSeries Access.
- The network address does not need to be identified.
- User name and password are not required.
STRPCO/STRPCCMD Disadvantages
- iSeries Access is required.
- Commands can be executed only on the client calling the program.
- Command maximum length is 123 characters.
Using RUNRMTCMD
You can USE The Run Remote Command (RUNRMTCMD) command to start applications on a remote location by using the remote executing (REXEC) service. Using RUNRMTCMD allows you to execute remote commands on multiple operating systems because it uses REXEC.
The remote computer does not have to be the client that is calling the program, so you will need to know the network location that you are attempting to execute the command on. If you are going to use the RUNRMTCMD command to call an application on the client calling the program, you could use the QDCRDEVD API that I discussed in my previous article, "Calling a PC Application from RPG." The primary reason to do this would be to take advantage of the large maximum command size of 2000 characters or simply to standardize the use of a more flexible option. But keep in mind that with RUNRMTCMD, you need to have an REXEC daemon running on the client; with STRPCCMD, you do not.
The remote network location can be specified using either SNA or TCP/IP and must have a REXEC daemon running in order to process the commands. In the next article in this series, I will discuss a couple of REXEC daemon options.
Besides opening files on the IFS, another common use for executing remote commands from RPG would be to open a Web browser to a specific URL. This could be useful for providing online documentation for the application or viewing a product associated with an order you are working with. This is also supported by the start, xdg-open, and open commands by passing the URL instead of the file name. This will open the URL with the default Web browser on a Windows computer:
start http://www.mcpressonline.com
Here is the classic fixed-format RPG code:
D**********************************************************************
DWS1 DS
D CMDSTRNG 1 700
D IP_ADDR 15
D DSPSTRING 52
D**********************************************************************
C/EJECT
C* Set the IP ADDRESS
C EVAL IP_ADDR = '10.10.10.10'
C* To automatically open the file with the associated application:
C* LINUX: xdg-open
C* MAC: open
C* WINDOWS: start
C MONITOR
C EVAL CMDSTRNG = 'RUNRMTCMD CMD('
C + '''start http://'
C + 'www.mcpressonline.com'')'
C + ' RMTLOCNAME('''
C + %TRIM(IP_ADDR)
C + ''' *IP)'
C + ' CCSID(1208)'
C + ' RMTUSER(rexecUser)'
C + ' RMTPWD(''pwdrexec'')'
C Z-ADD 700 CMDLEN
C CALL 'QCMDEXC'
C PARM CMDSTRNG
C PARM CMDLEN 15 5
C ON-ERROR
C EVAL DSPSTRING = 'ERROR occurred on QCMDEXC!'
C DSPSTRING DSPLY
C ENDMON
C MOVE *ON *INLR
And here is the same program in free-format code:
D commandString S 700A varying
D ipAddress S 15A varying
D displayString S 52A
D* Prototype for QCMDEXC API
D ExecuteCommand...
D PR extPgm('QCMDEXC')
D argInCommand 65535A const options(*varsize)
D argInLength 15P 5 const
D**********************************************************************
/free
// Set the IP Address
ipAddress = '10.10.10.10';
// To Automatically open the file with the associated application:
// LINUX: xdg-open
// MAC: open
// WINDOWS: start
monitor;
commandString = 'RUNRMTCMD CMD('
+ '''start http://'
+ 'www.mcpressonline.com'')'
+ ' RMTLOCNAME('''
+ %TRIM(ipAddress)
+ ''' *IP)'
+ ' CCSID(1208)'
+ ' RMTUSER(rexecUser)'
+ ' RMTPWD(''pwdrexec'') ';
ExecuteCommand(%trim(commandString):%len(%trim(commandString)));
on-error;
displayString = 'ERROR occurred on QCMDEXC!';
DSPLY displayString;
endmon;
*inLr = *ON;
/end-free
The IP address is hard-coded in these examples to show that you can specify the remote location that you want to execute the commands on, but you can also use the QDCRDEVD API to retrieve the IP address of the client that is running the application.
The RMTLOCNAME supports TCP/IP and SNA, which are determined by *IP and *SNA, respectively.
The CCSID parameter is optional. This is used to indicate the CCSID of the target system. If a CCSID is not specified, then the default value of 819 will be used for ISO-8859.
The RMTUSER and RMTPWD parameters are also optional. If the REXEC daemon does not require a user name and password, then you do not need to send them.
After the RUNRMTCMD command has been executed, a spool file will be generated containing any output from the command that was executed. The spool file may also contain error information if a problem occurs during execution of the RUNRMTCMD.
Using REXEC
The rexec() function is an alternative that provides the same functionality as the RUNRMTCMD, which allows you to receive information back from the execution of the remote command within the program instead of having to process the spool file. The resulting functionality of rexec() is no different than RUNRMTCMD because they both use REXEC, but the additional features introduce some additional complexity. I know that some of you are eager to provide the capability to call applications from RPG, so I decided to save the rexec() function for a future topic.
A Brief Note About Security
When using the REXEC services across the network, you should be aware that the password is sent across the network in clear text (unencrypted), so you will want to make sure that your network connection is secure.
LATEST COMMENTS
MC Press Online