让scanf,从管道读取等等(WinAPI)

时间:2017-09-16 07:00:51

标签: winapi pipe scanf

我想发送一个程序,该程序可能使用scanf,获取或类似的东西,一些输入使用来自winapi程序的管道。写作似乎有效,但我需要做些什么才能让其他程序读取这个输入?

一个例子是这样的:

int main()
{
 HANDLE outputhandlewrite;
 HANDLE outputhandleread;
 HANDLE inputhandlewrite;
 HANDLE inputhandleread;

 char gdbpath = ".\SysGCC\bin\arm-linux-gnueabihf-gdb.exe";
 char gdbcommand = "help";

 SECURITY_ATTRIBUTES saAttr;

 PROCESS_INFORMATION piProcInfo;
 STARTUPINFO siStartInfo;

 BOOL bSuccess

 saAttr.nLength = sizeof(saAttr);
 saAttr.bInheritHandle = TRUE; 
 saAttr.lpSecurityDescriptor = NULL; 

 CreatePipe(&outputhandleread,&outputhandlewrite,&saAttr,0);
 SetHandleInformation(outputhandleread,HANDLE_FLAG_INHERIT,0);
 CreatePipe(&inputhandleread,&inputhandlewrite,&saAttr,0);
 SetHandleInformation(inputhandlewrite,HANDLE_FLAG_INHERIT,0);

 ZeroMemory(&piProcInfo,sizeof(piProcInfo));
 ZeroMemory(siStartInfo,sizeof(siStartInfo));

 siStartInfo.cb = sizeof(STARTUPINFO);
 siStartInfo.hStdError = g_hChildStd_OUT_Wr;
 siStartInfo.hStdOutput = g_hChildStd_OUT_Wr;
 siStartInfo.hStdInput = g_hChildStd_IN_Rd;
 siStartInfo.dwFlags |= STARTF_USESTDHANDLES;

CreateProcess(NULL,gdbpath,NULL,NULL,TRUE,0,NULL,NULL,&piProcInfo,&siStartInfo);

for (;;)
{
for (i = 0; i < (*ds).mds.mdscs.debuggerlineswidth; i++)
{
    bSuccess = ReadFile((*ds).debuggeroutputdata.outputhandle, pipebuffer + i, 1, &bytesread, NULL);

    if (*(pipebuffer + i) == '\r')
    {
        bSuccess = ReadFile((*ds).debuggeroutputdata.outputhandle, pipebuffer + i, 1, &bytesread, NULL);
        break;
    }
}

*(pipebuffer + i) = 0;

//Here is just Code for printing the received ouput to the window
}

        bSuccess = WriteFile((*ds).debuggerinputdata.inputhandle, (*ds).debuggerinputdata.inputstring, strlen((*ds).debuggerinputdata.inputstring) + 1, &byteswritten, NULL);
        bSuccess = WriteFile((*ds).debuggerinputdata.inputhandle, (*ds).debuggerinputdata.inputstring, 0, &byteswritten, NULL);
}

创建过程有效,读取工作和WriteFile返回成功,写入的字节数等于输入字符串的长度。enter code here

1 个答案:

答案 0 :(得分:0)

当您在Custom controller / view实例中使用CreateProcess传递时,将您读取的管道末尾作为标准输入句柄传递。

如果我没记错的话,你需要用可继承的句柄创建管道。