Windows程序执行CMD命令 不能得到输出结果

最近在学习Windows远程CMD 遇到一些问题
代码如下:

#include <windows.h>
#include <cstdio>

void ShowError(const char* szText)
{
    char szError[MAX_PATH] = { 0 };
    sprintf_s(szError, "%s: %d\n", szText, GetLastError());
    printf("%s", szError);
}
BOOL PipeCmd(char *szCmdLine, char *szResBuff, DWORD dwResBuffSize)
{
    BOOL bRet = FALSE;
    HANDLE hReadPipe = NULL;
    HANDLE hWritePipe = NULL;
    SECURITY_ATTRIBUTES  SecAttributes = { 0 };
    SecAttributes.bInheritHandle = TRUE;
    SecAttributes.lpSecurityDescriptor = NULL;
    SecAttributes.nLength = sizeof(SecAttributes);
    bRet = CreatePipe(&hReadPipe, &hWritePipe, &SecAttributes, 0);
    if (FALSE == bRet)
    {
        ShowError("CreatePipe");
        return FALSE;
    }
    STARTUPINFO si = { 0 };
    GetStartupInfo(&si);
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    si.hStdOutput = hWritePipe;
    si.hStdError = hWritePipe;
    PROCESS_INFORMATION pi = { 0 };
    bRet = CreateProcess(NULL, szCmdLine, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
    if (FALSE == bRet)
    {
        ShowError("CreateProcess");
        return FALSE;
    }
    RtlZeroMemory(szResBuff, dwResBuffSize);
    DWORD dwRes;
    ReadFile(hReadPipe, szResBuff, dwResBuffSize, &dwRes, NULL);
    WaitForSingleObject(pi.hThread, INFINITE);
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(hReadPipe);
    CloseHandle(hWritePipe);
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);
    return TRUE;
}
int main(int argc, char *argv[])
{
    char szCmdLine[] = "whoami";
    char szResBuff[1024] = { 0 };
    DWORD dwResBuffSize = 1024;
    if (!PipeCmd(szCmdLine, szResBuff, dwResBuffSize))
    {
        printf("Pipe CMD ERROR\n");
    }
    else
    {
        printf("Pipe CMD OK\n");
        printf("%s\n", szResBuff);
    }
    return 0;
}


这段程序的目的是执行命令行命令 并且将输出结果写入管道
将管道数据读入数组 最终打印出数组
简而言之就是用程序执行CMD命令并且输出返回结果

img

但执行程序后得不到任何输出

代码有两处问题,一是管线关闭的顺序,还有一处是createprocess参数问题,bInHeritHandles也是一定要设置为TRUE,新的进程能输出信息必须继承调用进程的句柄。我都给你修改可以运行了。有运行结果图片如下。有帮助请采纳,谢谢!

img

#include <windows.h>
#include <cstdio>
#include <processthreadsapi.h>
void ShowError(const char* szText)
{
    char szError[MAX_PATH] = { 0 };
    sprintf_s(szError, "%s: %d\n", szText, GetLastError());
    printf("%s", szError);
}
BOOL PipeCmd(char *szCmdLine, char *szResBuff, DWORD dwResBuffSize)
{
    BOOL bRet = FALSE;
    HANDLE hReadPipe = NULL;
    HANDLE hWritePipe = NULL;
    SECURITY_ATTRIBUTES  SecAttributes = { 0 };
    SecAttributes.bInheritHandle = TRUE;
    SecAttributes.lpSecurityDescriptor = NULL;
    SecAttributes.nLength = sizeof(SecAttributes);
    bRet = CreatePipe(&hReadPipe, &hWritePipe, &SecAttributes, 0);
    if (FALSE == bRet)
    {
        ShowError("CreatePipe");
        return FALSE;
    }
    STARTUPINFO si = { 0 };
    GetStartupInfo(&si);
    si.cb = sizeof(si);
    si.dwFlags = STARTF_USESTDHANDLES | STARTF_USESHOWWINDOW;
    si.wShowWindow = SW_HIDE;
    si.hStdOutput = hWritePipe;
    si.hStdError = hWritePipe;
    PROCESS_INFORMATION pi = { 0 };
    bRet = CreateProcess(NULL, szCmdLine, NULL, NULL, TRUE, 0, NULL, NULL, &si, &pi);
    if (FALSE == bRet)
    {
        ShowError("CreateProcess");
        return FALSE;
    }
    CloseHandle(hWritePipe);

    RtlZeroMemory(szResBuff, dwResBuffSize);
    DWORD dwRes;
    ReadFile(hReadPipe, szResBuff, dwResBuffSize, &dwRes, NULL);
    MessageBox(NULL, szResBuff, "执行结果", MB_OK);

    WaitForSingleObject(pi.hThread, INFINITE);
    WaitForSingleObject(pi.hProcess, INFINITE);
    CloseHandle(hReadPipe);
    //CloseHandle(hWritePipe);
    CloseHandle(pi.hThread);
    CloseHandle(pi.hProcess);

    system("pause");
    return TRUE;
}
int main(int argc, char *argv[])
{
    char szCmdLine[] = "whoami";
    char szResBuff[1024] = { 0 };
    DWORD dwResBuffSize = 1024;
    if (!PipeCmd(szCmdLine, szResBuff, dwResBuffSize))
    {
        printf("Pipe CMD ERROR\n");
    }
    else
    {
        printf("Pipe CMD OK\n");
        printf("%s\n", szResBuff);
    }
    return 0;
}

你试试换个端口,应该可以

WaitForSingleObject(pi.hThread, INFINITE);
WaitForSingleObject(pi.hProcess, INFINITE);
这两句无限等待下去,所以没有返回,在这句前后加入打印试试看,看是不是堵到这里了