URGENT: Pass cmd line or other messages to active instance of application

Posted on 2006-05-23
Last Modified: 2010-04-16
I have an app that only allows one instance to be running at a time.  I'm currently using ShowWindow & SetForegroundWindow in the Main entry point to detect the ealier process and then activate it (via the handle, etc) when found & then the 2nd instance shuts down.  This all works well for a very long time now.

However, I now need to modify the load to accept command line args & also files (via a file association).  Right now, everything works fine when there is no other instance open - both the cmd line args and/or the file when double clicked open the application, process the args, etc.  My question is - if in the state where the app is already open, how do I pass the file info or other generic cmd line data to the first already opened instance before shutting the 2nd instance down?

Can this be done easily via SendNotifyMessage, or the like?  Does anyone have any sample C# code that would do this.  I'm using .NET 2.0 if that helps w/ a more elegant solution.

Thanks in advance.
Question by:slapiwite
    LVL 85

    Accepted Solution

    "I'm using .NET 2.0 if that helps w/ a more elegant solution."

    .Net 2.0 has a new "Application Model" that is designed to do just as you are describing.

    Take a look at the StartupNextInstance() event:

    I haven't tried using it with C# before...


    Author Comment

    Hmm.. looks interesting.  There doesn't appear to be a C# equivalent.  However, I suppose I could always add a reference to Microsoft.VisualBasic & then modify how my app is loaded using this new model.

    Or, if I can use the VB design to create a similar 'Application Model' for C#...

    I'll leave htis question open while I play with the two above & see if it does the trick in case anyone else has any ideas that are C# oriented.  Otherwise, all the points are yours - thanks for the help!
    LVL 12

    Expert Comment

    There is a C# approach for this... let me see if I can find the example..
    LVL 12

    Assisted Solution

    From < >

    A single-instance application is one that allows the user to open only one instance at a time. Strictly speaking, a second instance is started, but it detects the presence of the first instance, and shuts itself down.

    Visual Studio 2005 makes single-instance applications very easy. In VB, one simply checks a box on the project settings dialog, and then handles the appropriate events. The code that accomplishes this is in the Microsoft.VisualBasic namespace, but it can be used in a C# application. This article will show you how to do this, and show how to transfer command-line arguments from the second instance to the original one.

    // the link has details and source code


    Author Comment

    I've split the comments - they were both helpful in finding a solution.

    Thanks to both of you.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    In one of my recent projects, I was working with IP cameras, I need to take some pictures from the camera and do some processing on it. The first step, was to retrieve the image from camera into Image object. So that it can be displayed or …
    Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    794 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now