Starting your program by clicking on an associated file
I'm working on a Windows project that should be able to load files that are double clicked from the Windows desktop. I'm fairly new to Windows. On a previous platform an associated file would be passed to the program on the command line. From browsing through the forums it looks like Windows does that as well. That would work fine once. I'm not sure, however, how a second file open request is handled. E.g., a user double clicks a file after your program is already open. Your program is already in the message loop, and therefore won't be checking the command line. I'm assuming that some kind of message must be sent to your program, but I can't seem to locate any information on what that message is. Is there a system message for this purpose or does each program set up its own method when establishing the association between the file type and the program.
If anyone can point me to the right information I would appreciate it.
Terry Kelly
# 1 Re: Starting your program by clicking on an associated file
I'm working on a Windows project that should be able to load files that are double clicked from the Windows desktop. I'm fairly new to Windows. On a previous platform an associated file would be passed to the program on the command line. From browsing through the forums it looks like Windows does that as well. That would work fine once. I'm not sure, however, how a second file open request is handled. E.g., a user double clicks a file after your program is already open. Your program is already in the message loop, and therefore won't be checking the command line. I'm assuming that some kind of message must be sent to your program, but I can't seem to locate any information on what that message is.
No message is sent to your program. Actually, Windows will open a second (or third, forth, ...) instance of your application. If you want different behavior, you'll have to set your app up to detect if another copy is running, then send it a message (using some kind of IPC).
Viggy
# 2 Re: Starting your program by clicking on an associated file
Having two or more instances of the same program seems rather inefficient. Is there a standard way to deal with this issue? (IPC doesn't mean anything to me yet)
Terry
# 3 Re: Starting your program by clicking on an associated file
Why? There are plenty of programs that run multiple instances. It really depends on what your application does. If you only want one instance of your application to run, you can create a named mutex in your app's initialization routine. If the mutex already exists, then you know that another instance is running, and you can close the current instance.
Another solution is written up here:
http://www.dev-archive.com/cpp/misc/misc/applicationcontrol/article.php/c10799/
Viggy
# 4 Re: Starting your program by clicking on an associated file
I suppose it just means a waste of memory. My application is a musical score editor designed to work with multiple documents. The only real complication I can foresee from multiple instances would be that I maintain my own clipboard. Since the file format is proprietary I never bothered to implement the Windows Clipboard. Multiple instances would not have access to the same clipboard as it stands.
Either way I have some learning to do. I don't usually balk at that, but I find the Windows documentation a chore.
Thanks for the link. The code there was well documented and easy to understand.
Terry
# 5 Re: Starting your program by clicking on an associated file
Having two or more instances of the same program seems rather inefficient. Is there a standard way to deal with this issue? (IPC doesn't mean anything to me yet)
Terry
yes. It is called DDE.
MFC framework supports this. I am not sure if your application uses MFC, but essentially, it falls into this:
Creating a bunch of registry entries ( key called ddeexec for the document type ) and listening to DDE messages.
In MFC, the first part is done by RegisterFileTypes and EnableShellOpen functions. And the second part is done by a OnDDECommand virtual function.
I personally haven't worked, but this is the way to do what you are looking for. Search for DDE