-
July 14th, 2011, 07:15 AM
#1
Application to accept arguments while running.
I am using visual studio 2008 and MFC. I accept arguments using a subclass of CCommandLineInfo and overriding ParseParam().
Now I want to pass these arguments to the application while running. For example "test.exe /start" and then to type in the console "test.exe /initialize" to be initialized again.
is there any way to do that, or maybe i am in the wrong path?
-
July 14th, 2011, 08:23 AM
#2
Re: Application to accept arguments while running.
Well that would run the program again.
I could see having it work this way:
1. Create a shared memory area in a dll.
2. Run the program and load the DLL.
3. Create another program that has access to the shared memory area and pass values to it this way.
Or you could create a COM client and Server or use TCP Sockets.
HTH,
ahoodin
To keep the plot moving, that's why.
-
July 14th, 2011, 08:56 AM
#3
Re: Application to accept arguments while running.
So, my approach is wrong.
My program is actually a dialog. So how can this dialog have access to this shared memory you are referring? With a timer maybe?
-
July 14th, 2011, 10:40 AM
#4
Re: Application to accept arguments while running.
Originally Posted by chrys7
My program is actually a dialog. So how can this dialog have access to this shared memory you are referring? With a timer maybe?
If you have a dialog, WHY would you want to control it from the console???
Vlad - MS MVP [2007 - 2012] - www.FeinSoftware.com
Convenience and productivity tools for Microsoft Visual Studio:
FeinWindows - replacement windows manager for Visual Studio, and more...
-
July 15th, 2011, 01:24 AM
#5
Re: Application to accept arguments while running.
Originally Posted by VladimirF
If you have a dialog, WHY would you want to control it from the console???
The client pays for the program and the client has this requirement
Last edited by chrys7; July 15th, 2011 at 01:33 AM.
-
July 15th, 2011, 01:48 AM
#6
Re: Application to accept arguments while running.
Best regards,
Igor
-
July 15th, 2011, 02:06 AM
#7
Re: Application to accept arguments while running.
Originally Posted by chrys7
The client pays for the program and the client has this requirement
Sometimes it's good to question the client requirements, understand them, and then give the client something more conventional.
Be aware that everytime you call test.ext /start or test.exe /initialize you are actually creating a new instance of the program (and two test.exe will appear in the task manager).
So the first problem to solve is to detect when an instance of your program is already running. Usually a named mutex is used for the detection part.
You'll need to detect the previous app instance and then pass the new command to it. There are many ways to do this, but for this task I find using a memory mapped file (backed by the system page file) very simple.
For a single instance implementation, see my reply here.
http://www.codeguru.com/forum/showth...81#post1624981
For a class that wraps a structure around a memory mapped file, see this code guru article.
C++ Programming: Memory Mapped Files Using RAII
Between both pieces of sample code you should have enough to get the job done.
The single instance code is a sample of a dialog app that is limited to a single instance. When a user opens a second instance of the dialog, the first instance is brought to the foreground. It does this by storing the hwnd of the first instance in a memory mapped file. When the second instance starts, it detects the first instance and reads the hwnd of the first instance out of the memory mapped file and brings it to the foreground.
If I recall, I manually created the memory mapped file in the single instance example rather than using the memory mapped file wrapper class of the second example. You'll see using the MmfFileStructT<> wrapper class is very simple to implement a memory mapped file.
-
July 15th, 2011, 02:46 AM
#8
Re: Application to accept arguments while running.
Another possibility would be Using WM_COPYDATA message to send the new command line options to the "first instance".
Note that it can replace only the second part (memory mapped file) of the whole procedure described by Arjay; the first part (detect the previous app instance) is the same.
Victor Nijegorodov
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|