I have a C++ code I did on VS2005, it worked just fine and after a while I upgraded the project to VS2013 without issues, everything compiled and worked as expected.
Recently I upgraded one of my computers to VS2019 and tried to check one of my old programs there, the code didn't compile and threw me a bunch of errors related to conversion from const char* to LPCWSTR.
Function Call:
std::string inputFile, cwd;
cwd = getCWD();
flag = openMultipleFileDialog("Select a valid CSV file.", "CSV Files (*.csv)\0*.csv\0\0", cwd, inputFile);
Function definition:
int openMultipleFileDialog(std::string title, char* filter, std::string cwd, std::string& fileName)
{
int ofnFlag;
char buffer[4096];
OPENFILENAME ofn = {0};
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFilter = filter;
ofn.lpstrFile = buffer;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(buffer);
ofn.lpstrTitle = title.c_str();
ofn.lpstrInitialDir = cwd.c_str();
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER;
//ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST | OFN_EXPLORER | OFN_ALLOWMULTISELECT;
ofnFlag = GetOpenFileName(&ofn);
fileName = buffer;
return ofnFlag;
}
I will update the post to add the VS2019 errors
I would like to know why is this happening, maybe something is flying under my radar and the VS upgrade came with a lot more changes than I expected.
Use the ASCII variants OPENFILENAMEA
and GetOpenFileNameA
instead of the non-explicit ones.
Search for information on Unicode support and the UNICODE
macro for more information.
GetOpenFileName is declared as follows:
WINCOMMDLGAPI BOOL APIENTRY GetOpenFileNameA(LPOPENFILENAMEA);
WINCOMMDLGAPI BOOL APIENTRY GetOpenFileNameW(LPOPENFILENAMEW);
#ifdef UNICODE
#define GetOpenFileName GetOpenFileNameW
#else
#define GetOpenFileName GetOpenFileNameA
#endif // !UNICODE
GetOpenFileNameA, which takes a file path of type const char*.
GetOpenFileNameW, which takes a file path of type const wchar_t*.
However, the default Character Set
of VS2019 is Unicode. So, it's calling the w
version of the function. There's no automatic conversion from const wchar_t* to const char*. Therefore, I suggest that you could call GetOpenFileNameA explicitly instead of GetOpenFileName.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.