<转>Running console applications silently

简介: Introduction Arrrgh! So you need to run your console app via a primary application, well ye've come to the right place.

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 the STARTUPINFO structure that we will populate then pass into the CreateProcess function. STARTUPINFO specifies 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 dwFlags to the flag STARTF_USESHOWWINDOW the 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;
	
}

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

相关文章
|
3天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
278 116
|
18天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
6天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
392 38
Meta SAM3开源:让图像分割,听懂你的话
|
13天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
669 220
|
1天前
|
Windows
dll错误修复 ,可指定下载dll,regsvr32等
dll错误修复 ,可指定下载dll,regsvr32等
129 95
|
11天前
|
人工智能 移动开发 自然语言处理
2025最新HTML静态网页制作工具推荐:10款免费在线生成器小白也能5分钟上手
晓猛团队精选2025年10款真正免费、无需编程的在线HTML建站工具,涵盖AI生成、拖拽编辑、设计稿转代码等多种类型,均支持浏览器直接使用、快速出图与文件导出,特别适合零基础用户快速搭建个人网站、落地页或企业官网。
1639 157
|
存储 人工智能 监控
从代码生成到自主决策:打造一个Coding驱动的“自我编程”Agent
本文介绍了一种基于LLM的“自我编程”Agent系统,通过代码驱动实现复杂逻辑。该Agent以Python为执行引擎,结合Py4j实现Java与Python交互,支持多工具调用、记忆分层与上下文工程,具备感知、认知、表达、自我评估等能力模块,目标是打造可进化的“1.5线”智能助手。
906 61