Club Tech iSeries Programming Tips Newsletter

Article ID: 20084
An iSeries Network Publication           
http://www.iSeriesNetwork.com
Home of iSeries NEWS Magazine                    
Editor: Scott Klement
Issue 198                                               
March 10, 2005

SPONSORED BY SEQUEL
                           TOP TEN REASONS
                   Why You Should RETIRE QUERY/400
SEQUEL is the BETTER way! Check out the Top Ten list and a video
Demo that shows how SEQUEL automatically converts QUERY objects
into SEQUEL views and reports. Ask for a FREE TRIAL today!
http://iseries.pentontech.com/t?ctl=4C32:103C2

**********************************************************************

EDITOR'S NOTE:

I will be at COMMON in Chicago next week. If you'll be there and 
happen to see me, please come over and say "hi." It's always good to 
hear from my readers!

 -- Scott Klement, Club Tech iSeries Programming Tips Editor

**********************************************************************

THIS WEEK: 
> Launch Commands Longer than 123 Characters on the PC
> Base64 Encode/Decode Utility
Pro and Pro VIP Edition Items: 
> Open Source SOAP with Axis
> Consistent Error Handling Part 2: Using a *PSSR in a Subprocedure

1. LAUNCH COMMANDS LONGER THAN 123 CHARACTERS ON THE PC

Q: STRPCCMD does a fine job of running programs on my PC from the iSeries. However, the command string can only be 123 characters long. That's not enough! How can I open a Word document that's located in a huge directory structure? A: The venerable STRPCCMD command has been around for a long time. When it was first made available, 123 characters was usually long enough. Now, it's definitely not. Here are some suggestions for overcoming this limitation:

CREATE A BATCH FILE

