//---------------------------------------------------------------------------
// Global External Functions
//
// Enter these in the Global External Functions window
//
// You must be using a version of PowerBuilder that can call 32-bit DLL's.
//

Function Long WinsockRCmd (Ref String Host, Long Port, Ref String LocalUser, &
                          Ref String RemoteUser, Ref String Cmd, &
                          Ref String ErrorMsg, Long ErrorMsgLen) &
                         Library "RCMD32.DLL"

Function Long RCmdRead (Long hRCmd, Ref String DataIn, Long DataLen) &
                     Library "RCMD32.DLL"

Function Long RCmdReadByte (Long hRCmd) Library "RCMD32.DLL"

Function Long RCmdClose (Long hRCmd) Library "RCMD32.DLL"

Function Long RCmdHandle (Long hRCmd) Library "RCMD32.DLL"

//----------------------------------------------------------------------------


// Example Powerbuild Script
// -------------------------

// This script simply executes the command "ls -x" on the host "unix" as the
// user "yourname" and displays the output in a message box.


integer li_handle     // Holds the WinsockRCmd handle
integer li_result     // Holds the result from other calls and length of data 

string s_host         // Holds the name of the remote host
string s_user         // Holds the name of the user on the remote host
string s_cmd          // Holds the command to execute
string s_errmsg       // Will hold any error message returned
string s_data         // Will hold data received from a RCmdRead call
string s_datastr      // Will hold the total data received


s_host = "unix"
s_user = "yourname"
s_cmd = "ls -x"
s_errmsg = space(128)   // Need to "reserve" space in the string
                        // since PB strings are dynamically allocated


// This connects to the remote host and begins executing the command
li_handle = WinsockRCmd(s_host, 514, s_user, s_user, s_cmd, s_errmsg, 128)

if li_handle < 0 then
  // We received an error message from RCMD.DLL (a Windows Sockets error)
  // or an error message from the remote host (such as "Command not found")
  MessageBox("Error from Remote Host", s_errmsg)
else

  li_result = 1
  s_datastr = ""

  // Loop until we receive all data.  Data is read in 64-byte blocks and
  // appended to s_datastr as it is received.
  do while li_result > 0
    s_data = Space(64)              /* Reserving 64 bytes in the string */
    li_result = RCmdRead(li_handle,s_data,64)
    if li_result > 0 then
      // Append the data to s_datastr.  The length of the string returned
      // is in li_result when li_result is positive.  Remember to only use
      // first N bytes in the string where N = li_result.  Left() does this.
      s_datastr = s_datastr + Left(s_data,li_result)
    end if

    // We really should check for negative values in li_result here and
    // report them as errors.  A return value of zero (0) means that all
    // data has been read.  A negative return value means that some error
    // has occurred.

  loop

  // Close the connection and free up resources.  This only needs to be done
  // if there was no error initially.
  li_result = RCmdClose(li_handle)

  MessageBox("Output from ls -x",s_datastr)

end if


//-----------------------------------------------------------------------------
// If the RCmdRead() function does not return the data from the host properly
// with your version of Powerbuilder (i.e. there are extraneous spaces returned)
// replace the above read loop with the following:
//
//
//  do while li_result > 0
//    li_result = RCmdReadByte(li_handle)
//    if li_result > 0 then
//      s_datastr = s_datastr + Char(li_result)
//    end if
//  loop
//
//-----------------------------------------------------------------------------

