I'm trying to create a function that gets a filepath from the OPENFILENAME dialog. My code looks something like this.
wstring src;
bool open()
{
const string title = "Select a File";
wchar_t filename[MAX_PATH];
OPENFILENAMEA ofn;
ZeroMemory(&filename, sizeof(filename));
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFilter = "Music (.mp3)\0*.mp3\0All\0*.*\0";
ofn.lpstrFile = LPSTR(filename);
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = title.c_str();
ofn.Flags = OFN_DONTADDTORECENT | OFN_FILEMUSTEXIST;
if (GetOpenFileNameA(&ofn))
{
src = filename; //<----------Save filepath in global variable
return true;
}
return false;
}
Upon placing a breakpoint at the commented row, I could inspect the value of 'src' as well as 'filename', which at this point were, to me, unidentifiable letters of asian origin. Why does this happen? Is this a conversion issue?
EDIT:
Thanks to a quick reply and a few comments, the code is now fully functional. Thanks Hans Passant for a very direct solution, and also huge thanks to Cody Gray for rewriting the function, explaining the mistake, as well as giving me a lesson in how it should be handled. As I am still taking my first steps to learn the winapi, this information will serve me well in future programs.