NO

Author Topic: Windows Example: Overcoming File Association Limits  (Read 6695 times)

Anonymous

  • Guest
Windows Example: Overcoming File Association Limits
« on: May 04, 2005, 01:38:51 PM »
Ok you've just written a rather complicated program that can handle multiple documents.  You set up a file association for the program's files so that your users can click on a file and launch the program.

All standard so far.

Now when a user clicks on a file, your program launches.

When they click on a second file, it launches again, so that you have two copies running at the same time.  If these two copies have files in common (eg. settings) this can be very chaotic.  

So you create a Mutex (or other signal) to tell any second copies to not load, so that you only ever have one copy running at a time.

Now when a user clicks a file, your program launches as normal.
However, when they click the second file, your program fails... effectively rendering file associations useless for opening more files.

The example below shows a way around this problem...

It only runs one copy at a time (using Mutex).  However, it's got a new trick...

When you launch the first copy, it parses it's command line normally, loading the file(s) the user has clicked on.

However, if the user clicks on a second file (drops files on the icon or the program, etc), the file association does not fail.  Instead, the command line from the second copy is handed off to the first copy which loads the file... so that the file association is still active.

The Demo program is a small exe that parses and displays it's command lines.  To see the "Smarter Associations" at work, create a temporary file association by right clicking on a file using "open with" to select the demo program.  The first time you do this, you will see Mutex Demo launch and display it's command line... the second time is where the good stuff happens... launching a second copy, you will see it's command line pop up in the window of the first copy.  

With a little extra code you could use this to load files into MDI edit windows, to build lists of songs to play... just about anything.

I hope you find this usefull!

JohnF

  • Guest
Windows Example: Overcoming File Association Limits
« Reply #1 on: May 05, 2005, 09:45:58 AM »
That's quite neat, thanks.

John

Anonymous

  • Guest
Windows Example: Overcoming File Association Limits
« Reply #2 on: May 06, 2005, 09:25:56 AM »
Quote from: "JohnF"
That's quite neat, thanks.
John


My pleasure.

I've always thought that if we shared our little tricks and tips, the world would be filled with better software.