我正在尝试在C++中执行powershell命令,并通过管道获取其输出。
我的程序对于cmd.exe完美地工作。但是,当我尝试使用powershell.exe做同样的事情时,我只得到"W"作为输出。
下面是可以用于cmd.exe的代码,我已经在下面的代码中注释了需要修改以执行powershell.exe的行。
HANDLE stdinRd, stdinWr, stdoutRd, stdoutWr;
DWORD readFromCmd();
DWORD writeToCmd(CString command);
int main(int argc,char* argv[])
{
SECURITY_ATTRIBUTES sa={sizeof(SECURITY_ATTRIBUTES), NULL, true};
if(!CreatePipe(&stdinRd, &stdinWr, &sa, 1000000) || !CreatePipe(&stdoutRd,&stdoutWr, &sa, 1000000))
{
printf("CreatePipe()");
}
STARTUPINFO si;
PROCESS_INFORMATION pi;
GetStartupInfo(&si);
si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
si.wShowWindow = SW_HIDE;
si.hStdOutput = stdoutWr;
si.hStdError = stdoutWr;
si.hStdInput = stdinRd;
// If powershell.exe is invoked, it does not work, however works for cmd.exe
//if(!CreateProcess(TEXT("C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\powershell.exe"), NULL, NULL, NULL, TRUE,0, NULL, TEXT("C:\\Windows"), &si, &pi))
if(!CreateProcess(TEXT("C:\\Windows\\System32\\cmd.exe"), NULL, NULL, NULL, TRUE,0, NULL, TEXT("C:\\Windows"), &si, &pi))
{
printf("CreateProcess()");
printf("CreateProcess() failed in initiatecmd(CString,int) method",0);
return -1;
}
writeToCmd(L"dir");
Sleep(1000);
readFromCmd();
getchar();
TerminateProcess(pi.hProcess,0);
CloseHandle(pi.hProcess);
return 0;
}
DWORD writeToCmd(CString command)
{
DWORD ret;
DWORD numberofbyteswritten;
command.AppendChar('\n');
LPSTR command_ANSI;
int size_needed = WideCharToMultiByte(CP_UTF8,0,command.GetString(),-1,NULL,0,NULL,NULL);
command_ANSI = (LPSTR) calloc(1, ( size_needed + 1 )* sizeof(char));
WideCharToMultiByte(CP_UTF8,0,command.GetString(),-1,command_ANSI,size_needed,NULL,NULL);
ret = WriteFile(stdinWr, command_ANSI, size_needed-1, &numberofbyteswritten, NULL);
if(ret==0)
{
printf("WriteFile()");
printf("WriteFile() method failed in writeToCmd(CString) method",0);
return 0;
}
CStringA temp;
temp.Format("%d",numberofbyteswritten);
temp += " bytes (Command:";
temp+=command;
temp+=") are successfully written to cmd";
printf("%s",temp);
return 1;
}
DWORD readFromCmd()
{
CString output_jsonstring;
DWORD ret;
DWORD dwRead;
while(1)
{
DWORD totalbytesavailable;
if(PeekNamedPipe(stdoutRd, NULL, 0, NULL, &totalbytesavailable, 0) == 0)
{
printf("PeekNamedPipe()");
printf("PeekNamedPipe() method failed in responseHandler() method",0);
return 0;
}
if(totalbytesavailable != 0)
{
char output_cmd[1000000];
if(ReadFile(stdoutRd, output_cmd, min(1000000,totalbytesavailable), &dwRead, NULL)==0)
{
printf("ReadFile()");
printf("ReadFile() method failed in responseHandler() method",0);
return 0;
}
int min = min(1000000,totalbytesavailable);
output_cmd[min]='\0';
printf("\n%s",output_cmd);
}
if(totalbytesavailable == 0)
break;
Sleep(100);
}
return 1;
}
如果使用CreateProcess()用于PowerShell,它的工作方式不同,但我只得到W作为输出。
这是什么原因? 如何解决这个问题?
编辑1: 如果我在一个循环中逐个字符地显示output_cmd,其中i = 0到strlen(output_cmd),我得到如下输出:
Windows PowerShell Copyright (C) 2014 Microsoft Corporation. All rights reserved. PS C:\Windows> 然后应用程序挂起!之后它不会输入任何内容或给出任何输出!
while(1)
是糟糕的代码,一定会在某个时候导致应用程序崩溃。在这里,有很多人会告诉你相反的意见 - 但也有很多人只是业余程序员...这只是一个友好的建议。永远不要使用无限循环。 - specializt