jiauan 2023-01-14 10:14 采纳率: 55.6%
浏览 31
已结题

关于vc/mfc调用exe之后,该怎么获取exe运行结果

关于vc/mfc调用exe之后,该怎么获取exe运行结果
如题,当使用ShellExecute等函数调用exe之后,怎么获取到exe内运算的结果 代码如下:
vc调用exe代码:

img

一个用C#生成的exe代码

img

在mfc中调用exe,传入两个参数a,b,在exe执行完成后结果为c,怎么在调用之后直接获取到c的值呢,求解答。
经查阅这应该是属于两个进程直接的通讯?是要用到管道,或者内存共享方面的内容,但是没有找到具体的例子

  • 写回答

3条回答 默认 最新

  • 赵4老师 2023-01-16 09:28
    关注

    仅供参考:

    #pragma comment(lib,"user32")
    #include <stdio.h>
    #include <string.h>
    #include <windows.h>
    int main() {
        SECURITY_ATTRIBUTES sa          = {0};
        STARTUPINFO         si          = {0};
        PROCESS_INFORMATION pi          = {0};
        HANDLE              hPipeOutputRead  = NULL;
        HANDLE              hPipeOutputWrite = NULL;
        HANDLE              hPipeInputRead   = NULL;
        HANDLE              hPipeInputWrite  = NULL;
        BOOL                bTest = 0;
        DWORD               dwNumberOfBytesRead = 0;
        DWORD               dwNumberOfBytesWrite = 0;
        CHAR                szMsg[100];
        CHAR                szBuffer[256];
    
        sa.nLength = sizeof(sa);
        sa.bInheritHandle = TRUE;
        sa.lpSecurityDescriptor = NULL;
    
        // Create pipe for standard output redirection.
        CreatePipe(&hPipeOutputRead,  // read handle
                &hPipeOutputWrite, // write handle
                &sa,      // security attributes
                0      // number of bytes reserved for pipe - 0 default
                );
    
        // Create pipe for standard input redirection.
        CreatePipe(&hPipeInputRead,  // read handle
                &hPipeInputWrite, // write handle
                &sa,      // security attributes
                0      // number of bytes reserved for pipe - 0 default
                );
    
        // Make child process use hPipeOutputWrite as standard out,
        // and make sure it does not show on screen.
        si.cb = sizeof(si);
        si.dwFlags     = STARTF_USESHOWWINDOW | STARTF_USESTDHANDLES;
        si.wShowWindow = SW_HIDE;
        si.hStdInput   = hPipeInputRead;
        si.hStdOutput  = hPipeOutputWrite;
        si.hStdError   = hPipeOutputWrite;
    
        CreateProcess (
              NULL, "cmd.exe",
              NULL, NULL,
              TRUE, 0,
              NULL, NULL,
              &si, &pi);
    
        // Now that handles have been inherited, close it to be safe.
        // You don't want to read or write to them accidentally.
        CloseHandle(hPipeOutputWrite);
        CloseHandle(hPipeInputRead);
    
        // Now test to capture DOS application output by reading
        // hPipeOutputRead.  Could also write to DOS application
        // standard input by writing to hPipeInputWrite.
        sprintf(szMsg, "ver\n");
        WriteFile(
              hPipeInputWrite,      // handle of the write end of our pipe
              &szMsg,               // address of buffer that send data
              strlen(szMsg),        // number of bytes to write
              &dwNumberOfBytesWrite,// address of number of bytes read
              NULL                  // non-overlapped.
              );
    
        while(TRUE)
        {
           bTest=ReadFile(
              hPipeOutputRead,      // handle of the read end of our pipe
              &szBuffer,            // address of buffer that receives data
              256,                  // number of bytes to read
              &dwNumberOfBytesRead, // address of number of bytes read
              NULL                  // non-overlapped.
              );
    
          if (!bTest){
              sprintf(szMsg, "Error #%d reading pipe.",GetLastError());
              printf("%s",szMsg);
              break;
          }
    
          // do something with data.
          szBuffer[dwNumberOfBytesRead] = 0;  // null terminate
          printf("%s",szBuffer);
          if ('>'==szBuffer[dwNumberOfBytesRead-1]) break;
        }
    
        sprintf(szMsg, "chcp\nexit\n");
        WriteFile(
              hPipeInputWrite,      // handle of the write end of our pipe
              &szMsg,               // address of buffer that send data
              strlen(szMsg),        // number of bytes to write
              &dwNumberOfBytesWrite,// address of number of bytes read
              NULL                  // non-overlapped.
              );
    
        while(TRUE)
        {
           bTest=ReadFile(
              hPipeOutputRead,      // handle of the read end of our pipe
              &szBuffer,            // address of buffer that receives data
              256,                  // number of bytes to read
              &dwNumberOfBytesRead, // address of number of bytes read
              NULL                  // non-overlapped.
              );
    
          if (!bTest){
              sprintf(szMsg, "Error #%d reading pipe.",GetLastError());
              printf("%s",szMsg);
              break;
          }
    
          // do something with data.
          szBuffer[dwNumberOfBytesRead] = 0;  // null terminate
          printf("%s",szBuffer);
        }
    
        // Wait for CONSPAWN to finish.
        WaitForSingleObject (pi.hProcess, INFINITE);
    
        // Close all remaining handles
        CloseHandle (pi.hProcess);
        CloseHandle (hPipeOutputRead);
        CloseHandle (hPipeInputWrite);
    
        return 0;
    }
    //C:\test>test
    //Microsoft Windows [版本 5.2.3790]
    //(C) 版权所有 1985-2003 Microsoft Corp.
    //
    //C:\test>ver
    //
    //Microsoft Windows [版本 5.2.3790]
    //
    //C:\test>chcp
    //活动的代码页: 936
    //
    //C:\test>exit
    //Error #109 reading pipe.
    //C:\test>
    
    
    
    评论

报告相同问题?

问题事件

  • 已结题 (查看结题原因) 1月31日
  • 创建了问题 1月14日

悬赏问题

  • ¥15 微信原生小程序tabBar编译报错
  • ¥350 麦克风声源定位坐标不准
  • ¥15 apifox与swagger使用
  • ¥15 egg异步请求返回404的问题
  • ¥20 Ti毫米波雷达板同步
  • ¥15 安装了kali后用VM打开,没有鼠标且无法控制怎么办啊
  • ¥15 关于#python#的问题:无功优化问题数学建模要以3机9节点为算例编写一个以最小有功网损为目标的无功优化问题,想要了解清楚其中数学建模的具体公式进行学习
  • ¥15 mvc采用element分页的问题
  • ¥15 proteus怎样构建他励直流发电机模型?
  • ¥15 求制作PPT有偿,帮