在Windows下的Java应用程序中编写C控制台应用程序
发布时间:2020-12-13 21:34:49 所属栏目:Windows 来源:网络整理
导读:如何在 Windows下将字符串数据从 Java发送到C控制台应用程序?我想这样做: BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream()));String o = ...;proc.getOutputStream().write(o.getBytes()); 但是当我这样做时,我从
如何在
Windows下将字符串数据从
Java发送到C控制台应用程序?我想这样做:
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())); String o = ...; proc.getOutputStream().write(o.getBytes()); 但是当我这样做时,我从未在C方面看到它: ReadFile(stdin_h,buf,sizeof(buf),&bytes,0) ReadFile永远不会返回. 以下是进一步的详细说明和示例代码. 我编写了一个简单的C控制台(Win32)应用程序,它从STDIN读取并根据输入执行操作. 现在我想编写一个Java应用程序来“驱动”C应用程序. Java应用程序应该: >使用Runtime.exec()启动C应用程序 我的Java应用程序似乎正在运行,但C应用程序从未在STDIN上接收任何数据. 这是C应用程序: int main() { ofstream f("c:temphacks.txt"); HANDLE stdin_h = GetStdHandle(STD_INPUT_HANDLE); DWORD file_type = GetFileType(stdin_h); if( file_type != FILE_TYPE_CHAR ) return 42; f << "Pipe" << endl; for( bool cont = true; cont; ) { char buf[64*1024] = {}; DWORD bytes = 0; if( ReadFile(stdin_h,0) ) { string in(buf,bytes); cout << "Got " << in.length() << " bytes: '" << in << "'" << endl; f << "Got " << in.length() << " bytes: '" << in << "'" << endl; if( in.find('Q') ) cont = false; } else { cout << "Err " << GetLastError() << " while reading file" << endl; f << "Err " << GetLastError() << " while reading file" << endl; } } } 这是Java方面: public static void main(String[] args) { Runtime rt =Runtime.getRuntime(); try { Process proc = rt.exec("c:devhacksx64debughacks.exe"); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())); int a = 0; while(a < 5) { String o = (a == 4 ? "Qn" : "An"); proc.getOutputStream().write(o.getBytes()); System.out.println("Wrote '" + o + "'"); ++a; } try { proc.waitFor(); // TODO code application logic here } catch (InterruptedException ex) { Logger.getLogger(Java_hacks.class.getName()).log(Level.SEVERE,null,ex); } } catch (IOException ex) { Logger.getLogger(Java_hacks.class.getName()).log(Level.SEVERE,ex); } } Java端似乎工作正常,但我没有收到C端的字符串. 我在这里做错了吗?如何在Windows下将字符串数据从Java发送到C控制台应用程序?
写完5个字符串后,为什么不刷新Java端的输出流?
proc.getOutputStream().flush(); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows – lastLogon与Active Directory中的lastLogonTime
- windows – 将敏感凭据放入AWS实例的userdata属性中
- windows-server-2008 – 如何编辑另一个磁盘的启动配置数据
- windows-server-2008-r2 – “无法打开服务器服务性能对象.
- Windows 安装mysql
- Win10和Win7共享打印机设置方法
- opencv-windows安装教程
- wpf – 在没有多点触控的开发机器上测试Windows 7多点触控?
- .net – 确定菜单项的放置位置是否有任何标准?
- Windows命令解释器:如何获取第一个管道命令的退出代码
推荐文章
站长推荐
- 无法使用ServerGuide实用程序获取IBM xSeries 34
- windows-server-2008 – 在Windows Server 2008上
- windows – 从批处理文件中获取默认网关
- .NET Compact Framework(Windows Mobile 6.1,SQL
- microsoft-office – Excel文件越来越大(> 150 M
- windows-phone-7 – 在锁定屏幕下跟踪加速度计
- windows-server-2008-r2 – Win2008R2:暴力攻击
- Windows server 2008启动remote dosktop service
- Cent OS & Windows 双系统自定义引导菜单
- windows-phone-7 – WP7 Mango中的相机捕捉
热点阅读