The worlds most elusive bug : Something to do with CFileDialog

Not an exception of any kind.
Nor any memory leak of any type.
Or is it any obvious bug.

More hideous than The Hag.
More evil than Satan.
More elusive than the Loch Ness Monster.
More invisible than the Yeti.

The thing.....is....??!!!!

Trust me : this bug(if it's a bug) is darn strange.
Explanation is quite tough and will make the whole thing more confusing, so I've attached the project(darn simple) to this thread. I really hope you can download the stuff and have a look.

When you run the program, do the following :

1) Without doing anything else, click the Find File button.
2) The program searches for WAV files in the given Sounds folder(included with the project) and list it in the extended combo box.
The program even outputs "T" to indicate success.
3) Wonderful. Perfect. The Lord! :D

Now, close the program and run it again.

1) Click the Browse button. Navigate to the Sounds folder(included within the project directory) and select the WAV file named "dogbark.wav".
2) Click OK on this File Open dialog.
3) The file path of the selected WAV file should appear in the edit box.
4) Click the Find File button.
5) The program searches for WAV files in the given Sounds folder(included with the project) and list it in the extended combo box.
But now, the program outputs "F", meaning the file can't be found.(even though you didn't move the file whatsoever)
And nothing gets listed in the combo box.
6) ****. Sucky. Total despair. Agony. Helplessness.

This is the bug. Notice that when you didn't click the Browse button, everything is perfect. When you click it, everything crumbles.
And it seems that this bug has something to do with the declaration of the CFileDialog object and it's extension-related parameter in the constructor. I've indicated this within the project.

If this isn't the world's most elusive bug, what is it? The world's cutest bug? Of course not! No bug is cute, k?!!!
And I hope it's not a bug within the accursed CFileDialog, cos' if it is......gggrrrr.......

Hope you can help and thanks a lot! :):):)
Xeon.
[2273 byte] By [Xeon] at [2007-11-17 22:40:12]
# 1 Re: The worlds most elusive bug : Something to do with CFileDialog
Hey wheer is the attachment?
Brandon Parker at 2007-11-10 8:41:49 >
# 2 Re: The worlds most elusive bug : Something to do with CFileDialog
Darn! Must be fooling around too much lately. Here it is, Brandon!
Thanks a lot! :)
Xeon at 2007-11-10 8:42:49 >
# 3 Re: The worlds most elusive bug : Something to do with CFileDialog
Something is wrong with it. I cant open it...says file is corrupt
Brandon Parker at 2007-11-10 8:43:47 >
# 4 Re: The worlds most elusive bug : Something to do with CFileDialog
Darn! That's strange! I downloaded the file and it's ok!
Anyway, here's the code :

//function for searching files of a given extension(strFileExt) and listing them in extended combo box
//nImage is the index of the graphic in the imagelist
void ListSoundFiles(CComboBoxEx& comboBox, CString strFolderName, CString strFileExt, int nImage)
{
//get sound files from Sounds folder and list 'em in sounds
//combo box
CFileFind fileFind;
CStringArray arFileTitles;
CString strCurrentFileExt = "";
strFileExt.MakeUpper();

COMBOBOXEXITEM cbItem;
ZeroMemory(&cbItem,sizeof(COMBOBOXEXITEM));
cbItem.mask = CBEIF_IMAGE | CBEIF_TEXT | CBEIF_SELECTEDIMAGE;

BOOL bFound = fileFind.FindFile(strFolderName + "\\*.*");
AfxMessageBox(bFound? "T" : "F");
while(bFound)
{
bFound = fileFind.FindNextFile();

strCurrentFileExt = fileFind.GetFileName().Right(3);
strCurrentFileExt.MakeUpper();
if(strCurrentFileExt == strFileExt)
{
arFileTitles.Add(fileFind.GetFileTitle());
}
}
fileFind.Close();

std::sort(arFileTitles.GetData(),arFileTitles.GetData() + arFileTitles.GetSize());

for(int i = 0; i < arFileTitles.GetSize(); i++)
{
cbItem.iItem = i;
cbItem.iImage = cbItem.iSelectedImage = nImage;
cbItem.pszText = (LPTSTR)(LPCTSTR)arFileTitles.GetAt(i);
cbItem.cchTextMax = 256;
comboBox.InsertItem(&cbItem);
}

comboBox.SetCurSel(0);
}

