Question

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.

Was it helpful?

Solution 3

I finally isolated the only difference between the builds:

I was using Visual Leak Detector in my debug build. After removing it, it started to work.

I'll try to get to the bottom why it happens, but I just wanted to thank every one trying to help me. I wish I could reward more points.

Some more people having similar problems:

OTHER TIPS

1 ofn.lpstrFilter shall be terminated by two zeros: \0\0.

2 Are you sure ZeroMemory() really zeros out ofn's content. You might try SecureZeroMemory() instead.

Referring 2 a note from msdn:

Use this [SecureZeroMemory()] function instead of ZeroMemory when you want to ensure that your data will be overwritten promptly, as some C++ compilers can optimize a call to ZeroMemory by removing it entirely.

I'd simply use memset() .

A few things...

1) You are initializing lpstrFile twice.

ofn.lpstrFile = szFile;
ofn.lpstrFile = TEXT('\0');

2) You should initialize szFile (probably your intention in #1):

TCHAR szFile[MAX_PATH];
szFile[0] = '\0';

3) nMaxFile should be the maximum number of CHARACTERS, not bytes. So, you want either MAX_PATH or sizeof(szFile) / sizeof(TCHAR).

4) You are missing a '*' in your file filter: should be *.jpg

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top