Skip to main content
gamedev.net
🔒 Locked

CreateProcess question

Started by darkchrono4 Feb 23, 2003 at 1:39 AM 7 replies 650+ views
Original Post
darkchrono4
darkchrono4
Trying to use CreateProcess to start an app in another folder.
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.cb = sizeof(startInfo);
startInfo.dwFlags = STARTF_USESHOWWINDOW;

CurrentDirectory = calloc(255, sizeof(TCHAR));
GetCurrentDirectory(size, CurrentDirectory);
			
ProcessStarted = CreateProcess(NULL,
                               filename,
			       NULL,
			       NULL,
			       FALSE,
			       NORMAL_PRIORITY_CLASS,
			       NULL,
			       CurrentDirectory,
			       &startInfo,
			       &procInfo);
 
If I run this nothing happens. Filename is passed to the function as a pointer with the full pathname to the app. What am I doing wrong here?
niyaw
niyaw
what does GetLastError say?
darkchrono4
darkchrono4
I guess there is actually no problem. Just the game wants the executable started from inside the game folder. I changed it to ShellExecute() but I really need my app to wait on the game so it can modify a file when the user is done with the game.
johnnie2
johnnie2
If you want to wait on the newly-created process, go back to CreateProcess().

quote:
Original post by darkchrono4

ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.cb = sizeof(startInfo);
startInfo.dwFlags = STARTF_USESHOWWINDOW;

CurrentDirectory = calloc(255, sizeof(TCHAR));
GetCurrentDirectory(size, CurrentDirectory);

ProcessStarted = CreateProcess(NULL,
filename,
NULL,
NULL,
FALSE,
NORMAL_PRIORITY_CLASS,
NULL,
CurrentDirectory,
&startInfo,
&procInfo);


If I run this nothing happens. Filename is passed to the function as a pointer with the full pathname to the app. What am I doing wrong here?


It looks like the STARTUPINFO struct is being initialized only halfway. At the very least, all lp members must be set to NULL and cbReserved2 set explicitly to zero (using ZeroMemory() or memset() won''t work in this case):


STARTUPINFO startupInfo;
memset(&startupInfo, sizeof(STARTUPINFO), 0);

startupInfo.cb = sizeof(STARTUPINFO);
startupInfo.lpReserved = NULL;
startupInfo.lpDesktop = NULL;
startupInfo.lpTitle = NULL;
startupInfo.cbReserved2 = 0;
startupInfo.lpReserved2 = NULL;


Also note that passing the current directory as the lpCurrentDirectory argument of CreateProcess() is the same as passing NULL, since the new process will have the same default directory as the calling process in both cases.

Now that the new process has been launched, use the handle stored in the PROCESS_INFORMATION struct along with WaitForSingleObject():


DWORD waitStatus;

// wait indefinitely until other process terminates
waitStatus = WaitForSingleObject(processInfo.hProcess, INFINITE);

if (waitStatus == WAIT_OBJECT_0) {
MessageBox(NULL, "Other process has been terminated.", "Indication", MB_OK);
}


RapscallionGL - arriving soon.
____________"Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C
darkchrono4
darkchrono4
Alright I went back and initialized the rest of startupinfo. I changed lpApplicationName to "Game.exe", changed lpCommandLine to "C:\\AAA", and changed lpCurrentDirectory to "C:\\AAA" because my program and game.exe aren''t in the same folder. With just about anything I do to it GetLastError is incorrect parameter.

Now if I use the code I posted first and run my program in the game directory it starts it fine. I''m not setting something right and I just don''t know what it is.
LessBread
LessBread
quote:
Original post by johnnie2
It looks like the STARTUPINFO struct is being initialized only halfway. At the very least, all lp members must be set to NULL and cbReserved2 set explicitly to zero (using ZeroMemory() or memset() won''t work in this case):


I agree with your assessment of STARTUPINFO not being initialized correctly, but I disagree with your statement regarding ZeroMemory/memset. Setting those pointers to NULL is the same as setting them to zero - a value they already have thanks to ZeroMemory. (This works in C too - where NULL is not defined as 0).

The structure isn''t initialized properly because darkchrono4 sets the flags to "uses show window"

startInfo.dwFlags = STARTF_USESHOWWINDOW;

but doesn''t set the show window structure member

startInfo.wShowWindow = SW_SHOW; (or desired flag)

So this should work just fine:

STARTUPINFO startInfo;
ZeroMemory(&startInfo, sizeof(startInfo));
startInfo.cb = sizeof(startInfo);
startInfo.dwFlags = STARTF_USESHOWWINDOW;
startInfo.wShowWindow = SW_SHOW;


"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
darkchrono4
darkchrono4
Its not that the code doesn''t work at all. Its just if my app is in another folder then it won''t start the game, I get an ''parameter incorrect'' error. If I copy my app to the folder the game is in then it starts it fine. I must not be setting the path to the game right or something but I''m not sure what I''m doing wrong.
LessBread
LessBread
Try setting the lpApplicationName parameter to the full path for the file you want to launch.
"I thought what I'd do was, I'd pretend I was one of those deaf-mutes." - the Laughing Man
darkchrono4
darkchrono4
Simple typo, I was using startInfo, which I hadn''t deleted, instead of the properly initized startupInfo.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.