And here's the code that's called when the FileFind push-button is clicked :

void CFileFinderDlg::OnFindfile()
{
//m_ilImages is the member variable image list object
m_ilImages.Add(AfxGetApp()->LoadIcon(IDR_MAINFRAME));
//m_Combo is a Control variable mapped to an extended combo box
m_Combo.SetImageList(&m_ilImages);

ListSoundFiles(m_Combo,"Sounds","WAV",0);
}

//And there's the code that's called when the Browse button is clicked:

void CFileFinderDlg::OnBrowse()
{
CFileDialog dlg(TRUE,NULL,NULL,OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST,
//********BUG seems to be caused by the line below*******
//if you change the extension filter of the File Open dialog
//to All files (*.*)|*.* instead, everything works fine!
"WAV files (*.wav)|*.wav", this);

if(dlg.DoModal() == IDOK)
{
//m_strSoundPath is a Value member variable mapped to the exit box
m_strSoundPath = dlg.GetPathName();

CEdit* pEdit = (CEdit*)GetDlgItem(IDC_EDIT1);
pEdit->SetWindowText(m_strSoundPath);
}
}

If you copy and paste all these into a new MFC dialog-based program and follow the steps, you should see the bug as listed.
Xeon at 2007-11-10 8:44:49 >
# 5 Re: The worlds most elusive bug : Something to do with CFileDialog
Ok the problem is when you use the CFileDialog and select a file you change your current directory. So on the second findfile call your current directory is already the Sounds directory. And since you dont have a folder called FileFinder/Sounds/Sounds you get a ERROR_PATH_NOT_FOUND error. So what you need to do is after calling the CFileDialog, reset the current directory to FileFinder directory.

Regards
Brandon Parker
Brandon Parker at 2007-11-10 8:45:49 >
# 6 Re: The worlds most elusive bug : Something to do with CFileDialog
For a start, the most elusive bug in the world can only be solved by the most talented programmer.

From Monday onwards, I'm gonna eat Win32 code and deep MFC internals for breakfast, lunch and dinner.
Xeon at 2007-11-10 8:46:48 >
# 7 Re: The worlds most elusive bug : Something to do with CFileDialog
LOL;) If thats a Thank You...and I think it maybe. YOUR WELCOME

Brandon Parker
Brandon Parker at 2007-11-10 8:47:48 >
# 8 Re: The worlds most elusive bug : Something to do with CFileDialog
U're the one whom I should thank, Brandon!
Anyway, I found a flag that might help to sove the problem, and I'm pretty sure you know it too :

OFN_NOCHANGEDIR
Restores the current directory to its original value if the user changed the directory while searching for files.
Windows NT 4.0/2000/XP: This flag is ineffective for GetOpenFileName.

As you can see, this flag resets the current directory to it's original value, so it will probably work for my case. The problem is : as listed above, MSDN says this flag is ineffective for NT 4.0, 2000 and XP.

And does that mean that this flag has no effect on these OSes and I gotta code my own code to reset the current directory?

Thanks a lot, Brandon! :):D
Xeon at 2007-11-10 8:48:51 >
# 9 Re: The worlds most elusive bug : Something to do with CFileDialog
Yeah but its easy...

// Save the current directory path
TCHAR szPath[MAX_PATH];
::GetCurrentDirectory(sizeof(szPath) / sizeof(TCHAR),szPath);
CString strPath = szPath;

// Call CFileDialog
if(dlg.DoModal() == IDOK)
{
// Reset the directory
::SetCurrentDirectory(strPath);
}
Brandon Parker at 2007-11-10 8:49:50 >
# 10 Re: The worlds most elusive bug : Something to do with CFileDialog
Let me be banned, but there's some things which must be done, in the name of our Father, God.
I really wanna express my utmost gratitude and gratefulness to the pure and ole~ mighty Brandon Parker.

Thank you, Brandon! :)
Xeon at 2007-11-10 8:50:58 >