我想从我的C ++代码访问cmd,该怎么做? [英] i want to access cmd from my c++ code, how to do it?

查看:106
本文介绍了我想从我的C ++代码访问cmd,该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从我的VC ++项目访问cmd.我已经读过,可以通过shellapi
完成 但我需要一个详尽的示例来解释每个步骤.因为我是VS的新手,所以我需要知道在哪里包含我拥有的dll.
基本上我必须从cmd执行一个exe文件,这将返回一个文件.我必须处理所有这些.
这可能吗?

任何教程或帮助将不胜感激
问候

I am trying to access cmd from my VC++ project. i have read that it can be done by shellapi
but i need an elaborate example explaining each step. as i am newbie to VS, i need to know where to include the dll that i have got.
basically i have to execute an exe file from cmd which will return a file. i have to handle all that.
is this possible?

Any tutorial or help will be appreciated
regards

推荐答案

您可以使用 ^ ]函数.
:)
You may use the system[^] function.
:)


看看这个通用控制台重定向器:
通用控制台重定向器 [重定向任意控制台的输入/输出 [
Have a look at this Universal Console Redirector:
Universal Console Redirector[^]

Another article about this:
Redirecting an arbitrary Console''s Input/Output[^]

Good luck!


以下代码示例将启动一个可执行文件:
The following code sample will launch an executable file:
STARTUPINFO si;
ZeroMemory( &si, sizeof(si) );
si.cb = sizeof(si);

PROCESS_INFORMATION pi;
ZeroMemory( &pi, sizeof(pi) );

CString sModname;
sModname = "C:\\myApp.exe"; //give application path

CString sCmdLine;
sCmdLine.Format(_T("\\?")); //add command line arguments

// Start the child process. 
if( !CreateProcess((LPTSTR)(LPCTSTR)sModname, (LPTSTR)(LPCTSTR)sCmdLine,
	NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi ) ) 
{
	//process was not started...
}

// Wait until child process exits
while(!waitForThread(pi.hProcess,INFINITE)); //wait without blocking

// Close process and thread handles. 
CloseHandle( pi.hProcess );
pi.hProcess  = 0;
CloseHandle( pi.hThread );
pi.hThread  = 0;


这篇关于我想从我的C ++代码访问cmd,该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