如何使用命令行参数为正在运行的应用程序提供不同的命令. [英] how to use command line parameter to give differnt command for the running application.

查看:126
本文介绍了如何使用命令行参数为正在运行的应用程序提供不同的命令.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个可以在不同模式下运行的vc ++ 6.0应用程序.我需要使用命令行参数在不同模式之间进行切换.即使用命令行参数运行应用程序后,如何再次使用命令行参数为正在运行的应用程序提供不同的命令.

预先谢谢您.

I have an vc++ 6.0 application which can run in different modes. I need to switch between different modes using command line parameters.i.e after running an appliction using command line parameters how can i again use command line parameter to give different command for the running application.

Thank you in advance

推荐答案

据我了解,您想使用命令行参数来修改已经运行的应用程序的行为.这实际上意味着您需要启动同一应用程序的第二个实例,即第二个进程,并在这些进程之间组织IPC.

您需要实现的第一件事是认识到流程的第一个实例已经在运行.有一种方法可以使用与传递命令行参数相同的机制.

我更喜欢使用命名管道的IPC技术.其中一个进程扮演了命名管道服务器的角色.

请从MSDN上查看有关如何实现这种服务器的代码: http://code.msdn.microsoft.com/CppNamedPipeServer-d1778534 [ ^ ].

现在,应用程序首先要做的是尝试连接到现有的命名管道服务器.管道的名称应该是系统唯一的.我更喜欢使用主应用程序可执行模块的完整路径名作为此类名称.在这种情况下,同一应用程序在不同目录中的副本无法与源应用程序通信,但这并不重要.我们将考虑从相同的可执行模块及其位置运行的过程的第一个和第二个实例对.

现在,当应用程序实例的一个实例无法连接到命名管道服务器时,这意味着该实例是第一个实例,而先前的实例尚未运行.在这种情况下,该过程应开始扮演命名管道服务器的角色.该进程的第二个实例将成功连接到该服务器,因此这表明该实例实际上是第二个实例.当发生这种情况时,该过程的第二个实例应与命名服务器开始事务并传递命令行参数;完成此操作后,该实例应断开连接并立即完成执行.

运行命名管道服务器的应用程序进程应在单独的线程中运行它,并使用线程通信原语与其他线程进行通信.

要实现此机制,需要付出很大的努力.

还有其他机制,例如,使用共享内存块传递数据.刚好是第一个进程的实例创建了共享内存块,并在退出时将其删除.对共享内存的访问应使用另一个用于互锁访问的IPC原语(例如名为互斥体)来保护.请参阅 http://msdn.microsoft.com/en-us/library/ms682411(v = vs.85).aspx [ http ://msdn.microsoft.com/zh-CN/library/aa366551(v = vs.85).aspx [
As I understand, you want to use command-line parameters to modify behavior of application which is already running. It really means you need to start a second instance of the same application, that is, the second process and organize IPC between these processes.

First thing you need to implement is the recognition of the fact that the first instance of the process is already running. There is a method to use the same mechanism used for passing command-line parameters.

I prefer the IPC technique using named pipes. One of the processes plays the role of named-pipe server.

Please see this code from MSDN on how to implement such server: http://code.msdn.microsoft.com/CppNamedPipeServer-d1778534[^].

Now, first thing the application should do it to attempt to connect to existing named pipe server. The name of the pipe should be system-unique. I prefer to use the full path name of the main application executable module for such name. In this case, the copy of the same application in different directory cannot communicate with the source application, but this is not important. We shall consider the pairs of first and second instance of the process run from the identical executable module and its location.

Now, when an instance of the application process is failed to connect to the named pipe server, it means that this instance is the first on, previous instance is not running yet. In this case, the process should start playing the role of the named pipe server. The second instance of the process will successfully connect to this server, so this is the indication that this instance is really the second one. When it happens, the second instance of the process should start transaction with the named server and pass the command line parameters; when this is done, this instance should disconnect and finish execution immediately.

The application process running named pipe server should run it in a separate thread and communicate with other thread using thread communication primitives.

Implementation of this mechanism requires considerable effort.

There are other mechanism, for example, using shared memory block for passing data. The instance of the process which happened to be a first one creates the shared memory block and removed it on exit. The access to the shared memory should be guarded using another IPC primitive for interlocking access, such as named mutex. See http://msdn.microsoft.com/en-us/library/ms682411(v=vs.85).aspx[^], http://msdn.microsoft.com/en-us/library/aa366551(v=vs.85).aspx[^].

—SA


这篇关于如何使用命令行参数为正在运行的应用程序提供不同的命令.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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