Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
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
Posted

1 solution

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
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900