使用命令行与C ++中的程序进行通信 [英] Using command line to communicate with a program in C++

查看:109
本文介绍了使用命令行与C ++中的程序进行通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在将Visual Studio 2005和C ++与Win32和MFC一起使用.我想知道在程序已经运行时是否可以使用命令行与我的程序进行通信.因此,例如,当我的程序正在运行时,我希望能够使用命令行告诉它执行某些操作.我该怎么做?

Hi all,

I''m using Visual Studio 2005 and C++ with both Win32 and MFC. I''m wondering if there is a way to use the command line to communicate with my program, while the program is already running. So, for instance, while my program is running, I''d like to be able to use the command line to tell it to do something. How do I do this??

Thanks!

推荐答案

可以通过多种方式进行进程间通信,例如管道,Windows消息,事件等.
要从命令行使用它们,您必须使用某种脚本语言(如perl),否则您将需要编写实用程序,然后从命令行运行它.
There are many ways in which you can do interprocess communication like pipes, windows messages, events etc.
To use these from the command line, you must use some sort of scripting language like perl or you will need to write a utility and then run it from the command line.


您可以"请使用命令行执行此操作.您可以使程序提供它自己的命令窗口",使您在键入内容时可以执行任务".

您还可以编写另一个命令行应用程序,然后可以将参数传递给该命令行应用程序,然后再指示运行中的应用程序执行某项操作.有几种方法可以做到这一点,我建议您用谷歌搜索"MFC进程间通信"一词,以了解可用的内容.

我想我在家中仍有一些代码,这些代码使用注册的消息在应用程序之间来回传输数据.如果我记得的话,我会看看有什么,如果有兴趣的话,将其发送给您.
You can''t use the commandline to do that. You could make your program provide a "command window" of it''s own that would allow you to have it "do stuff" when you type something in.

You could also write another commandline app that you could pass parameters to that would then instruct your alreay running application to do something. There are a few ways to go about ddoing this, and I suggest you google the phrase "MFC interprocess communications" to get an idea of what''s available to you.

I think I still have some code at home that uses registered messages to transfer data back and forth between applications. If I remember, I''ll see what I have, and send it to you if you''re interested.


您可以执行此操作,让应用处理命令行.如果实例已经在运行,则当前实例将向该实例发送一条消息,告诉它该怎么做然后退出.如果实例未在运行,则当前实例将启动并继续执行命令".

关于第二实例如何与第一实例通信,可以选择几个选项.最简单的是WM_COPYDATA,如果您的应用程序是控制台应用程序,那么您仍然可以拥有一个仅用于处理消息的隐藏窗口.
You could do this, have the app handle the command line. If an instance is already running, then the current instance will send a message to that instance telling it what to do and then exit. If an instance is not running, the current instance will start and proceed to execute the "command".

As to how the 2nd instance can communicate with the first, there are several options you can choose from. The simplest would be WM_COPYDATA, and if your app is a console app, you could still have a hidden window merely to handle messages.


这篇关于使用命令行与C ++中的程序进行通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