5

Is it possible to use Install[] to start a MathLink program with a custom PATH environment variable?

I am trying to use mEngine to connect Mathematica to MATLAB on Windows. It only works if mEngine.exe is launched when the PATH environment variable includes the path to the MATLAB libraries. Is it possible to modify the PATH for launching this program only, without needing to modify the system path? Or is there another way to launch mEngine.exe?

Szabolcs
  • 24,728
  • 9
  • 85
  • 174
  • this is on windows, I suppose? – acl Nov 30 '11 at 12:59
  • @acl yes, I'll update the question. – Szabolcs Nov 30 '11 at 13:01
  • while I have never used windows, I vaguely remember that DOS has batch files. so could you not start mEngine from a batch file that temporarily redefines the path, then reverts? – acl Nov 30 '11 at 13:15
  • 1
    @acl Yes, this works very well ... I don't know why I had the idea that we should pass `Install` the MathLink executable and not a script that starts it ... I wonder if I should delete this question now. – Szabolcs Nov 30 '11 at 18:27
  • @Szabolcs well, I'd leave it. I'm sure you're not the first or last to have this problem and simply not think of the answer. – acl Nov 30 '11 at 18:56
  • @Szabolcs -- There is a *Mathematica* function `Environment[name]` which allows you to read the kernel's shell variable `name`. But there is no *Mathematica* function for setting environment variables (such as `SetEnvironment[name,value]`). Is this what you are after? – Arnoud Buzing Nov 30 '11 at 19:30
  • @Szabolcs, I'd try acl's answer and see if it works. If it does, encourage him to post it as an answer. – rcollyer Nov 30 '11 at 21:35
  • 1
    @rcollyer you could also construct a batch file and use `setx` to set a global env PATH var. So, first run that batch, then install your program, and after that run another batch file that removes the added directories from %path% – Dr. belisarius Nov 30 '11 at 23:40
  • @acl Yes, as, rcollyer said, you should post it as an answer. – Szabolcs Dec 01 '11 at 07:59
  • @belisarius I didn't know about `setx`. It doesn't seem to be available on all versions of Windows (not on this XP at least). But `set` works well. – Szabolcs Dec 01 '11 at 08:04
  • @Szabolcs I wouldn't know what to put in my answer. Last time I wrote batch programs for non-unix systems was on DOS 3.3. Maybe you can answer yourself with the solution you used. – acl Dec 01 '11 at 10:27

1 Answers1

8

@acl's solution to wrap mEngine.exe in a batch file, and temporarily modify the PATH from there, works correctly:

I used this as the contents of mEngine.bat:

set PATH=c:\path\to\matlab\bin\win32;%PATH%
start mEngine.exe %*
  • *% ensures that all command line arguments are passed on to mEngine.exe
  • start is necessary to prevent the command window from staying open until mEngine.exe terminates

It can be started using Install["mEngine.bat"].

Since all the information that is needed for the kernel to communicate with mEngine.exe is passed by Install[] as command line arguments, all we need to do is launch mEngine.exe with these arguments. It is not necessary for Install[] to know the location of mEngine.exe, the important thing is that the process gets launched with the correct command line arguments, which is ensured by %*.

Szabolcs
  • 24,728
  • 9
  • 85
  • 174