We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now


Derived CFileDialog

dkremer asked
Medium Priority
Last Modified: 2013-11-19
How can I create a custom Explorer (OFN_EXPLORER) custom File Open dialog with my controls on it ?
I copied the dialog template from the comdlg32.dll and added my controls. I created a new class derived from CFileDialog associated member variables with the newly created controls, in the constructor I once tried adding OFN_ENABLETEMPLATE but no go, and another  time I changed the m_lpTemplateName member of the m_ofn member and still nothing...

Any ideas ?
Watch Question

Can you give the exact steps you followed for subclassing the CFileDialog?
If the steps you have followed are just as given above, probably you have missed adding the name of the resource template in the resource.h since it doesn't happen automatically.
You can add following line in resource.h to give an id to the file dialog template.
#define IDD_NEWFILEOPEN  150 /// or any unique number
also add following line in your class definition, public section:
Hope this solves your problem.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts


The ID is already defined in Resource.H as I copied the dialog template in the ComDlg32.DLL using the Visual Studio so when I pasted it it added the ID, and I manually added the IDD=IDD_MY_DIALOG to the derived class, still it doesn't show (I traced until it calls the API function which supposed to open the dialog and nothing appears). It (DoModal) returns with an ID of 2.
Please help !!! ;-)

      - Dror

I am not able to figure out what is wrong with these details. One thing I suspect is that you are calling the CFileDialog DoModal() from a DLL. This is the problem I was facing in my application since proper resource handle switching needs to be done in DLL ( an extended DLL).
Another check you can do is to see whether the resource ID is unique. But usually this problem ends up with some assertion.
Please give me a more wide picture of how you have implemented the dialog.


Yeah, that's right, I'm calling DoModal from inside an ActiveX control I'm writing. I'm trying to write a custom file open dialog with a preview "pane" on the side allowing Visual Basic to use it as if they were using the Microsoft Common Dialogs control.
Why doesn't it work with DLLs ?

      - Dror.

 Then that might be the real problem.
Go to microsoft site knowledge base area and query about resources in extension DLL. Article ID is Q150121 and title is "PRB : MFC loads wrong resources in Extension DLL".
All you have to do is switch the resource handle before calling DoModal() to the handle of your DLL and then after DoModal() reset it.
This worked well in my application. Similar should work in your case as well.


I hope this is really the problem, mind sending me a sample since I'm haveing implementing the information from the KB and the DLLHUSK sample as I can't use DllMain - MFC already supplies a function for OCX controls and don't know where to initialize the AFX_EXTENSION_MODULE variable. I'm not I sure this is the problem, 'cuz, in applications that use extention DLLs this error should occur when a function in the DLL calls AfxGetInstanceHandle() it would return the applications handle and not the extention DLL handle, where as in OCXs each OCX has a COleControlModule (which derives CWinApp) and it "replaces" the application object in a standard application. I use the resources alot in the OCX and never had a problem and had to change it by calling AfxSetResourceHandle(). But hey, I'm willing to try out anything now ;-)
I've read stuff about problems with subclassing Explorer style file dialogs in microsoft.public.vc.mfc and someone said you couldn't replace the template, you can add your own as a child (???) and if you want to do stuff on the already existing controls you have to do it at run-time. He also posted an example but it was too old and was deleted from the server (I tried www.dejanews.com but they don't carry the attachments ;-( ).
Anyway, thanks,
    - Dror

This is the way I implemented CFileDialog in my application ( an extended DLL)

 HINSTANCE hInstOld = AfxGetResourceHandle();
 CMyFileDialog dlg(TRUE, lpszDeftext, lpszFileName, dwFlags,  szFilter,this); // with properly initialized parameters

Hope this works in your application as well.


As I said in the previous comment, I can't initialize the AFX_EXTENSION_MODULE in DllMain since MFC has a special DllMain for OCX Dlls. Again, as I said in the previous comment, there shouldn't be any need to use is since OCXs __HAVE__ CWinApp derived as an application object and thus have an hInstance and don't need to use the extra functions required for an extention DLL.

       - Dror

I don't know how to solve the problem in OCX since I haven't worked on OCXs. You can check whether the constructor of the CFileDialog is getting proper resource handle by debugging the application.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.