19
Fri, Apr
5 New Articles

Calling a PC Application from RPG, Part II

RPG
Typography
  • Smaller Small Medium Big Bigger
  • Default Helvetica Segoe Georgia Times

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:

 

  1. Identify the IP address of a computer from within an RPG program (previously discussed here)
  2. Call the remote application from within an RPG program
  3. 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.

Thomas Snyder

Thomas Snyder has a diverse spectrum of programming experience encompassing IBM technologies, open source, Apple, and Microsoft and using these technologies with applications on the server, on the web, or on mobile devices.

Tom has more than 20 years' experience as a software developer in various environments, primarily in RPG, Java, C#, and PHP. He holds certifications in Java from Sun and PHP from Zend. Prior to software development, Tom worked as a hardware engineer at Intel. He is a proud United States Naval Veteran Submariner who served aboard the USS Whale SSN638 submarine.

Tom is the bestselling author of Advanced, Integrated RPG, which covers the latest programming techniques for RPG ILE and Java to use open-source technologies. His latest book, co-written with Vedish Shah, is Extract, Transform, and Load with SQL Server Integration Services.

Originally from and currently residing in Scranton, Pennsylvania, Tom is currently involved in a mobile application startup company, JoltRabbit LLC.


MC Press books written by Thomas Snyder available now on the MC Press Bookstore.

Advanced, Integrated RPG Advanced, Integrated RPG
See how to take advantage of the latest technologies from within existing RPG applications.
List Price $79.95

Now On Sale

Extract, Transform, and Load with SQL Server Integration Services Extract, Transform, and Load with SQL Server Integration Services
Learn how to implement Microsoft’s SQL Server Integration Services for business applications.
List Price $79.95

Now On Sale

BLOG COMMENTS POWERED BY DISQUS

LATEST COMMENTS

Support MC Press Online

$0.00 Raised:
$

Book Reviews

