Application to accept arguments while running.
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: Application to accept arguments while running.

  1. #1
    Join Date
    Jul 2011
    Posts
    3

    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?

  2. #2
    Join Date
    Mar 2001
    Posts
    2,374

    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.

  3. #3
    Join Date
    Jul 2011
    Posts
    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?

  4. #4
    Join Date
    Aug 2000
    Location
    New York, NY, USA
    Posts
    5,546

    Re: Application to accept arguments while running.

    Quote Originally Posted by chrys7 View Post
    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:
    FeinViewer - an integrated GDI objects viewer for Visual C++ Debugger, and more...

  5. #5
    Join Date
    Jul 2011
    Posts
    3

    Smile Re: Application to accept arguments while running.

    Quote Originally Posted by VladimirF View Post
    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 02:33 AM.

  6. #6
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,051

    Re: Application to accept arguments while running.

    Best regards,
    Igor

  7. #7
    Arjay's Avatar
    Arjay is offline Moderator / MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    11,441

    Re: Application to accept arguments while running.

    Quote Originally Posted by chrys7 View Post
    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.

  8. #8
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,544

    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
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center