external program
-
How can I start an external program - probably an .exe file - in a C++ program. And which would be the best way to be sure the external program has finished?
Create to process using
CreateProcess
orShellExecute
(there are other options). UseWaitForSingleObject
on the processHANDLE
to wait till process has exited. Steve -
How can I start an external program - probably an .exe file - in a C++ program. And which would be the best way to be sure the external program has finished?
Hi MrChefman, maybe it is some helpful to you if you run an application from your program use PROCESS_INFORMATION l_ProcessInfo; STARTUPINFO l_StartupInfo; ZeroMemory(&l_StartupInfo, sizeof(STARTUPINFO)); l_StartupInfo.cb = sizeof(STARTUPINFO); l_StartupInfo.dwFlags = STARTF_USESHOWWINDOW; l_StartupInfo.wShowWindow = SW_SHOWMAXIMIZED; CreateProcess(NULL, "Notepad.exe", NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &l_StartupInfo, &l_ProcessInfo); }
-
Hi MrChefman, maybe it is some helpful to you if you run an application from your program use PROCESS_INFORMATION l_ProcessInfo; STARTUPINFO l_StartupInfo; ZeroMemory(&l_StartupInfo, sizeof(STARTUPINFO)); l_StartupInfo.cb = sizeof(STARTUPINFO); l_StartupInfo.dwFlags = STARTF_USESHOWWINDOW; l_StartupInfo.wShowWindow = SW_SHOWMAXIMIZED; CreateProcess(NULL, "Notepad.exe", NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &l_StartupInfo, &l_ProcessInfo); }
To complete this example: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ PROCESS_INFORMATION l_ProcessInfo; STARTUPINFO l_StartupInfo; ZeroMemory(&l_StartupInfo, sizeof(STARTUPINFO)); l_StartupInfo.cb = sizeof(STARTUPINFO); l_StartupInfo.dwFlags = STARTF_USESHOWWINDOW; l_StartupInfo.wShowWindow = SW_SHOWMAXIMIZED; if ( CreateProcess(NULL, "Notepad.exe", NULL, NULL, FALSE, 0, NULL, NULL, &l_StartupInfo, &l_ProcessInfo)) { CloseHandle(l_ProcessInfo.hThread); WaitForSingleObject(l_ProcessInfo.hProcess, INFINITE); // Wait for it to finish. CloseHandle(l_ProcessInfo.hProcess); } \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Forgetting to close the handles is a common source of resource leaks. Steve
-
To complete this example: \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ PROCESS_INFORMATION l_ProcessInfo; STARTUPINFO l_StartupInfo; ZeroMemory(&l_StartupInfo, sizeof(STARTUPINFO)); l_StartupInfo.cb = sizeof(STARTUPINFO); l_StartupInfo.dwFlags = STARTF_USESHOWWINDOW; l_StartupInfo.wShowWindow = SW_SHOWMAXIMIZED; if ( CreateProcess(NULL, "Notepad.exe", NULL, NULL, FALSE, 0, NULL, NULL, &l_StartupInfo, &l_ProcessInfo)) { CloseHandle(l_ProcessInfo.hThread); WaitForSingleObject(l_ProcessInfo.hProcess, INFINITE); // Wait for it to finish. CloseHandle(l_ProcessInfo.hProcess); } \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ Forgetting to close the handles is a common source of resource leaks. Steve
Thanks
-
Thanks
Thanks a lot. But I still have a problem in Building the .exe file. Linking works but when trying to build the .exe there are errors: unresolved external symbol _WinMain@16 I hope, my translation from the German version into English is right ;-) I included both header-files windows.h and winbase.h into my win32-application-project. Where is my mistake?
-
Thanks a lot. But I still have a problem in Building the .exe file. Linking works but when trying to build the .exe there are errors: unresolved external symbol _WinMain@16 I hope, my translation from the German version into English is right ;-) I included both header-files windows.h and winbase.h into my win32-application-project. Where is my mistake?
You must insert library Kernel32.lib to your project ----------------- conglt
-
Thanks a lot. But I still have a problem in Building the .exe file. Linking works but when trying to build the .exe there are errors: unresolved external symbol _WinMain@16 I hope, my translation from the German version into English is right ;-) I included both header-files windows.h and winbase.h into my win32-application-project. Where is my mistake?
The linker is looking for the entry point for a console application, but the application is being compiled and linked as a windows application (i.e. not a console application). This error has been observed after changing the project settings from using MBCS to UNICODE characters. Under the project settings, under the ‘Link’ tab, category ‘output’, set the ‘Entry-point symbol’ to ‘wWinMainCRTStartup’.
-
How can I start an external program - probably an .exe file - in a C++ program. And which would be the best way to be sure the external program has finished?
See here.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
See here.
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb
-
Thanks a lot. It works! But there's still a little problem: When I use the external thread, the command line interface appears for a short time. how can I suppress the command line?
MrChefman wrote:
...the command line interface appears for a short time.
You mean the console window? Have you tried using
ShellExecute(..., SW_HIDE)
, or settingSTARTUPINFO.wShowWindow
toSW_HIDE
if usingCreateProcess()
?
"Let us be thankful for the fools. But for them the rest of us could not succeed." - Mark Twain
"There is no death, only a change of worlds." - Native American Proverb