Resource Center

  • SB Profound WC 5536 Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application. You can find Part 1 here. In Part 2 of our free Node.js Webinar Series, Brian May teaches you the different tooling options available for writing code, debugging, and using Git for version control. Brian will briefly discuss the different tools available, and demonstrate his preferred setup for Node development on IBM i or any platform. Attend this webinar to learn:

  • SB Profound WP 5539More than ever, there is a demand for IT to deliver innovation. Your IBM i has been an essential part of your business operations for years. However, your organization may struggle to maintain the current system and implement new projects. The thousands of customers we've worked with and surveyed state that expectations regarding the digital footprint and vision of the company are not aligned with the current IT environment.

  • SB HelpSystems ROBOT Generic IBM announced the E1080 servers using the latest Power10 processor in September 2021. The most powerful processor from IBM to date, Power10 is designed to handle the demands of doing business in today’s high-tech atmosphere, including running cloud applications, supporting big data, and managing AI workloads. But what does Power10 mean for your data center? In this recorded webinar, IBMers Dan Sundt and Dylan Boday join IBM Power Champion Tom Huntington for a discussion on why Power10 technology is the right strategic investment if you run IBM i, AIX, or Linux. In this action-packed hour, Tom will share trends from the IBM i and AIX user communities while Dan and Dylan dive into the tech specs for key hardware, including:

  • Magic MarkTRY the one package that solves all your document design and printing challenges on all your platforms. Produce bar code labels, electronic forms, ad hoc reports, and RFID tags – without programming! MarkMagic is the only document design and print solution that combines report writing, WYSIWYG label and forms design, and conditional printing in one integrated product. Make sure your data survives when catastrophe hits. Request your trial now!  Request Now.

  • SB HelpSystems ROBOT GenericForms of ransomware has been around for over 30 years, and with more and more organizations suffering attacks each year, it continues to endure. What has made ransomware such a durable threat and what is the best way to combat it? In order to prevent ransomware, organizations must first understand how it works.

  • SB HelpSystems ROBOT GenericIT security is a top priority for businesses around the world, but most IBM i pros don’t know where to begin—and most cybersecurity experts don’t know IBM i. In this session, Robin Tatam explores the business impact of lax IBM i security, the top vulnerabilities putting IBM i at risk, and the steps you can take to protect your organization. If you’re looking to avoid unexpected downtime or corrupted data, you don’t want to miss this session.

  • SB HelpSystems ROBOT GenericCan you trust all of your users all of the time? A typical end user receives 16 malicious emails each month, but only 17 percent of these phishing campaigns are reported to IT. Once an attack is underway, most organizations won’t discover the breach until six months later. A staggering amount of damage can occur in that time. Despite these risks, 93 percent of organizations are leaving their IBM i systems vulnerable to cybercrime. In this on-demand webinar, IBM i security experts Robin Tatam and Sandi Moore will reveal:

  • FORTRA Disaster protection is vital to every business. Yet, it often consists of patched together procedures that are prone to error. From automatic backups to data encryption to media management, Robot automates the routine (yet often complex) tasks of iSeries backup and recovery, saving you time and money and making the process safer and more reliable. Automate your backups with the Robot Backup and Recovery Solution. Key features include:

  • FORTRAManaging messages on your IBM i can be more than a full-time job if you have to do it manually. Messages need a response and resources must be monitored—often over multiple systems and across platforms. How can you be sure you won’t miss important system events? Automate your message center with the Robot Message Management Solution. Key features include:

  • FORTRAThe thought of printing, distributing, and storing iSeries reports manually may reduce you to tears. Paper and labor costs associated with report generation can spiral out of control. Mountains of paper threaten to swamp your files. Robot automates report bursting, distribution, bundling, and archiving, and offers secure, selective online report viewing. Manage your reports with the Robot Report Management Solution. Key features include:

  • FORTRAFor over 30 years, Robot has been a leader in systems management for IBM i. With batch job creation and scheduling at its core, the Robot Job Scheduling Solution reduces the opportunity for human error and helps you maintain service levels, automating even the biggest, most complex runbooks. Manage your job schedule with the Robot Job Scheduling Solution. Key features include:

  • LANSA Business users want new applications now. Market and regulatory pressures require faster application updates and delivery into production. Your IBM i developers may be approaching retirement, and you see no sure way to fill their positions with experienced developers. In addition, you may be caught between maintaining your existing applications and the uncertainty of moving to something new.

  • LANSAWhen it comes to creating your business applications, there are hundreds of coding platforms and programming languages to choose from. These options range from very complex traditional programming languages to Low-Code platforms where sometimes no traditional coding experience is needed. Download our whitepaper, The Power of Writing Code in a Low-Code Solution, and:

  • LANSASupply Chain is becoming increasingly complex and unpredictable. From raw materials for manufacturing to food supply chains, the journey from source to production to delivery to consumers is marred with inefficiencies, manual processes, shortages, recalls, counterfeits, and scandals. In this webinar, we discuss how:

  • The MC Resource Centers bring you the widest selection of white papers, trial software, and on-demand webcasts for you to choose from. >> Review the list of White Papers, Trial Software or On-Demand Webcast at the MC Press Resource Center. >> Add the items to yru Cart and complet he checkout process and submit

  • Profound Logic Have you been wondering about Node.js? Our free Node.js Webinar Series takes you from total beginner to creating a fully-functional IBM i Node.js business application.

  • SB Profound WC 5536Join us for this hour-long webcast that will explore:

  • Fortra IT managers hoping to find new IBM i talent are discovering that the pool of experienced RPG programmers and operators or administrators with intimate knowledge of the operating system and the applications that run on it is small. This begs the question: How will you manage the platform that supports such a big part of your business? This guide offers strategies and software suggestions to help you plan IT staffing and resources and smooth the transition after your AS/400 talent retires. Read on to learn: