Install MathLink program with arbitrary PATH environment
Asked Answered
W

1

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?

Wheeled answered 30/11, 2011 at 12:53 Comment(11)
this is on windows, I suppose?Handcart
@Handcart yes, I'll update the question.Wheeled
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?Handcart
@Handcart 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.Wheeled
@Wheeled 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.Handcart
@Wheeled -- 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?Kongo
@Szabolcs, I'd try acl's answer and see if it works. If it does, encourage him to post it as an answer.Chutney
@Chutney 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%Mariomariology
@Handcart Yes, as, rcollyer said, you should post it as an answer.Wheeled
@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.Wheeled
@Wheeled 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.Handcart
W
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 %*.

Wheeled answered 1/12, 2011 at 10:35 Comment(3)
Good to know, never occurred to me. +1.Fantail
@Handcart I do realize for UNIX folks it's part of the fundamentals! Plus, probably there it wouldn't be necessary to give the matlab path at allWheeled
@Wheeled I really was just jokingly referring to "acl's solution"!Handcart

© 2022 - 2024 — McMap. All rights reserved.