c ++读取java进程的输出

c ++读取java进程的输出,第1张

概述c ++读取java进程输出

我有一个运行java进程并读取输出的C ++程序。 我使用了以下MSDN代码:

http://msdn.microsoft.com/en-us/library/ms682499%28v=vs.85%29.aspx

但是,在阅读输出的最后一行时

bSuccess = Readfile(g_hinputfile,chBuf,BUFSIZE,&DWRead,NulL);

bsucces等于1,所以程序继续到下一个循环,当到达同一行时,程序只是“飞行”,无一例外地停下来deBUGging,断点从不移动到下一行。

获取已经运行的windows进程控制台输出在命令提示符? 直接StreamReader来命令提示符

windows上的“系统”和“系统空闲进程”PID是否是常量?

我可以通过任务pipe理器来处理我的windows进程吗?

运行Apache 2.2作为一个单一的httpd.exe进行deBUGging

服务器devise模式/最佳实践的最佳来源是什么?

我想这是因为没有EOF来表明停止阅读。 但是,java 没有 EOF charcater。 Java程序只是做:

System.out.close();

最后。

我可以在C ++ Java代码中做些什么来修复它?

编辑:

这是代码。

它不适用于任何控制台应用程序,读取结束然后“挂起”。

HANDLE g_hChildStd_IN_Rd = NulL; HANDLE g_hChildStd_IN_Wr = NulL; HANDLE g_hChildStd_OUT_Rd = NulL; HANDLE g_hChildStd_OUT_Wr = NulL; HANDLE g_hinputfile = NulL; PROCESS_informatION piProcInfo; voID CreateChildProcess() // Create a child process that uses the prevIoUsly created pipes for STDIN and STDOUT. { std::string d=processLoaction; TCHAR *cmdline=new TCHAR[d.size()+1]; cmdline[d.size()]=0; std::copy(d.begin(),d.end(),cmdline); STARTUPINFO siStartInfo; BOol bSuccess = FALSE; // Set up members of the PROCESS_informatION structure. ZeroMemory( &piProcInfo,sizeof(PROCESS_informatION) ); // Set up members of the STARTUPINFO structure. // This structure specifIEs the STDIN and STDOUT handles for redirection. ZeroMemory( &siStartInfo,sizeof(STARTUPINFO) ); siStartInfo.cb = sizeof(STARTUPINFO); siStartInfo.hStdError = g_hChildStd_OUT_Wr; siStartInfo.hStdOutput = g_hChildStd_OUT_Wr; siStartInfo.DWFlags |= STARTF_USESTDHANDLES; // Create the child process. bSuccess = CreateProcess(NulL,cmdline,NulL,TRUE,&siStartInfo,&piProcInfo); // receives PROCESS_informatION // If an error occurs,exit the application. if ( ! bSuccess ) throw new Exception("Failed to create process"); else { // Close handles to the child process and its primary thread. // Some applications might keep these handles to monitor the status // of the child process,for example. CloseHandle(piProcInfo.hProcess); CloseHandle(piProcInfo.hThread); } } voID ReadFromPipe(voID) // Read output from the child process's pipe for STDOUT // and write to the parent process's pipe for STDOUT. // Stop when there is no more data. { DWORD DWRead,DWWritten,status; CHAR chBuf[BUFSIZE]; BOol bSuccess = FALSE; HANDLE hParentStdOut = GetStdHandle(STD_OUTPUT_HANDLE); BOol res; for (;;) { bSuccess = Readfile( g_hChildStd_OUT_Rd,NulL); if( ! bSuccess || DWRead == 0 ) break; chBuf[DWRead] = NulL; bSuccess = Writefile(hParentStdOut,DWRead,&DWWritten,NulL); if (! bSuccess ) break; } } int main(int argc,char** argv) { //run the Jar to valIDate manifest & jeff Security_ATTRIBUTES saAttr; // Set the binheritHandle flag so pipe handles are inherited. saAttr.nLength = sizeof(Security_ATTRIBUTES); saAttr.binheritHandle = TRUE; saAttr.lpSecurityDescriptor = NulL; // Create a pipe for the child process's STDOUT. if ( ! CreatePipe(&g_hChildStd_OUT_Rd,&g_hChildStd_OUT_Wr,&saAttr,0) ) throw new Exception("faild to create a pipe for the child process's STDOUT"); // Ensure the read handle to the pipe for STDOUT is not inherited. if ( ! SetHandleinformation(g_hChildStd_OUT_Rd,HANDLE_FLAG_inherit,0) ) throw new Exception("Read handle to the pipe for STDOUT is inherited"); CreateChildProcess(); ReadFromPipe(); ... }

用窗户把手把窗户做成最高层

windows中创build过程是什么?

如何触发和忘记一个subprocess?

我怎样才能设置善良和进程亲和力在同一时间?

在windows中创build一个后台进程

这个问题似乎在你链接的页面的一些注释中得到了解决:你必须在创建子进程之后关闭管道的写入端,但是在从另一端开始读取之前。 如果你不这样做,即使子进程关闭它的输出,管道的写入端仍然是打开的(由父进程),所以EOF永远不会到达。

啊,不要忘了关闭父进程的所有句柄。 链接页面中的示例没有,并且泄漏!

总结

以上是内存溢出为你收集整理的c ++读取java进程的输出全部内容,希望文章能够帮你解决c ++读取java进程的输出所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1158759.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-01
下一篇 2022-06-01

发表评论

登录后才能评论

评论列表(0条)

保存