2nd call to GetOpenFileName crashes without error on Win 8.1 64 bit machine


A personal project I'm working on is using tinyfiledialogs to help with some cross-platform gui stuff; this works great for the Unix systems I've tested on, however I've run up against a problem when testing on my Windows 8.1 64 bit machine.

The GetOpenFileName() function, when called from tinyfiledialogs.c, or from the below code, will execute correctly on the first call but crash shortly after the dialog window is presented on the second. However this only appears to be happening on my 64 bit machine - running the below code (or my code that uses tinyfiledialogs.c) on a Windows 7 32 bit machine works without issue.

#include <stdio.h>
#include <windows.h>

void openD(){

char Filestring[1024] = "\0";

ofn.lStructSize = sizeof(OPENFILENAME);
ofn.lpstrFile = Filestring;
ofn.nMaxFile = 1024;

int retval = GetOpenFileName(&ofn);
if(retval == 1) printf("True!\n");
else {
    printf("value: %d\n", retval);
    int err_val = CommDlgExtendedError();
        case CDERR_DIALOGFAILURE:   printf("ERR: CDERR_DIALOGFAILURE\n");   break;
        case CDERR_FINDRESFAILURE:  printf("CDERR_FINDRESFAILURE\n");       break;
        case CDERR_INITIALIZATION:  printf("CDERR_INITIALIZATION\n");       break;
        case CDERR_LOADRESFAILURE:  printf("CDERR_LOADRESFAILURE\n");       break;
        case CDERR_LOADSTRFAILURE:  printf("CDERR_LOADSTRFAILURE\n");       break;
        case CDERR_LOCKRESFAILURE:  printf("CDERR_LOCKRESFAILURE\n");       break;
        case CDERR_MEMALLOCFAILURE: printf("CDERR_MEMALLOCFAILURE\n");      break;
        case CDERR_MEMLOCKFAILURE:  printf("CDERR_MEMLOCKFAILURE\n");       break;
        case CDERR_NOHINSTANCE:     printf("CDERR_NOHINSTANCE\n");          break;
        case CDERR_NOHOOK:          printf("CDERR_NOHOOK\n");               break;
        case CDERR_NOTEMPLATE:      printf("CDERR_NOTEMPLATE\n");           break;
        case CDERR_STRUCTSIZE:      printf("CDERR_STRUCTSIZE\n");           break;
        case FNERR_BUFFERTOOSMALL:  printf("FNERR_BUFFERTOOSMALL\n");       break;
        case FNERR_INVALIDFILENAME: printf("FNERR_INVALIDFILENAME\n");      break;
        case FNERR_SUBCLASSFAILURE: printf("FNERR_SUBCLASSFAILURE\n");      break;

int main()

The first call returns TRUE, the second fails to return and the program crashes. Attaching to a running instantiation of the program with the VS Express 2012 Debugger catches the following exceptions when the second call is made; first a popup:

Unhandled exception at 0x759C36DC (shell32.dll) in ofd_64.exe: 0xC0000005: Access violation reading location 0x70017AD8.

Followed in Output by:

First-chance exception at 0x759C36DC (shell32.dll) in ofd_64.exe: 0xC0000005: Access violation reading location 0x70017AD8. Unhandled exception at 0x759C36DC (shell32.dll) in ofd_64.exe: 0xC0000005: Access violation reading location 0x70017AD8.

Over multiple trials on the Windows 8.1 machine the exceptions have remained constant.

Curiously, if I remove the OFN_EXPLORER flag the process executes normally for both calls. This isn't a good solution though. Any help would be appreciated.


The call stack from VS Express 2012 shows:

shell32.dll!00007ffe6ef02777()  Unknown
shell32.dll!00007ffe6ebbf1c4()  Unknown
shell32.dll!00007ffe6ea5b5f3()  Unknown
shell32.dll!00007ffe6ea5b555()  Unknown
shell32.dll!00007ffe6ea5b493()  Unknown
shell32.dll!00007ffe6ea5b386()  Unknown
shell32.dll!00007ffe6eb07f02()  Unknown
shell32.dll!00007ffe6eb86d78()  Unknown
shell32.dll!00007ffe6e93bf9b()  Unknown
ExplorerFrame.dll!00007ffe5b912b30()    Unknown
ExplorerFrame.dll!00007ffe5b912abd()    Unknown
ExplorerFrame.dll!00007ffe5b8ff511()    Unknown
shell32.dll!00007ffe6e8043ed()  Unknown
shell32.dll!00007ffe6e813e1f()  Unknown
SHCore.dll!00007ffe6c30142f()   Unknown
ntdll.dll!00007ffe71bab247()    Unknown
ntdll.dll!00007ffe71bc8e15()    Unknown
kernel32.dll!00007ffe706b13d2() Unknown
ntdll.dll!00007ffe71ba54e4()    Unknown
asked on Stack Overflow Feb 12, 2016 by JawKnee • edited Feb 12, 2016 by JawKnee

2 Answers


As suggested to me elsewhere, I tried calling CoInitializeEx prior to the openD function calls - this has solved the problem, though I can't say why. It doesn't seem to matter whether I use the COINIT_APARTMENTTHREADED or COINIT_MULTITHREADED flag, either works. Perhaps it's just some oddity in my particular machine.

answered on Stack Overflow Feb 12, 2016 by JawKnee

Its a problem of the Stack Size. Do not overwrite default Stack Size in VC++ 32 Bit Properties -> Linker -> System -> Stack Reserved Size. Let it empty. So the problem was solved on 64 Bit systems.

answered on Stack Overflow Apr 26, 2020 by Matthias Wiede

User contributions licensed under CC BY-SA 3.0