Opening a Second File from Explorer
-
I'm working in MFC under Visual Studio 2010. I've got my program associated with a file extension and clicking on a file of that type in Windows Explorer will open the program and load that file. All's well so far. Now I need to be able to click on another file of the type from WE and load that file into the same instance of the program already running. What happens now when I click on another file is the focus just switches to the program and nothing loads. When the program isn't running and you click on a file associated with it, the program starts and in the loading of the App instance you check the command line, which is loaded into a CCommandLineInfo structure and the program deals with the command. I can't figure out how to get the second file to load when the program is already running by selecting it in WE (opening a file from within the program has been working fine from the start). I have looked at WM_ACTIVATE and WM_ACTIVATEAPP which are called when the program gets focus while running, but I don't know how you would get the information about the file just selected in Windows Explorer. I'm probably missing some key terminology in my searches. I can't find any information on how to do this. Maybe I'm slow kid in class today... Additionally if I select multiple files in Windows Explorer and open them at the same time, it opens multiple instances of the program instead of opening one instance any loading those files into that one instance. Is there any way to handle that? Thanks
-
I'm working in MFC under Visual Studio 2010. I've got my program associated with a file extension and clicking on a file of that type in Windows Explorer will open the program and load that file. All's well so far. Now I need to be able to click on another file of the type from WE and load that file into the same instance of the program already running. What happens now when I click on another file is the focus just switches to the program and nothing loads. When the program isn't running and you click on a file associated with it, the program starts and in the loading of the App instance you check the command line, which is loaded into a CCommandLineInfo structure and the program deals with the command. I can't figure out how to get the second file to load when the program is already running by selecting it in WE (opening a file from within the program has been working fine from the start). I have looked at WM_ACTIVATE and WM_ACTIVATEAPP which are called when the program gets focus while running, but I don't know how you would get the information about the file just selected in Windows Explorer. I'm probably missing some key terminology in my searches. I can't find any information on how to do this. Maybe I'm slow kid in class today... Additionally if I select multiple files in Windows Explorer and open them at the same time, it opens multiple instances of the program instead of opening one instance any loading those files into that one instance. Is there any way to handle that? Thanks
There are two solutions:
- Implement a COM server for
IDropTaget
. - Use DDE.
The first one is the preferred method. See Verbs and File Associations[^]. The keywords from the above link may be used to search for example code (but I did not found something so far; note to myself: might be worth to write an article about this topic).
- Implement a COM server for
-
There are two solutions:
- Implement a COM server for
IDropTaget
. - Use DDE.
The first one is the preferred method. See Verbs and File Associations[^]. The keywords from the above link may be used to search for example code (but I did not found something so far; note to myself: might be worth to write an article about this topic).
I'm lost how would you use DDE from Windows Explorer and wouldn't something have to be installed in Windows Explorer for that to work. And what if the user is using some other type of file browser? And I thought IDropTarget was only for drag and drop operations. I'm talking about double clicking on a file in Windows Explorer. I know it works in programs like Word. Does that use DDE? That seems kind of odd.
- Implement a COM server for
-
I'm lost how would you use DDE from Windows Explorer and wouldn't something have to be installed in Windows Explorer for that to work. And what if the user is using some other type of file browser? And I thought IDropTarget was only for drag and drop operations. I'm talking about double clicking on a file in Windows Explorer. I know it works in programs like Word. Does that use DDE? That seems kind of odd.
The Explorer will scan the registry searching for the application name associated with the file extension. When found it reads the shell/open keys and checks if there are entries for command, ddeexec, and DropTarget. Then it will use the found method with the highest priority to pass the data (file names) to your application after starting it if necessary. Nothing must be installed in the Explorer. You must only create the registry keys and add code to your application to handle the data (the code for basic command handling is already present). Other file browsers should do it like the Explorer. One reason why
IDropTarget
was choosen is explained in the link:Quote:
This method leverages an application's existing drop target implementation (if one exists), making it easy for applications that already support drag-and-drop through an IDropTarget implementation on their top level window to map that functionality into a verb that ise presented in the explorer.
I don't think Word is still using DDE (while old versions probably used it). It will probably use
IDropTarget
. -
The Explorer will scan the registry searching for the application name associated with the file extension. When found it reads the shell/open keys and checks if there are entries for command, ddeexec, and DropTarget. Then it will use the found method with the highest priority to pass the data (file names) to your application after starting it if necessary. Nothing must be installed in the Explorer. You must only create the registry keys and add code to your application to handle the data (the code for basic command handling is already present). Other file browsers should do it like the Explorer. One reason why
IDropTarget
was choosen is explained in the link:Quote:
This method leverages an application's existing drop target implementation (if one exists), making it easy for applications that already support drag-and-drop through an IDropTarget implementation on their top level window to map that functionality into a verb that ise presented in the explorer.
I don't think Word is still using DDE (while old versions probably used it). It will probably use
IDropTarget
. -
The Explorer will scan the registry searching for the application name associated with the file extension. When found it reads the shell/open keys and checks if there are entries for command, ddeexec, and DropTarget. Then it will use the found method with the highest priority to pass the data (file names) to your application after starting it if necessary. Nothing must be installed in the Explorer. You must only create the registry keys and add code to your application to handle the data (the code for basic command handling is already present). Other file browsers should do it like the Explorer. One reason why
IDropTarget
was choosen is explained in the link:Quote:
This method leverages an application's existing drop target implementation (if one exists), making it easy for applications that already support drag-and-drop through an IDropTarget implementation on their top level window to map that functionality into a verb that ise presented in the explorer.
I don't think Word is still using DDE (while old versions probably used it). It will probably use
IDropTarget
. -
I'll look at IDropTarget next week. My customer found a bug this morning that is taking priority. This will be back on the front burner next week. Thanks
It took me a few days to get back to this, then I ran into a few dead ends before hitting on the solution to get IDropTarget to work for my application. In the end Michael Dunn's article from here on CodeProject: was the best way forward. However once I got IDropTarget working, I ended up discovering my original problem had to do with locking my app down to one instance. A couple of years ago when this project was still in its infancy I used PJ Naughter's CSingleInstance class () to check and see if there was an instance of the program already running and if there was, switch to that instance. A second instance would start when I double clicked on a file associated with the program when an instance was already running. When the second instance is detected, it sends a message to the original instance to take focus and quits. PJ Naughter conveniently created a means to send the command line from the second instance to the first before quitting, so I took advantage of that and now the first instance loads the file correctly on double click. Implementing IDropTarget in the end turned out to not be necessary, but it will be a nice feature to allow users to drag and drop files into the program. It also expanded my understanding of COM, which I may need to understand better down the road. I need to do a lot of communication between programs as part of this project and I will need to do a lot more inter-program communication down the road. Just thought I would post an update for future reference if someone comes across a similar problem in the future and finds this thread. Thanks for the help!