Parent program's working directory is not searched for DLLs when child program is started with CreateProcess

StackOverflow https://stackoverflow.com/questions/16795125

سؤال

I have a very weird problem. I am working on a C program under Windows that writes data to the temp directory. It uses GetTempFileName to generate a unique name. The data is actually an exe that I start with CreateProcess after the data is written to the temp file. For lpCurrentDirectory param I use NULL so to use the current working directory of the parent program becomes a working directory of the child one. There are all the needed DLLs.

On most computers (XP, 2003, 7, 8) everything works as expected. But on one particular Windows 7 I get exception when the child program starts. Very strangely but if I move all the needed DLLs to the temp directory, everything works as expected.

Hmm, so why the parent program's working directory is not searched for the DLLs?

Many thanks.

هل كانت مفيدة؟

المحلول

Your original question asked why the child process does not inherit the working directory of the parent. Well, if you pass NULL to CreateProcess then the new process will inherit the working directory of the parent process at the time that the call to CreateProcess is made. The documentation makes this quite clear and there are no surprises here. CreateProcess behaves precisely as advertised.

The edited question concentrates on DLL search. It's not easy to answer this new take on the question from this information. The best thing to do is to run the program under the profile mode of Dependency Walker. This should reveal what is wrong. You may well find that the problem is not at all what you expect it to be.

And for future reference, I recommend that you include full details of any errors when you ask a question. You stated that an exception was raised but really you need to supply the full details of any exceptions.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top