tags:

views:

239

answers:

3

I use GetOpenFilename() to let the user select a file. Here is the code:

  wchar_t buffer[MAX_PATH] = { 0 };

  OPENFILENAMEW open_filename = { sizeof (OPENFILENAMEW) };

  open_filename.hwndOwner   = handle_;
  open_filename.lpstrFilter = L"Video Files\0*.avi;*.mpg;*.wmv;*.asf\0"
                              L"All Files\0*.*\0";
  open_filename.lpstrFile   = buffer;
  open_filename.nMaxFile    = MAX_PATH; 
  open_filename.lpstrTitle  = L"Open media file...";
  open_filename.Flags       = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;

  ::GetOpenFileNameW(&open_filename);

The file dialog shows up, but when I

  • change the Filter or
  • click on "My Computer"

the file list turns empty. Pressing [F5] does not help, but if I switch to the parent folder and return to the original folder (in the case of the Filter change) the filtering works fine and files show up in the list.

EDIT: My system is Windows XP (SP3) 32-bit - nothing special. It happens on other machines - with the same config - as well.

A: 

One thing you haven't done that might be causing problems is fully initialize the OPENFILENAMEW structure, especially the lStructSize element. I've seen this causing strange effects before. I'd suggest having something like

  OPENFILENAMEW open_filename = { sizeof (OPENFILENAMEW) };
  ZeroMemory(&open_filename, sizeof (OPENFILENAMEW));
  open_filename.lStructSize = sizeof (OPENFILENAMEW);
DavidK
OPENFILENAMEW open_filename = { sizeof (OPENFILENAMEW) }; does exactly what you proposed.
Andrey
Sigh, my brain doesn't seem to be functioning well today...
DavidK
A: 

Okay, I have figured out the problem, or at least, I have a solution that is working for me.

Earlier in the code, I had the following call to initialize COM...

::CoInitializeEx(NULL, COINIT_MULTITHREADED);

Well, changing this to...

::CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);

...solves the problem for me! Now the file dialog is filtering again.

I searched the web for this and it seems that a very few people faces the same problem, but no one published the aforementioned solution. Can anyone verify my findings?

beef2k
A: 

Thank you, beef2k. It works. But my problem has a little difference. Everything worked fine until I added the SHBrowseForFolder call. I had got the same effect since that moment. But adding CoInitializeEx(NULL, COINIT_APARTMENTTHREADED); solved the problem.

nikitozz