Handmade Network»Forums
1 posts
Win32 GetOpenFlileName crashes my game
Edited by ProgrammingPunk on

On windows i want to be able to load a level for my game trough the a explorer window. I use the this api: https://learn.microsoft.com/en-us/windows/win32/api/commdlg/nf-commdlg-getopenfilenamea

I'm able to open the explorer, get the filename and load the level but sometimes the programm just crashes with an access violation somewhere in my d3d11 render pipeline out of nowhere after i close the explorer window (even if i don't select a file)...

I really don't understand what is going, so any kind of help or suggestion is welcome. I really would like to give you more information but i really don't have a clue what this is causing.

This is standard code from an example on the official documentation. The only thing i changed is that the hwndOwnder is set to NULL because otherwise the explorer window would not open, maybe that causes problems?

Here is the code for the API:

// Initialize OPENFILENAME

ZeroMemory(&ofn, sizeof(ofn));

ofn.lStructSize = sizeof(ofn);

ofn.hwndOwner = NULL;

ofn.lpstrFile = szFile;

// Set lpstrFile[0] to '\0' so that GetOpenFileName does not
// use the contents of szFile to initialize itself.

ofn.lpstrFile[0] = '\0';

ofn.nMaxFile = sizeof(szFile);

ofn.lpstrFilter = "All\0*.*\0Text\0*.TXT\0";

ofn.nFilterIndex = 1;

ofn.lpstrFileTitle = NULL; ofn.nMaxFileTitle = 0;

ofn.lpstrInitialDir = NULL;

ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST; // Display the Open dialog box.

if (GetOpenFileNameA(&ofn)==TRUE){

   hf = CreateFile(ofn.lpstrFile, 
              GENERIC_READ,
                0,
                            (LPSECURITY_ATTRIBUTES) NULL,
                            OPEN_EXISTING,
                            FILE_ATTRIBUTE_NORMAL,
                            (HANDLE) NULL);

...........file gets loaded

Mārtiņš Možeiko
2559 posts / 2 projects
Win32 GetOpenFlileName crashes my game

It is valid to have NULL owner window. Nothing bad will happen.

OP's problem was solved on Discord - there was some other code related to DirectSound that messed up something with memory and that's why it seemed this unrelated code is the problem.