When I compile my program in debug mode GetOpenFileName(&ofn)
never returns.
It works perfectly fine in release mode. The only difference I can spot is that a lot of things are being optimized away in release mode.
OPENFILENAME ofn;
TCHAR szFile[MAX_PATH];
szFile[0] = '\0';
szFile[1] = '\0';
//Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = NULL;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = TEXT("Images (*.jpg;*.png;*.bmp;*.tga;*.psd)\0*.jpg;*.png;*.bmp;*.tga;*.psd\0\0");
ofn.lpstrInitialDir = TEXT(".");
ofn.lpstrTitle = TEXT("Open 512x512 image");
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
...
GetOpenFileName(&ofn)
I'm compiling using Unicode character set.
If I remove \0
from the middle of ofn.lpstrFilter
it works in debug mode, but obviously the filter doesn't function correctly.
This is how the string looks like in the memory in both debug and release mode around the \0
spot:
...snip...
0x00364BB4 70 00 p.
0x00364BB6 73 00 s.
0x00364BB8 64 00 d.
0x00364BBA 29 00 ).
0x00364BBC 00 00 ..
0x00364BBE 2e 00 ..
0x00364BC0 6a 00 j.
0x00364BC2 70 00 p.
0x00364BC4 67 00 g.
0x00364BC6 3b 00 ;.
...snip...
I'm probably doing something silly (I don't have much winapi /w Unicode experience), but I can't figure out what. Any ideas?
EDIT: Updated with current code.
Basically
ofn.lpstrFilter = TEXT("Images (*.jpg;*.png;*.bmp;*.tga;*.psd)*.jpg;*.png;*.bmp;*.tga;*.psd\0");
^
works, but
ofn.lpstrFilter = TEXT("Images (*.jpg;*.png;*.bmp;*.tga;*.psd)\0*.jpg;*.png;*.bmp;*.tga;*.psd\0");
^
doesn't.
EDIT: Tried reproducing same error in fresh Visual Studio project and I couldn't. It works. Did a diff on project settings and there aren't any mayor differences.