How to Create Programs - Linking to EXE files

 


1. How do I run another program?

Use the Exec function - this allows you to specify the command line arguments, also it allows you to control whether your program waits for the other program to finish, or whether your program continues running immediately after calling Exec. For example:

  // TestExec.cls
  Ubercode 1 Class TestExec

  public function main()
  code
    call Exec("notepad.exe", "", EXEC_NOWAIT+SW_SHOWNORMAL)
  end function

  end class

The example just shown uses Exec to open the Windows notepad.exe program. The EXEC_NOWAIT option specifies the program continues running immediately after starting notepad, and the SW_SHOWNORMAL option specifies that notepad is shown in a normal sized window (ie not minimized and not maximized).

2. How do I pass a command line argument to another program?

Again the Exec function is able to do this. Refer to the example above, where the second string argument of Exec is an empty string. This parameter can be replaced by a command line argument which is passed to notepad on the command line. Notepad interprets this argument as the name of the file to open. Thus the following code:

  call Exec("notepad.exe", "myfile.txt", EXEC_NOWAIT+SW_SHOWNORMAL)

uses notepad to open the file myfile.txt (assuming this file exists). If you are running another Ubercode program, you can obtain the command line arguments as a string in parameter of function main(), or by using the Arguments constant.

3. How do I get the task list (list of running programs)?

Use the Applications function with a For each loop to build up the list of programs. You can then search the resulting list or display the list to the user. The following example displays the programs in a list:

  // Apps.cls
  Ubercode 1 Class Apps

  public function main()
  var
    app:string[*]
    applist:string[*]
  code
    for each app in Applications()
      applist <- applist + app + NL
    end for
    call Listbox("Apps", "", applist)
  end function

  end class

This works as follows: The for each loop gets each application name returned by the Applications function, and appends it to a string. The string is then shown to the user in a Listbox window. After the user clicks OK the program will end.

4. How do I run an MS-DOS program?

Use the Exec function to run a command shell (command.com or cmd.exe). The command shell can then run inbuilt MS-DOS commands such as Dir, Copy etc. For example:

  // Msdos.cls
  Ubercode 1 Class Msdos

  public function main()
  code
    call Exec("cmd.exe", "/c dir", EXEC_WAIT+SW_SHOWNORMAL)
  end function

  end class

The example just shown uses Exec to run the Windows command shell (cmd.exe under Windows NT / 2000 / XP and command.com under earlier versions of Windows). The command shell runs the Dir command and then quits, then the call to Exec returns and the program ends. Note the EXEC_WAIT option waits for cmd.exe to quit, which it does immediately after the directory listing has been shown.

5. How do I capture the output of an MS-DOS program?

Use Exec() to run a command shell (command.com or cmd.exe), make the command shell run the program that generates the output, redirect the output to a text file, then load the text file into the program as a string. In the next example, cmd.exe is the command shell, captureProg is the command having its output captured, the angle bracket redirects the output to the text file, and Loadfile reads the text back into a string:

  // MsdosOutput.cls
  Ubercode 1 Class MsdosOutput

  public function main()
  var
    captureProg : string[*]
    outputText : string[*]
  code
    captureProg <- "dir c:\windows\*.exe > file.txt"
    call Exec("cmd.exe", "/c """ + captureProg + """", EXEC_WAIT+SW_SHOWNORMAL)
    call Loadfile("file.txt", FILE_TEXT, outputText)
    call Listbox("Captured output", "", outputText)
  end function

  end class

The double quotes (specified as "" above, following the /c) enclose the part of the command with the captured output, and the four double quotes (specified as """" above) is a string consisting of one double quote. When the program runs, the captured output is loaded into the string variable outputText and is shown in a window. The result is the output of the dir command is captured and shown on screen.

Note this technique can also be used with batch files. Also note that Windows 95 and 98 use command.com instead of cmd.exe. These versions of Windows check for a command.pif file in the logged directory, to control whether command.com is executed in a minimized or normal state.

6. How do I run code in other languages (C, C++, Delphi etc)?

The easiest way of doing this is to compile the external code into a DLL. Declare public functions in the DLL for the code you want to run, being sure to use the STDCALL convention. You will then be able to call these functions up from Ubercode programs.