Home | Tools

 

A simple interface between a Rev stack and an external application

 

The script below is an example of using Revolution's shell() function to cause an external application to be run under Windows OS.  This method is used in the carbon bed and photo filter modules of our Internet application, ERC Web Courses.

 

The method is a simple way to provide a graphical interface to a "number crunching" program.  A user enters input values into a Revolution stack and clicks a "run" button.  The stack then writes the input values to a text file on disk, causes the external application to run, and waits for it to finish.  The external application executes and writes its output to a text file on disk.  The Revolution stack then reads in the output file and displays the results.  The external application is written and complied so that it does not display a window during execution., although you may want to display one during development.

 

The disk access only takes a fraction of a second, so this method will work in many cases.  In other cases, such as when the user needs to make changes in input values during a continuously running simulation, the stack and the external must be able to communicate data via RAM.  In that case, the external can be made into a .DLL file for Windows or an XCMD for Macintosh.

 

The external executable file or application is named "carbon_1.exe" below.

 

on runWin32

 

  global gModuleSupportPath # disk path to folder containing external file

 

  put gModuleSupportPath into lWinPath

  replace "/" with "\" in lWinPath

 

  # check for existance of the executable

  # name must match name below in two places

  set the directory to gModuleSupportPath

  if there is a file "carbon_1.exe" then

    # exe file is there

  else

    answer "Can't run! The folder or file is missing or was renamed!"

    exit to metacard # current name of Revolution’s engine

  end if

 

  # different Windows OS have different shell command programs as default

  # on Win 98 command.com is default

  # on Win XP cmd.exe is default

  # on Win XP, command.com options and behavior different than same on Win 98

 

  # the shellCommand may return full path if previously set to system global $COMSPEC

 

  if the shellCommand contains "command.com" then

    runCommandDotCom

  else if the shellCommand contains "cmd.exe" then

    runCmdDotExe

  else

    answer "unknown shellCommand"

  end if

 

end runWin32

 

on runCmdDotExe

  # running on Win XP or similar

 

  # can't just use "start" and full path to executable because of possible spaces in path

  # so have to cd to directory first (doesn't care about spaces in path), then "start"

  # cannot use two separate get shell lines

  # see "&" below to put two or more command lines in one get shell()

 

  # the command continuation character in cmd.exe is & (don't confuse with MC &)

 

  put "get shell(" & quote & "cd" && lWinPath & "& start /min /high /wait carbon_1.exe" & quote & ")" into todo

 

  # answer todo # use in development for checking string

 

  do todo

 

  # nothing comes back to MC "it" or the result as it does with command.com on Win 98

 

end runCmdDotExe

 

on runCommandDotCom

 

  # running on Win 98 or similar

 

  # because of get MC shell() syntax requiring quotes around entire input string

  # and because of possible spaces in path and because command.com's cd

  # is sensitive to spaces in path

  # need to write a batch file and do "cd" with quotes around path followed by

  # command in batch file to execute exe file

  # and finally have MC shell() execute the batch file

  # the command continuation character in command.com is | but doesn't

  # help because need quotes around entire "get shell" and around path for "cd"

 

  put "C:\temp_mc.bat" into tBatPath

  put "file:" & tBatPath into tBatUrl

 

  # need literal quote characters around path with spaces for command.com

  put "cd" && quote & lWinPath & quote into temp

  put cr & "carbon_1.exe" & cr after temp

 

  try

    put temp into url tBatUrl

  catch errornum

    answer "Error: can't write command file to C:\ and so can't run!"

    exit to metacard # current name of Revolution’s engine

  end try

 

  # no priority options or /b as on Win XP cmd.exe, and /m here not /min

  # want /wait option so focus doesn't return to MC until after execution is done

 

  put "get shell(" & quote & "start /m /wait" && tBatPath & quote & ")" into todo

 

  # answer todo # use in development for checking string

 

  do todo

 

  # command.com, at least on Win98, returns entire console contents to MC "it"

  # and so could check for Fortran program "STOP" for good run but

  # have to check for existance of full output files for cmd.exe anyway

 

  # clean up

  if there is a file tBatPath then delete file tBatPath

 

end runCommandDotCom

 

Home | Tools