I'm trying to launch the command line through my c++ program and then have cmd run a command.
(我试图通过我的C ++程序启动命令行,然后让cmd运行命令。)
I'm not sure what I'm doing wrong. (我不确定自己在做什么错。)
I've looked at the MSDN documentation but I'm unable to understand what to change in my code. (我查看了MSDN文档,但无法理解代码中要更改的内容。)
Below is the chunk of code that I have written.
(以下是我编写的代码块。)
I'm trying to launch cmd and then run the command in cmdArgs. (我正在尝试启动cmd,然后在cmdArgs中运行命令。)
However, on running the program it just launches the cmd without running the nslookup part of it. (但是,在运行程序时,它仅启动cmd而没有运行它的nslookup部分。)
I've tried with other commands as well like ipconfig, but they do not get executed. (我已经尝试使用其他命令以及ipconfig之类的命令,但是它们没有被执行。)
Could someone help me understand what I'm doing wrong. (有人可以帮助我了解我在做什么错。)
When I launch the program, it just opens up cmd.
(当我启动程序时,它只是打开cmd。)
What I'm trying to do is have the cmdArgs runs and view the output on the cmd screen. (我想做的是让cmdArgs运行并在cmd屏幕上查看输出。)
I'm new to c++, so if this is trivial I apologize.
(我是C ++的新手,所以如果这很简单,我深表歉意。)
I've looked at other questions on the site, but it seems that the format of cmdArgs is correct - program name followed by the arg. (我看过网站上的其他问题,但看来cmdArgs的格式是正确的-程序名后跟arg。)
STARTUPINFO si;
PROCESS_INFORMATION pi;
ZeroMemory(&si, sizeof(si));
si.cb = sizeof(si);
ZeroMemory(&pi, sizeof(pi));
LPTSTR cmdPath = _T("C:\Windows\System32\cmd.exe");
LPTSTR cmdArgs = _T("C:\Windows\System32\cmd.exe nslookup myip.opendns.com. resolver1.opendns.com");
if (!CreateProcess(cmdPath, cmdArgs, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi))
{
std::cout << "Create Process failed: " << GetLastError() << std::endl;
return "Failed";
}
ask by spdcbr translate from so 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…