Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
478 views
in Technique[技术] by (71.8m points)

c++ - 如何使用CreateProcess在cmd中执行命令?(How to execute a command in cmd using CreateProcess?)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try using this:

(尝试使用此:)

wchar_t command[] = L"nslookup myip.opendns.com. resolver1.opendns.com";

wchar_t cmd[MAX_PATH] ;
wchar_t cmdline[ MAX_PATH + 50 ];
swprintf_s( cmdline, L"%s /c %s", cmd, command );

STARTUPINFOW startInf;
memset( &startInf, 0, sizeof startInf );
startInf.cb = sizeof(startInf);

PROCESS_INFORMATION procInf;
memset( &procInf, 0, sizeof procInf );

BOOL b = CreateProcessW( NULL, cmdline, NULL, NULL, FALSE,
    NORMAL_PRIORITY_CLASS | CREATE_NO_WINDOW, NULL, NULL, &startInf, &procInf );

DWORD dwErr = 0;
if( b ) 
{
    // Wait till process completes
    WaitForSingleObject( procInf.hProcess, INFINITE );
    // Check process’s exit code
    GetExitCodeProcess( procInf.hProcess, &dwErr );
    // Avoid memory leak by closing process handle
    CloseHandle( procInf.hProcess );
} 
else 
{
    dwErr = GetLastError();
}
if( dwErr ) 
{
    wprintf(_T(“Command failed. Error %d
”),dwErr);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...