TerminateProcess()
-
Alright, I'm trying to incorporate TerminateProcess() into my progrms. I have two programs running at the same time, the first program opens the second via system("") and then continues to run a series of GetAsyncKeyState()checks that blocks input when certain keys are pressed. The second program is an annoying little game that runs through a random array of MessageBoxes. The issue I've hit is that when the second program finishes, the first program is still going--which means the BlockInput code is still ready to set off. What I'm trying to do is incorporate TerminateProcess() at the very end of the second program so that right before the second program finishes it closes the first, does anyone know how to do this?
-
Alright, I'm trying to incorporate TerminateProcess() into my progrms. I have two programs running at the same time, the first program opens the second via system("") and then continues to run a series of GetAsyncKeyState()checks that blocks input when certain keys are pressed. The second program is an annoying little game that runs through a random array of MessageBoxes. The issue I've hit is that when the second program finishes, the first program is still going--which means the BlockInput code is still ready to set off. What I'm trying to do is incorporate TerminateProcess() at the very end of the second program so that right before the second program finishes it closes the first, does anyone know how to do this?
Why not have the first program start the second using
CreateProcess()
and then wait on the process handle? An alternative is to use a globally named event and have the first application wait on it (which is pretty much what my first suggestion is doing.)Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
Why not have the first program start the second using
CreateProcess()
and then wait on the process handle? An alternative is to use a globally named event and have the first application wait on it (which is pretty much what my first suggestion is doing.)Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
Because I'm attending a High School that offers no programming class at all and thus I have no prior instructed knowledge and have no idea how to do that. I've attempted via examples from searching around but I couldn't get them to work because I just don't have enough knowledge at this point.
-
Because I'm attending a High School that offers no programming class at all and thus I have no prior instructed knowledge and have no idea how to do that. I've attempted via examples from searching around but I couldn't get them to work because I just don't have enough knowledge at this point.
DWORD ExecuteProcess(LPCTSTR pCommandLine, LPCTSTR pDirectory = NULL, DWORD millisecondsTimeout = INFINITE)
{
// _alloca allocates the memory on the stack, so I don't need to worry about cleaning it up
// You could use new and then delete the buffer later
TCHAR* pCmdLine = (TCHAR*) _alloca((lstrlen(pCommandLine) + 1) * sizeof(TCHAR));
lstrcpy(pCmdLine, pCommandLine);STARTUPINFO startupInfo; memset(&startupInfo, 0, sizeof(startupInfo)); startupInfo.cb = sizeof(startupInfo); PROCESS\_INFORMATION processInfo; memset(&processInfo, 0, sizeof(processInfo)); DWORD rval; if (CreateProcess(NULL, pCmdLine, NULL, NULL, FALSE, CREATE\_DEFAULT\_ERROR\_MODE, NULL, pDirectory, &startupInfo, &processInfo)) { rval = WaitForSingleObject(processInfo.hProcess, millisecondsTimeout); } else { rval = GetLastError(); } CloseHandle(processInfo.hProcess); CloseHandle(processInfo.hThread); return rval;
}
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
DWORD ExecuteProcess(LPCTSTR pCommandLine, LPCTSTR pDirectory = NULL, DWORD millisecondsTimeout = INFINITE)
{
// _alloca allocates the memory on the stack, so I don't need to worry about cleaning it up
// You could use new and then delete the buffer later
TCHAR* pCmdLine = (TCHAR*) _alloca((lstrlen(pCommandLine) + 1) * sizeof(TCHAR));
lstrcpy(pCmdLine, pCommandLine);STARTUPINFO startupInfo; memset(&startupInfo, 0, sizeof(startupInfo)); startupInfo.cb = sizeof(startupInfo); PROCESS\_INFORMATION processInfo; memset(&processInfo, 0, sizeof(processInfo)); DWORD rval; if (CreateProcess(NULL, pCmdLine, NULL, NULL, FALSE, CREATE\_DEFAULT\_ERROR\_MODE, NULL, pDirectory, &startupInfo, &processInfo)) { rval = WaitForSingleObject(processInfo.hProcess, millisecondsTimeout); } else { rval = GetLastError(); } CloseHandle(processInfo.hProcess); CloseHandle(processInfo.hThread); return rval;
}
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
okay, thanks for the tidbit of code but I still dont know where to insert it or how to tell it what to open
-
Alright, I'm trying to incorporate TerminateProcess() into my progrms. I have two programs running at the same time, the first program opens the second via system("") and then continues to run a series of GetAsyncKeyState()checks that blocks input when certain keys are pressed. The second program is an annoying little game that runs through a random array of MessageBoxes. The issue I've hit is that when the second program finishes, the first program is still going--which means the BlockInput code is still ready to set off. What I'm trying to do is incorporate TerminateProcess() at the very end of the second program so that right before the second program finishes it closes the first, does anyone know how to do this?
I never did get an answer to my initial question
-
I never did get an answer to my initial question
You can't do what you want.
system
doesn't give you back the identity of your process, so you have no way to (later) control it. You have to use some other mechanism to create the process, like (as Joe suggested)CreateProcess
, which does give you the process identity (through the PID and a process handle in the case ofCreateProcess
).Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p
-
okay, thanks for the tidbit of code but I still dont know where to insert it or how to tell it what to open
You call this function in the first program instead of the
system
function. It runs the second program (given in one of the parameters), waits for the second program to finish, then the function returns, so you can carry on doing whatever the first program should do. Iain.In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!
-
I never did get an answer to my initial question
gamefreak2291 wrote:
I never did get an answer to my initial question
Your original question was posted when Europe had just gone to bed, India was debating getting to eat its breakfast Bhaji, and the Americas were having their after work beer. Or in your case, doing their homework. 1/ You don't pay us - we'll answer if / when we feel like. 2/ I'll give you credit for not putting "URGENTZ" in your subject line! Anyway, Joe has given you a very good answer - if you need details, functions like CreateProcess have very long documentation to read. I wish you success with your course, Iain.
In the process of moving to Sweden for love (awwww). If you're in Scandinavia and want an MVP on the payroll (or happy with a remote worker), give me a job!
-
okay, thanks for the tidbit of code but I still dont know where to insert it or how to tell it what to open
You call ExecuteProcess with the same command line you would use at a command prompt. The directory is what the current directory should be set to. For example,
ExecuteProcess(_T("C:\\Windows\\System32\\Notepad.exe win.ini"), _T("C:\\Windows"));
This is very simple code, but it illustrates the basic principles. Go to MSDN and read up onCreateProcess()
andWaitForSingleObject()
. Read up on multi-threading and synchronization. Experiment. Step through the code. Until you fully understand this code, you will not be qualified to write any code.Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke