关于调用exe的问题
2010-07-23 17:34
316 查看
Introduction
Arrrgh! So you need to run your console app via a primary application, well ye've come to the right place. This article is intended for those who need to run one of those pesky little console applications without the console window popping up over your main app or worse popping up when calling it from a service.So now matey lets dive into it and see how to avoid the console windows from sinking our application. Prepare the app for silent running.
Walking the Plank
Ok! That's enough of the pirate stuff. On with the show..You have probably searched the web and have seen many developers show examples of how to spawn applications using the
createProcessfunction. Well I shall do the same thing but with a twist. I will do it in such a way that the application that you would like to spawn does not show up when run.
There are several caveats to this process and many gotcha's. The main one is that if this program you wish to run is interactive, that is requires input from the user outside of what can be passed in through the command line, then blindly using this code will cause your program to hang. To recap! How can you input information into a program if you can't see the window. Ok! Think you get the point.
There are ways of doing this but they will not be covered in this article.
Running Silent
The trick to running a console window silent is burried in theSTARTUPINFOstructure that we will populate then pass into the
createProcessfunction.
STARTUPINFOspecifies the main window properties. There are many items in the
STARTUPINFOstructure that we just don't care about. The ones that are of interest to us are the
DWORD cb
DWORD dwFlags
WORD wShowWindow
First the
STARTUPINFOis instantiated
STARTUPINFO StartupInfo;
then the memory is cleared for the length of the structure.
memset(&StartupInfo, 0, sizeof(StartupInfo));
or
::ZeroMemory(&StartupInfo, sizeof(StartupInfo));
If this is not done our data within our
STARTUPINFOobject could be corrupt rendering it useless and possible crashing the application.
Next we will fill our structure with the relevant code that will tell our console window to start up without showing itself.
// set the size of the structure StartupInfo.cb = sizeof(STARTUPINFO); // tell the application that we are setting the window display // information within this structure StartupInfo.dwFlags = STARTF_USESHOWWINDOW; // set the window display to HIDE StartupInfo.wShowWindow = SW_HIDE;
Well that's not much of a trick but remember unless you specify the
dwFlagsto the flag
STARTF_USESHOWWINDOWthe setting in
wShowWindowwill not be recognized and your window will not be hidden.
Davy Jones' Locker
Well this is where I will leave you to it.To test this use the function source below and paste it into your application or Download the project file above and test it out. The application will open the IPConfig.exe application in a hidden window, pipe the text to a file and then display the contents of that file in the main application window.
DWORD RunSilent(char* strFunct, char* strstrParams){ STARTUPINFO StartupInfo; PROCESS_INFORMATION ProcessInfo; char Args[4096]; char *pEnvCMD = NULL; char *pDefaultCMD = "CMD.EXE"; ULONG rc; memset(&StartupInfo, 0, sizeof(StartupInfo)); StartupInfo.cb = sizeof(STARTUPINFO); StartupInfo.dwFlags = STARTF_USESHOWWINDOW; StartupInfo.wShowWindow = SW_HIDE; Args[0] = 0; pEnvCMD = getenv("COMSPEC"); if(pEnvCMD){ strcpy(Args, pEnvCMD); } else{ strcpy(Args, pDefaultCMD); } // "/c" option - Do the command then terminate the command window strcat(Args, " /c "); //the application you would like to run from the command window strcat(Args, strFunct); strcat(Args, " "); //the parameters passed to the application being run from the command window. strcat(Args, strstrParams); if (!createProcess( NULL, Args, NULL, NULL, FALSE, create_NEW_CONSOLE, NULL, NULL, &StartupInfo, &ProcessInfo)) { return GetLastError(); } WaitForSingleObject(ProcessInfo.hProcess, INFINITE); if(!GetExitCodeProcess(ProcessInfo.hProcess, &rc)) rc = 0; CloseHandle(ProcessInfo.hThread); CloseHandle(ProcessInfo.hProcess); return rc; }
相关文章推荐
- 关于浏览器页面直接调用本地exe和java代码调用本地exe问题
- 关于vc++调用 exe文件的问题
- 请教一个关于WEBFORM 调用外部EXE程序的问题
- 关于VC调用dll,exe时引用目录路径问题
- 关于vs2012/2013的C编译器生成的exe的向后兼容xp的问题
- 关于调用系统拍照返回图片报错的问题
- 记录下关于调用RAR解压缩的问题
- Android 关于调用系统内已安装的相机问题
- 遇到一个关于调用javascript语句不起作用的问题
- 关于C#中调用C++dll传递回调函数的问题
- 关于private成员、protected成员、public成员调用问题
- 关于vb.net里面api Unicode/ANSI调用一个问题解答
- 关于ajax代码包含到页面不能调用的问题
- 小议函数调用—关于调用后的值保存问题
- 关于struts2.5.2 初步学习 遇到的几点改变以及使用通配符调用无效的问题
- [转]关于C与C++中互相调用动态链接库的问题
- 关于Fragment调用getActivity的一些问题
- 关于在manageBean中调用业务方法的问题
- 【转】修复关于apache-xampp的问题:Port 443 in use by “vmware-hostd.exe”!
- 关于QWidget的子类调用show()方法不能显示的问题