Help with 'CreateProcess & wait' in Borland C++

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
Hi all,

I am getting stuck with a piece of code to execute a process and wait for it to close, so found this example:

//-------------Code Start-------------
STARTUPINFO StartInfo; // name structure
PROCESS_INFORMATION ProcInfo; // name structure
memset(&ProcInfo, 0, sizeof(ProcInfo)); // Set up memory block
memset(&StartInfo, 0 , sizeof(StartInfo)); // Set up memory block
StartInfo.cb = sizeof(StartInfo); // Set structure size
int res = CreateProcess(NULL, "c:\\notepad.exe", NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo); // starts MyApp
if (res)
{
WaitForSingleObject(ProcInfo.hThread, INFINITE); // wait forever for process to finish
SetFocus(); // Bring back focus
}

MessageDlg("Completed)+")",mtInformation,TMsgDlgButtons() << mbOK, 0);

exit (0);


//-------------Code End-------------


It does open notepad and waits but when I close notepad I recieve this error message:

"Canont focus an invisable or disabled window"

I am a lost and would appricate some help...

FYI I am using Borland C++ Builder 2010 (not my choice!)

Thanks,

James
 

jameswhite1979

Senior member
Apr 15, 2005
367
0
0
STARTUPINFO StartInfo; // name structure
PROCESS_INFORMATION ProcInfo; // name structure
memset(&ProcInfo, 0, sizeof(ProcInfo)); // Set up memory block
memset(&StartInfo, 0 , sizeof(StartInfo)); // Set up memory block
StartInfo.cb = sizeof(StartInfo); // Set structure size
int res = CreateProcess(NULL, "c:\\notepad.exe", NULL, NULL, NULL, NULL, NULL, NULL, &StartInfo, &ProcInfo); // starts MyApp
if (res)

WaitForSingleObject(ProcInfo.hThread, INFINITE);


/\/\/\Sorted with the above code /\/\/\
 

Cogman

Lifer
Sep 19, 2000
10,284
138
106
You might want to use code blocks for more complex code, ie [ code ] stuff(); [ /code ]

as for the problem. According the the sample in the MSDN, you want to wait for the process handle and not the thread handle (I don't really think that is your problem however.)

It looks like your real problem is in the SetFocus(); function. The windows SetFocus function needs a HWND to set the focus, since you didn't receive a compiler error, I'm guessing this is a borderland specific function that you just called. Apparently it is hooked to a window that doesn't exist, or is invisible.