Professional Documents
Culture Documents
Operating system programs and other application programs can be called from SAP in
one of four ways.
The first two methods depend on the location of the application program – whether it
resides on the local PC, or the application server. Both are easy to implement. The third
way, using ABAP objects relies on the application being called being an OLE server.
The fourth I regard as a kind of back door....
An OLE server uses object technology for sharing information and services across
process and machine boundaries.
A program can be run on the local PC by using function modules ‘WS_EXECUTE' (for
versions < 4.6c) or ‘GUI_EXEC' for versions of 4.6c or greater:
Code:
Form Check_Directory using pu_outdir.
*
* Check that the directory exists and if it doesn't build it up.
*
Data: Begin of t_path occurs 0,
directory(30) type c,
End of t_path,
*
begin of t_command occurs 0,
cline(72) type c,
End Of t_command,
Code:
Call Function 'GUI_EXEC'
Exporting
Command = W_Batfile.
If sy-subrc <> 0.
Move c_could_not_execute to pc_status.
Else.
*
* Get the results back....
*
Zap t_batch.
Move w_txtfile to string_filename.
*
* If the file is being loaded from a floppy, there is a
* timing problem where this code is executed too quickly.
*
* Loop until we get some data or we've tried long enough
*
Do.
In either case no results are passed back to the calling ABAP. The program is executed.
Full stop.
In certain cases, the ABAP program can be dependant on the results from the program
that has just been executed external to SAP. Therefore you need to be able to tell when a
programs finished or not.
In object oriented languages such as VB, you can test for the application ending by
trying to activate it's window. If the window can be activated it's still there, so go to
sleep for a little while and try and activate it again. When you can't activate it the
program has finished.
It's just as easy in ABAP, but without using any object technology.
What you do is to write a small batch file which essentially consists of:
Code:
@echo off
echo *** > c: un.flg
Myprog
Del c: un.flg
This batch program creates a file called run.flg which exists for the duration of the
execution of the program Myprog. The file is then deleted when the program has
completed.
Within your ABAP you would kick of the execution of the batch file and wait for the
file Run.Flg to disappear. Due to the speed of processing you may have to code to wait
for the file to appear and then wait for it to disappear.
Code:
*
* Build up the file names...
*
Concatenate c_filenme '.' c_batext into w_batfile.
Concatenate c_filenme '.' c_txtext into w_txtfile.
*
* And the batch file...
*
Concatenate 'dir' pu_filename '>' w_txtfile
into w_command
separated by ' '.
Zap t_batch.
Move '@echo off' to t_batch-line.
Append t_batch.
Move w_command to t_batch-line.
Append t_batch.
Move 'Exit' to t_batch-line.
Append t_batch.
*
Move w_batfile to string_filename.
Call Function 'GUI_EXEC'
Exporting
Command = W_Batfile.
If sy-subrc <> 0.
Move c_could_not_execute to pc_status.
Else.
*
* Get the results back....
*
Zap t_batch.
Move w_txtfile to string_filename.
*
* If the file is being loaded from a floppy, there is a
* timing problem where this code is executed too quickly.
*
* Loop until we get some data or we've tried long enough
*
Do.
Call Function 'GUI_UPLOAD'
Exporting
Filename = string_filename
Filetype = 'ASC'
Tables
Data_Tab = t_batch
Exceptions
File_Open_Error =1
File_Read_Error =2
No_Batch =3
Gui_Refuse_Filetransfer =4
Invalid_Type =5
No_Authority =6
Unknown_Error =7
Bad_Data_Format =8
Header_Not_Allowed =9
Separator_Not_Allowed = 10
Header_Too_Long = 11
Unknown_Dp_Error = 12
Access_Denied = 13
Dp_Out_Of_Memory = 14
Disk_Full = 15
Dp_Timeout = 16
Others = 17.
*
* Anything in the batch listing ?
*
Describe table t_batch lines w_lines.
If w_lines <> 0.
Exit.
EndIf.
*
* Exit if we've tried 10 times.
*
If sy-index > 10.
Move 1 to sy-subrc.
Exit.
EndIf.
EndDo.
Move w_batfile to rlgrap_filename.
Call Function 'GUI_DELETE_FILE'
Exporting
File_Name = rlgrap_filename
Exceptions
Failed =1
Others = 2.
This is a little more complicated than running a program on the local PC as SAP is stricter in what
it allows you to run on an Application server.
There are two transactions and a function module that we are interested in. These are:
External commands within SAP are given an arbitrary name by which they are referenced when
you wish to use them. Alongside the command is the operating system that the command is for.
These two values allow the same command to be defined for more than one operating system.
The only difference being the actual command line parameters themselves.
Note that there are 4 entries for the command ‘RRR_ROUTER_START' – one for OS/400 one for
SunOs, one for Unix and one for NT. The difference is in the the OS command.
Code:
*Eject
***********************************************************************
*
* Procedure: Get_Server_File
*
* Purpose: Gets File Time and size details for server file
*
* Entry: File name to get details for
*
* Exit: File Create Date
* File modification time,
* File Size
* Status: Ok
* File Not Found
* Could not execute
*
* Called By: Perform Get_Server_file using w_filename
* changing w_date
* w_file_time
* w_file_length
* w_status
*
* Calls:
*
* Modification History:
*
* Date Reason Version Who
*
Form Get_Server_File using pu_filename like zfilepath-initpath
changing pc_date
pc_time
pc_size
pc_status type status_code.
*
Constants: c_Unix_Months(36) type c value
'JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC'.
Data: Begin of t_splits occurs 0,
line(132) type c,
End of t_splits,
t_exec_protocol like btcxpm occurs 0 with header line,
w_lines type i,
w_month type i,
w_nummnth(2) type n,
w_filename like sxpgcolist-Parameters.
*
Move pu_filename to w_filename.
Call Function 'SXPG_COMMAND_EXECUTE'
Exporting
Commandname = 'LIST_DB2DUMP'
Additional_Parameters = w_filename
Operatingsystem = 'UNIX'
Stdout = 'X'
Stderr = 'X'
Terminationwait = 'X'
Tables
Exec_Protocol = t_exec_protocol
Exceptions
No_Permission =1
Command_Not_Found =2
Parameters_Too_Long =3
Security_Risk =4
Wrong_Check_Call_Interface =5
Program_Start_Error =6
Program_Termination_Error =7
X_Error =8
Parameter_Expected =9
Too_Many_Parameters = 10
Illegal_Command = 11
Wrong_Asynchronous_Parameters = 12
Cant_Enq_Tbtco_Entry = 13
Jobcount_Generation_Error = 14
Others = 15.
If sy-subrc <> 0.
Move c_could_not_execute to pc_status.
Else.
*
* There should be only one line of text returned.
*
Describe table t_exec_protocol lines w_lines.
Read Table t_exec_protocol index 1.
Condense t_exec_protocol-Message.
Split t_exec_protocol at ' ' into table t_splits.
*
COMMANDNAME.
This parameter is the name of the command that you wish to run, displayed in the first
column of SM69 or SM49.
ADDITIONAL_PARAMETERS.
Additional command line parameters that are provided to the command after the
command name. For example, the command:
Code:
Pkzip -Par *.* test.zip
Code:
-Par *.* test.zip
OPERATINGSYSTEM.
This is the operating system that you wish to execute the command for. (Remember the
four different versions of the command pointed out above ?). This is an optional
parameter and defaults to the actual operating system your version of SAP is running
under.
TargetSystem.
An RFC targeted host system. This enables you to run the command on a remote
system. Again, an optional parameter.
These flags allow you to redirect the STDOUT and STDERR pipes. Optional
parameters that default to using STDOUT and STDERR.
TERMINATIONWAIT.
What it says – waits for the program to complete before returning to ABAP. An optional
parameter that defaults to True. (ie SAP waits)
TRACE.
An optional Boolean. Turn this on and an RFC trace file is generated. This defaults to
false.
STATUS.
This is the start or end status of the external command. God knows what the Start status
of a program is all about. Used during an RFC Trace.
EXITCODE.
The RC code of the external program at termination or end.
EXEC_PROTOCOL.
The contents of STDOUT if you're a Unix devotee, or CON: if you're from the other
side of the pond.
This table contains 2 fields. A length field specifying the length of the line displayed by
the external program, and the line itself that was displayed…….
You can then make use of the EXEC_PROTOCOL table like so:
Code:
*
* There should be only one line of text returned.
*
Describe table t_exec_protocol lines w_lines.
Read Table t_exec_protocol index 1.
Condense t_exec_protocol-Message.
Split t_exec_protocol at ' ' into table t_splits.
*
* ls -l format:
*
* Size = 6
* Day = 7
* Month = 8
* Time = 9
* File name = 10.
*
Read Table t_splits index 10.
If t_splits-line = w_filename.
*
* Size
*
Read Table t_splits index 6.
Move t_splits-line to pc_size.
*
* Date
*
Read Table t_splits index 7.
Move t_splits-line to pc_date.
Read Table t_splits index 8.
Translate t_splits-line to upper case.
Search c_Unix_Months for t_splits-line.
If sy-subrc = 0.
Compute w_month = 1 + ( sy-fdpos / 3 ).
Move w_month to w_nummnth.
*
* Year - assume current year
*
Concatenate pc_date w_nummnth sy-datum+2(2)
into pc_date
separated by '/'.
*
* Time
*
Read Table t_splits index 9.
Move t_splits-line to pc_time.
EndIf.
Else.
Move c_file_not_found to pc_status.
EndIf.
EndIf.
OLE.
This is achieved by exposing the various methods used by the application via an
interface that can be accessed by external programs. This interface is based around
object technology and is available in the majority of Microsoft applications from Excel
to Word, Project, Outlook and so on.
By using ABAP objects and creating an object of the target application, you can then
call the various methods or access the various properties within the application:
Code:
*Eject
**********************************************************************
*
* Procedure: Get_Single_Border
*
* Purpose: Get single border for a range of cells
*
* Entry: Work Sheet
* Entry: Cell Range to Get border for
* Entry: Border to Get (Excel Values)
*
* Exit: Border type (Excel Values)
*
* Called By:
* '
*
* Calls:
*
* Modification History:
*
Form Get_Single_Border Using pu_sheet Type Ole2_Object
pu_range Type XlRangeAddress
pu_border Type XlBorder
Changing pc_linestyle Type XlLineStyle.
*
Data: Range type Ole2_Object,
Borders type Ole2_Object.
*
Call Method of pu_Sheet 'Range' = Range Exporting #1 = pu_range.
Call Method of Range 'Borders' = Borders
Exporting #1 = pu_border.
Get Property of Borders: 'Linestyle' = pc_Linestyle.
*
Free object Borders.
Free object Range.
EndForm.
Open Dataset
The OPEN DATASET command can also be used to run external programs
Code:
*
* Are we using a filter ?
*
If pu_filter = ''.
Open Dataset pu_dataset
for input
in text mode.
Else.
Open Dataset pu_dataset
for input
in text mode
filter pu_filter.
EndIf.
This code is part of a set of generic subroutines that I use for handling interfaces. There are two
seemingly similar Open Dataset statements. The second one uses the Filter clause.
The filter clause can be used to pass the file contents through an external program to perform
some manipulation on the data. The program forms part of a pipe with the SAP being the recipient
at the end.
To take a simple example, the following Open Dataset statement will open the named file, pass
the file through a program called Fileport, translate the file from EBCDIC to ASCII (including
packed fields) (see 0005 - ASCII Codes ) using the specified script and then passes the
translated data onto SAP.
Code:
Open Dataset p_inf for input
in binary mode
filter '/gldata/scripts/fdr.sh'.
By changing the parameter of the filter clause, external OS programs can be run.