You could create a batch file on the PC that contains the longer name and then execute that batch file with STRPCCMD. It's even possible to create the batch file on the fly. For example, you can use STRPCCMD to create a batch file called MYFILE.BAT in the C:\DATA directory. You create it using the MS-DOS echo command to write data to a file on disk. The following sample code uses this technique to launch a long-named Word document:
STRPCCMD PCCMD('cmd /c ECHO SET MYDOC=c:\documents and settings> +
                C:\DATA\MYFILE.BAT')
STRPCCMD PCCMD('cmd /c ECHO SET MYDOC=%MYDOC%\alexander.acctdom\my +
                documents>> C:\DATA\MYFILE.BAT')
STRPCCMD PCCMD('cmd /c ECHO SET MYDOC=%MYDOC%\weekly\credit memos\acme +
                inc>> C:\DATA\MYFILE.BAT')
STRPCCMD PCCMD('cmd /c ECHO SET MYDOC=%MYDOC%\memo84612.doc>> +
                C:\DATA\MYFILE.BAT')
STRPCCMD PCCMD('cmd /c ECHO start "%MYDOC%">> C:\DATA\MYFILE.BAT')
STRPCCMD PCCMD('C:\DATA\MYFILE.BAT')
The idea for this technique came from a message posted to the iSeries Network forums by Herman Van der Staey. Thanks, Herman!

USE RUNRMTCMD INSTEAD

Another problem with STRPCCMD, aside from the length limitation, is that it relies on 5250 emulation from iSeries Access in order to determine which computer to connect to and which user to run the commands as. Not everyone uses iSeries Access. In fact, not everyone runs Windows! Another IBM-supplied way to run commands on another computer is the RUNRMTCMD command. This allows up to 2000 characters to be specified for the command to run on the PC. When used over TCP/IP, it uses the RExec protocol that's widely used on other platforms as well. You're not limited to Windows! To use this command, you first need to run an incoming remote command service on the PC. If you're running Windows, there's a free RExec service available in iSeries Access that you can install. Once installed, it needs to be enabled under "Services" in the control panel. It will be called "iSeries Incoming Remote Command" or something similar. If you want to do things like open Word documents or Web browsers, make certain that you go into the properties of the service and check the "Allow programs to interact with the desktop" checkbox. If you don't have iSeries Access, or don't want to use it, search the Internet for an RExec server for Windows. There are several other vendors besides IBM who offer them. If you're running a variant of Unix like FreeBSD or Linux, you can also enable an RExec server, usually called "rexecd." Check the documentation for your operating system for more information. Unlike STRPCCMD, the RUNRMTCMD command does require that you specify the host to connect to as well as a userid and password for that host. Although this can be a little cumbersome, it does let you run remote commands rom batch jobs. That would be impossible with STRPCCMD! When running it from within an interactive session, you can get some of the same functionality that STRPCCMD offers by writing a little CL program. In that program, you can retrieve the current userid using the RTVJOBA command, and you can retrieve the IP address of the PC that's connected to the 5250 session using the QDCRDEVD API. The following sample code demonstrates this technique:
PGM

    DCL VAR(&RCVVAR) TYPE(*CHAR) LEN(1000)
    DCL VAR(&RCVLEN) TYPE(*CHAR) LEN(4)
    DCL VAR(&DEVICE) TYPE(*CHAR) LEN(10)
    DCL VAR(&ERRCODE) TYPE(*CHAR) LEN(8)
    DCL VAR(&IPADDR)  TYPE(*CHAR) LEN(15)
    DCL VAR(&USERID) TYPE(*CHAR) LEN(10)
    DCL VAR(&PASSWD) TYPE(*CHAR) LEN(15) VALUE('mypasswrd')
    /* FIND OUT THE IP ADDRESS OF THE PC THAT IS +
       CONNECTED TO THIS JOB.                    */

    RTVJOBA JOB(&DEVICE) USER(&USERID)

    CHGVAR VAR(%BIN(&RCVLEN)) VALUE(1000)
    CHGVAR VAR(%BIN(&ERRCODE 1 4)) VALUE(0)

    CALL PGM(QDCRDEVD) PARM(&RCVVAR    +
                            &RCVLEN    +
                            'DEVD0600' +
                            &DEVICE    +
                            &ERRCODE   )

    CHGVAR VAR(&IPADDR) VALUE(%SST(&RCVVAR 878 15))
    /* RUN A COMMAND ON THAT PC -- TIMEOUT AFTER 5 SECS      +
       ANY MESSAGES FROM THE PC GO TO A QSYSPRT SPOOLED FILE */

    OVRPRTF FILE(QSYSPRT) HOLD(*YES)

    RUNRMTCMD  CMD('start "c:\documents and settings\alexander.acctdom+
                   \my documents\weekly\credit memos\acme inc+
                   \memo84612.doc"') +
               RMTLOCNAME(&IPADDR *IP) +
               RMTUSER(&USERID) +
               RMTPWD(&PASSWD) +
               WAITTIME(5)

    DLTOVR  FILE(QSYSPRT)
    /* SHOW THE PC'S MESSAGES TO THE USER, THEN DELETE IT */

    DSPSPLF FILE(QSYSPRT) JOB(*) SPLNBR(*LAST)
    DLTSPLF FILE(QSYSPRT) JOB(*) SPLNBR(*LAST)

ENDPGM
One of the unfortunate drawbacks to RUNRMTCMD is that any messages from the command running on the PC get written to a spooled file. This means that any errors that occur on the PC are difficult to detect. That's why, in the sample code above, I've told it to display the spooled file that this command creates and then delete it. That way, you'll see any messages from the PC, and you won't end up filling your spool with responses from RUNRMTCMD.

USE THE REXEC COMMAND IN QSHELL

There's also an REXEC utility available in QShell. You could use this instead of RUNRMTCMD. It also uses the RExec protocol and interfaces with the same incoming remote command service on the PC. Its advantage over RUNRMTCMD is that you can control where any messages from the PC get sent to. You could even write a QShell script that processes these messages and handles errors accordingly. You can learn more about this command by reading the following page in the Information Center: http://iseries.pentontech.com/t?ctl=4C27:103C2

USE THE REXEC() API

Finally, perhaps the best way to run a remote command is from an RPG or C program using the rexec() API. It also uses the same RExec protocol as the RUNRMTCMD command, but like the QShell version, it gives you complete control over where any messages go. I demonstrated the rexec() API in the June 17, 2004, issue of this newsletter. Professional members of the iSeries Network can read that article at the following link: http://iseries.pentontech.com/t?ctl=4C23:103C2 ********************************************************************** SPECIAL ISERIES NETWORK OFFER Know the differences between RFID and bar code technology and attend this FREE iSeries Network Webcast event. On 3/23 at 12 p.m. Eastern expert Sharon Hoffman presents how RFID works, how it differs from bar code solutions, the potential benefits and pitfalls of RFID solutions, and more. Sponsored by T.L. Ashford. Seating is limited -- sign up today! http://iseries.pentontech.com/t?ctl=4C22:103C2 **********************************************************************

2. BASE64 ENCODE/DECODE UTILITY

Base64 is a popular scheme used to encode binary data so that it can be transferred over media that requires plain text. For example, when you want to send a picture through e-mail, your e-mail reader will encode the picture using base64, transmit the base64-encoded document, and then the recipient's e-mail software will decode it so that the recipient can view it. This same encoding scheme is also commonly used when storing binary data in HTTP cookies. I wrote some ILE RPG routines that will encode and decode base64 data so that I can use it in my programs. I've turned those routines into an open source service program and put it on my Web site so you can download it and use it in your programs as well. This utility is located in the open source section of my Web site. If you're interested, you can download it from the following link: http://iseries.pentontech.com/t?ctl=4C34:103C2 I demonstrated how to create an e-mail message with a base64-encoded attachment in a recent article that I wrote for iSeries NEWS magazine. If you're interested in that article, it's called "RPG and the IFS: Text Files in the World" and was published in the January 2005 issue. If you have a ProVIP account on the iSeries Network, you can read it at the following link: http://iseries.pentontech.com/t?ctl=4C30:103C2 ********************************************************************** SPECIAL ISERIES NETWORK OFFER To propose, budget, and plan an electronic forms implementation, you need hard data. In this Webcast recording, Dan Elam presented models & metrics for generating accurate cost justification plus guidance on quantifying "soft dollar" benefits. Additionaly Environment One's Don McKay and Quadrant Software presented a real life case study. View this replay now! http://iseries.pentontech.com/t?ctl=4C25:103C2 ***************** ABOUT ISERIES NETWORK NEWSLETTERS ****************** WHY DID I GET ONLY TWO TIPS WHEN FOUR ARE LISTED? The last two tips in this newsletter are premium content that is available only to Pro and Pro VIP members of the iSeries Network. If you're not a Pro or Pro VIP member, you will see the titles of these two extra tips but will not be able to read their content. Please consider upgrading your membership. If you do, you will receive not only the two bonus tips but also a subscription to iSeries NEWS magazine -- and the cost can be as low as USD $29/year! More info about the benefits is here: http://iseries.pentontech.com/t?ctl=4C26:103C2 The Subscribe/Join page is here: http://iseries.pentontech.com/t?ctl=4C35:103C2 If you're a Pro or Pro VIP member and you're not getting the extra tips, here's what you need to do: a) Follow this link: http://iseries.pentontech.com/t?ctl=4C2E:103C2 b) Make certain that the e-mail address you're using matches the e- mail that you're subscribed to this newsletter with. If not, unsubscribe using the instructions at the bottom of the newsletter and then resubscribe through the membership profile page. c) If you're still having problems, contact mailto:service@iseriesnetwork.com . ***************** ABOUT ISERIES NETWORK NEWSLETTERS ****************** Club Tech iSeries Programming Tips Newsletter is published weekly on Thursday, except for the first Thursday of the month. Club Tech iSeries Systems Management Newsletter is published on alternate Wednesdays. NEWS Wire Daily brings you iSeries industry news, tech tips, product news, and IBM announcements Monday through Thursday. All are *FREE OF CHARGE*! IF YOU HAVE a technical question, please submit it to mailto:clubtechprogrammingtips@iseriesnetwork.com or post it in the appropriate iSeriesNetwork.com forum. This issue of the Programming Tips newsletter was edited by Scott Klement, at mailto:sklement@iseriesnetwork.com . FOR NEW SUBSCRIPTIONS, you can subscribe by joining the iSeries Network with our handy Web form at http://iseries.pentontech.com/t?ctl=4C35:103C2 . TO CHANGE YOUR E-MAIL ADDRESS, modify your iSeries Network profile at http://iseries.pentontech.com/t?ctl=4C2E:103C2 . IF YOU HAVE PROBLEMS subscribing or unsubscribing or have questions about your subscription, e-mail customer service at mailto:prgrmtips@iseriesnetwork.com . IF YOU WANT TO SPONSOR a Club Tech iSeries Programming Tips Newsletter, please contact your iSeries Network sales manager. Click here for details: http://iseries.pentontech.com/t?ctl=4C2D:103C2 . iSeries is a trademark of International Business Machines (IBM) Corporation and is used by Penton Media, Inc., under license. iSeries NEWS, iSeriesNetwork.com, and the iSeriesNetwork.com newsletters are published independently of IBM, which is not responsible in any way for the content. Penton Media, Inc., is solely responsible for the editorial content and control of iSeries NEWS, iSeriesNetwork.com, and the iSeriesNetwork.com newsletters. ___________________________ Copyright 2005 Penton Technology Media, 221 E. 29th St., Loveland, CO 80538 (800) 793-5714 or (970) 203-2894 http://www.iSeriesNetwork.com

ProVIP Sponsors

ProVIP Sponsors