Problems with CFileDialog instantiation

巧了我就是萌 提交于 2019-12-01 06:39:22

The issue seems to be that you're using the incorrect string type.

The quick solution is to use TCHAR and not char. The better solution is to just use wide strings and make sure the build is Unicode.

When you create a project in Visual Studio, the default character set type that is used is Unicode, not MBCS and not "Not Set". This means that Windows API and MFC functions that take character arrays and pointers will be using wide characters. Therefore using char, char *, const char*, on Windows API functions that expect wide strings will not compile.

The indication that your code is wrong, even if you knew nothing about Unicode or MBCS, is that the functions you're calling take types of LPCTSTR -- that is not a const char *, it is what it is, namely a constant pointer to a TCHAR. If you stuck with knowing to use the types specified, you would have been good to go.

So the lesson is that if a function wants a type, provide a variable or expression of that type, not what you think the type is equivalent to.

Ok. So I've changed my code to the following:

CFile theFile;
TCHAR strFilter[] = { _T("TXT Files (*.txt)|*.txt|All Files (*.*)|*.*||") };
CFileDialog fDlg = CFileDialog(TRUE, _T(".txt"), NULL, 0, strFilter);

And, there are now no problems. Thank you for your responses!

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!