Consultor Eletrônico



Kbase 21728: How To Access the Computer Name Using DOS NET OS-COMMAND
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   30/01/2002
SUMMARY:

This Solution offers a second method of accessing the computer name from a Progress 4GL client session. One solution is to use the Windows API GetComputerNameA function call to obtain the current computer name; a process described in Progress Knowledge Base Solution 17114. Here, however, we use the DOS NET command to obtain the computer name and optionally, some other related information.

EXPLANATION:

The DOS NET command returns the computer name, the user name and a few other bits of information in the form of separate lines. These lines, if not blank, will each consist of a property, value pair. Any of these properties and/or values may be extracted via slight modifications to the Progress 4GL code below.

In this code the output of the NET command is sent to a temporary file where it will be read and processed one line at a time.

Since only the computer name is of interest in this case, the line beginning with "Computer name" is processed by truncating the string "Computer name" and trimming the resulting string to obtain the computer name as desired.

NOTE: that although a REPEAT loop is not necessary in this case, since the "computer name" entry is the first line of the output, the REPEAT loop is preserved for generalization purposes and also to make the code easier to modify if needed.

SOLUTION:

/* Sample code */
DEFINE VARIABLE cTempFilename AS CHARACTER NO-UNDO.
DEFINE VARIABLE cComputerName AS CHARACTER NO-UNDO.
DEFINE VARIABLE cPropertyName AS CHARACTER NO-UNDO.

OS-COMMAND SILENT "net config workstation > cNetConfig.txt".

ASSIGN
cTempFilename = "cNetConfig.txt"
cPropertyName = "Computer name".

INPUT FROM VALUE( cTempFilename ) NO-ECHO.
REPEAT:
IMPORT UNFORMATTED cComputerName.
IF cComputerName BEGINS cPropertyName THEN DO:
cComputerName = TRIM( SUBSTRING ( cComputerName , INDEX ( cComputerName, cPropertyName ) + LENGTH( cPropertyName ) ) ).
MESSAGE cComputerName
VIEW-AS ALERT-BOX INFO BUTTONS OK.
LEAVE.
END.
END.


References to Written Documentation:

Progress Language Reference
Progress Programming Handbook
MS DOS NET Command Syntax
Progress Knowledge Base Solution 17114, "How to Call WIN32 API Function: GetComputerName"