Thursday, March 10, 2016

Things must be considered when launching process(exe) from other process

You might have used CreateProcess() API when ever you want to launch child process from main process. But how about the settings of parent process reflecting to child process?

If you don't want your current directory not to be inherited to child then make sure you pass appropriate path to it, explicitly. Otherwise, your child App may fail to perform expected functionality.

If you need your child process to get the similar current directory of parent then just pass lpCurrentDirectory = NULL in this API


CreateProcess API, Windows


Here is the CreateProcess API's parameters:

// Start the child process.
if (!CreateProcess(
strAppFullPath,  // No module name (use command line). Must send param
NULL, // Command line
NULL,               // Process handle not inheritable
NULL,               // Thread handle not inheritable
TRUE,               // Set handle inheritance to FALSE
0,                    // No creation flags
NULL,               // Use parent's environment block
                strAppDir,         // Pass child process specific current directory
&si,         // Pointer to STARTUPINFO structure. Must send parameter
&pi) // Pointer to PROCESS_INFORMATION structure - Must send param
)
{

Current directory is not always the Application directory!

When you happen to get the application directory then it is good practice to use appropriate call for that: Say, GetApplicationFilePath();

Instead if you try GetCurrentDirectory() then there would be chance that your current directory not equals to ApplicationDirectory. This is because form the App, any functionality could change the CurrentDirectory().

No comments: