views:

115

answers:

3

Why does the root directory of a process, started by a windows process manager, change to the directory of where the pm is located?

Using msdn process manager code to create a pm service to run a few exes.
The exes save log files in the root relative to their location.
When started by the process manager, they are saving to the process manager directory?

Any advice is appreciated, thanks.

+1  A: 

Sounds like a working directory issue. By default the working directory for an application is not where the application is launched, but rather the directory that it was launched from. Check if there is a way to set the working directory that the process should be launched in. I know this is possible in .Net but if you're not using .Net then I'm not sure how you'd do it.

Db
What is "the directory where the application is launched" ? Did you mean "the directory where the application is located" ?
MSalters
+1  A: 

Tosses 'should be on superuser!!!' shield

The PM is a process itself started from wherever the PM shortcut points to, so the WD will be the location of the executable. If you start another process from that, it will fork (errr, windows equivelent) another process with the same WD. If you think about it, what else would you expect it to do?

eskerber
expect it to be smarter........is that so much to ask..haha
Tommy
So if you were to have this PM process fork another process, you would want it to change the WD to the location of the executable code first? That would be extra 'fluff' code for the PM :).
eskerber
@Tommy. Thats not smarter. When I start a program I expect the working directory to be the directory where I am, so the program can work on the files I am working on. I dont want to copy my files to the application folder just so it can read them. Thus it is smarter to leave changing the working directory upto the application rather than the caller/starter of the application. Your app just needs to be smarter :-)
Martin York
Why would you have to copy your files to the application folder? The working directory should be the directory where the application is, whether or not I start the app or the pm does from it's directory...extra fluff for my app that is not always started by the pm.
Tommy
"The working directory should be the directory where the application is"Very incorrect assumption. Write a simply python script to print "Hello" to a file. FILE = open(test.txt", "w")FILE.writelines("Hello")FILE.close()..and run it from various directories. You will ALWAYS see the test.txt written where the process was called from, not the directory of the file.python ./test.py -- (Shows up in ./)cd /python <directory>/test.py -- (Shows up in root (/))
eskerber
+1  A: 

To be honest, I don't know what the MSDN process manager is, but I know that whenever I create processes, I call CreateProcess, this function has an ability to decide which directory the process is created in. For the example I'll create a CMD process which is in the C:\ directory.

STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));

CreateProcess(_T("cmd.exe"), // Note, because this is windows, it will automatically find cmd.exe because it's in one of its automatic search directories, so no need to put c:/windodws/system32/cmd.exe
NULL,
NULL,
NULL,
FALSE,
0,
NULL,
_T("c:\\"),
&si,
&pi);

Handles to the process and main thread are kept in the PROCESS_INFORMATION object, and must be closed using CloseHandle after you finished with them.

For more info on all these parameters (even though for the most part you will be using CreateProcess almost exactly as I just did) here's the MSDN page:

http://msdn.microsoft.com/en-us/library/ms682425%28VS.85%29.aspx

Ori Osherov