Reputation: 1300
Normally, when you use CFileDialog to open a file multiple times, current directory is maintained. I.e., each time the dialog opens at the directory where previous dialog ended (if it was not cancelled). And this works even if you use different instances of CFileDialog from different places.
And even after program is restarted, the previous directory is maintained.
But it looks like in my app this doesn't work across DLL: CFileDialogs called from main .exe program have their current dir and CFileDialogs from .dll have another one. They don't interact with each other, but otherwise behave "normally".
So I end up with two inconsistent current directories, and you have to navigate to required location again from DLL. And sometimes you forget to change dir and you load some data in .exe from one dir and some other data in .dll from another dir and gets weird results.
Do you know a method to have the same current dir when opening CFileDialog from .exe and .dll?
Clarification: I don't want the dialogs to always start from the same dir, I want them to retain last used dir, but do it consistently in .exe and .dll.
My .exe is created in VS2003, .dll in VS2010.
(My initial guess was that you need to provide the main window handle in CFileDialog constructor in DLL, but this didn't help. My next guess is to manually get current directory via, well, GetCurrentDirectory() and set it as a starting location for dll's CFileDialog (and then set current dir to where it ended). But this looks quite ugly and I'm not sure it will work in all OSes from WinXP to Win8, because they have different rules for default directory of CFileDialog according to MSDN.)
Upvotes: 1
Views: 890
Reputation: 1814
You have to use the CFileDialog as given below:
CFileDialog FileDlg( TRUE, NULL, NULL, OFN_NOCHANGEDIR, _T( "All Files (*.*)|*.*||" ));
The flag OFN_NOCHANGEDIR indicates to retain the previous path on opening file dialog. The filter is just an example. You have to use your own filters for file extensions.
If you have to get the same location in both exe and dll use the same file dialog object in both.
Upvotes: 2
Reputation: 22922
You need to set the lpstrInitialDir of the CFileDialog::m_ofn member member structure. My experience is that the default folder used also varies between target operating system and explorer settings.
Upvotes: 1