Prev: How to recover from a EXCEPTION_STACK_OVERFLOW?
Next: Question Use of IOCTL_DISK_UPDATE_PROPERTIES Update dynamic volume Partition on VISTA
From: Vladimir Scherbina on 24 Nov 2006 05:55 If you use CreateProcess you have more control, unlike when using "run" functionality. You can pass the SW_SHOW flag to STARTUPINFO structure, and achieve the same effect. The Windows "Run" creates the process already with this flag. Following code should work: STARTUPINFO si = {0}; PROCESS_INFORMATION pi = {0}; si.cb = sizeof(si); TCHAR szCmd[MAX_PATH]; _tcscpy(szCmd, TEXT("rundll32.exe user32.dll,MessageBoxA")); si.wShowWindow = SW_SHOW; si.dwFlags = STARTF_USESHOWWINDOW; CreateProcess(NULL, szCmd, 0, 0, FALSE, 0, NULL, 0, &si, &pi); WaitForSingleObject(pi.hProcess, -1); // close handles of hThread, hProcess -- Vladimir (Windows SDK MVP) "dc2000" <dc2000(a)discussions.microsoft.com> wrote in message news:E70932D6-9F5D-440A-8018-344D83487BC3(a)microsoft.com... > Interesting. I've just tried the following code: > > STARTUPINFO si = {0}; > PROCESS_INFORMATION pi = {0}; > si.cb = sizeof(si); > CreateProcess(NULL, "rundll32 user32.dll MessageBox", 0, 0, FALSE, 0, > NULL, > NULL, &si, &pi); > > The result is TRUE but nothing happens. If one types the same in the Run > window, it shows a dialog box. What is the difference here? > > PS. I'm using that rundll32 line as an example, I don't need to emulate a > call to MessageBox > > > "Vladimir Scherbina" wrote: > >> "David Lowndes" <DavidL(a)example.invalid> wrote in message >> news:kt9cm2tv0rhgk94h0v82npscudf2l1lupu(a)4ax.com... >> >> [...] >> >> > ShellExecute(Ex) is essentially the >> > API that is used. >> >> Or CreateProcess specifiying the command line arguments. >> >> -- >> Vladimir (Windows SDK MVP) >> >> >>
From: G�nter Prossliner on 24 Nov 2006 10:56 Hello Vladimir! Hello David! >> ShellExecute(Ex) is essentially the >> API that is used. > > Or CreateProcess specifiying the command line arguments. If you want to do exactly what Start->Run is using, you should prefere "ShellExecute" instead of "CreateProcess". "CreateProcess" can only be used to start executeable images, while "ShellExecute" can start any file which has a "OPEN" verb (you may choose other verbs if you have to - PRINT or EDIT for example) registered. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp GP
From: dc2000 on 24 Nov 2006 14:07 Wow, thanks Vladimir! Now it works. I guess the trick was to use STARTF_USESHOWWINDOW and put szCms into a non-constant line (which I missed in documentation.) My last question is -- if I don't want to wait until this new process finishes, can I close both handles with CloseHandle() right after CreateProcess returns them? "Vladimir Scherbina" wrote: > If you use CreateProcess you have more control, unlike when using "run" > functionality. You can pass the SW_SHOW flag to STARTUPINFO structure, and > achieve the same effect. The Windows "Run" creates the process already with > this flag. Following code should work: > > STARTUPINFO si = {0}; > PROCESS_INFORMATION pi = {0}; > > si.cb = sizeof(si); > > TCHAR szCmd[MAX_PATH]; > > _tcscpy(szCmd, TEXT("rundll32.exe user32.dll,MessageBoxA")); > > si.wShowWindow = SW_SHOW; > si.dwFlags = STARTF_USESHOWWINDOW; > > CreateProcess(NULL, szCmd, 0, 0, FALSE, 0, NULL, 0, &si, &pi); > > WaitForSingleObject(pi.hProcess, -1); > > // close handles of hThread, hProcess > > -- > Vladimir (Windows SDK MVP) > > > "dc2000" <dc2000(a)discussions.microsoft.com> wrote in message > news:E70932D6-9F5D-440A-8018-344D83487BC3(a)microsoft.com... > > Interesting. I've just tried the following code: > > > > STARTUPINFO si = {0}; > > PROCESS_INFORMATION pi = {0}; > > si.cb = sizeof(si); > > CreateProcess(NULL, "rundll32 user32.dll MessageBox", 0, 0, FALSE, 0, > > NULL, > > NULL, &si, &pi); > > > > The result is TRUE but nothing happens. If one types the same in the Run > > window, it shows a dialog box. What is the difference here? > > > > PS. I'm using that rundll32 line as an example, I don't need to emulate a > > call to MessageBox > > > > > > "Vladimir Scherbina" wrote: > > > >> "David Lowndes" <DavidL(a)example.invalid> wrote in message > >> news:kt9cm2tv0rhgk94h0v82npscudf2l1lupu(a)4ax.com... > >> > >> [...] > >> > >> > ShellExecute(Ex) is essentially the > >> > API that is used. > >> > >> Or CreateProcess specifiying the command line arguments. > >> > >> -- > >> Vladimir (Windows SDK MVP) > >> > >> > >> > >
From: Vladimir Scherbina on 24 Nov 2006 14:10 "G�nter Prossliner" <g.prossliner/gmx/at> wrote in message news:%23B7WVE%23DHHA.4620(a)TK2MSFTNGP04.phx.gbl... > If you want to do exactly what Start->Run is using, you should prefere > "ShellExecute" instead of "CreateProcess". > > "CreateProcess" can only be used to start executeable images, while > "ShellExecute" can start any file which has a "OPEN" verb (you may choose > other verbs if you have to - PRINT or EDIT for example) registered. > http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/shell/reference/functions/shellexecute.asp Yes, we remember that. In cases when there is a need to (simply) run a process I would choose the CreateProcess method, because its much faster then ShellExecute variant. -- Vladimir (Windows SDK MVP)
From: Vladimir Scherbina on 24 Nov 2006 14:12
"dc2000" <dc2000(a)discussions.microsoft.com> wrote in message news:2B2A3E8D-BB5D-46CC-B128-9CB1AB100EF5(a)microsoft.com... > Wow, thanks Vladimir! Now it works. I guess the trick was to use > STARTF_USESHOWWINDOW and put szCms into a non-constant line (which I > missed > in documentation.) True. The unicode version of CreateProcess may change the content of lpCmdLine param. > My last question is -- if I don't want to wait until this new process > finishes, can I close both handles with CloseHandle() right after > CreateProcess returns them? Yes, you can close them w/o problems - the process will contiue running. -- Vladimir (Windows SDK MVP) |