The root problem in these lines is:
char szFileName[MAX_PATH] = ""; ... ofn.lpstrFile = (LPWSTR)szFileName; ofn.nMaxFile = MAX_PATH;
This creates the MAX_PATH character buffer, but it tells the GetSaveFileName function that it is the MAX_PATH character buffer. This could probably lead to a crash (or quiet memory loss) when someone selects a long path name.
Brushstroke is the cast. Do not lie to the compiler or libraries. They do not like it, and in the end they will always take revenge. Replace these lines as follows:
WCHAR szFileName[MAX_PATH] = L""; ... ofn.lpstrFile = szFileName; // no cast needed ofn.nMaxFile = MAX_PATH;
Now the selected file name will be returned as a string with wide characters. Tony Lion's answer is correct in that you need to use wprintf instead of printf to print wide character strings:
wprintf(L"the path is : %s\n", ofn.lpstrFile);
If you need a string in 8-bit characters instead of wide characters, you can use WideCharToMultiByte. But I would just stick with the broad character APIs in general.
Never give up if you do not know exactly what he is doing and why it is necessary in your particular case.
Adrian mccarthy
source share