I had this code that uses the legacy API function GetOpenFileName
#include <windows.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
OPENFILENAME ofn;
char szFile[260];
HWND hwnd = NULL;
HANDLE hf;
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = szFile;
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "Txt files (*.txt)\0*.txt\0Task files (*01.*)\0*01.*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);
return 0;
}
The filter name "Task files (*01.*)" is correctly shown in the dialog:
I've rewritten the code to use the FileDialog object as follows:
#include <windows.h>
#include <shobjidl.h>
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, PWSTR pCmdLine, int nCmdShow)
{
HRESULT hr = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED |
COINIT_DISABLE_OLE1DDE);
if (SUCCEEDED(hr))
{
IFileOpenDialog *pFileOpen;
hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_ALL,
IID_IFileOpenDialog, reinterpret_cast<void**>(&pFileOpen));
if (SUCCEEDED(hr))
{
COMDLG_FILTERSPEC fileTypes[] =
{
{L"Txt files (*.txt)", L"*.txt"},
{L"Task files (*01.*)", L"*01.*"},
};
hr = pFileOpen->SetFileTypes(ARRAYSIZE(fileTypes), fileTypes);
if (SUCCEEDED(hr))
{
hr = pFileOpen->Show(NULL);
pFileOpen->Release();
}
CoUninitialize();
}
}
return 0;
}
But now the filter name is garbled, as the pattern is appended to it.
Is it a bug in the FileDialog object or am I doing something wrong?