GetOpenFileName() Is Interfering With SFML

171 Views Asked by At

I create an OPENFILENAME:

OPENFILENAME ofn;   
char szFile[260];      
HWND hwnd = NULL;             

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
ofn.lpstrFile = (LPWSTR)szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"PNG Files\0*.PNG*\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;

std::string input;
if (GetOpenFileName(&ofn))
{
    input = CW2A(ofn.lpstrFile);
    std::cout << input << std::endl;
}
else
    errorHandle("Open Dialog Problem");

But then when I try to import something via SMFL it says "Error: Unable to open file.":

sf::Texture _cursor;
    if (!_cursor.loadFromFile("Resources/Metal_Norm.png"))
        errorHandle("-Cursor Texture Couldn't Load");

Not sure why this error is occurring if anyone has any possible answers I would appreciate it.

2

There are 2 best solutions below

4
On BEST ANSWER

GetOpenFileName changes the current directory as you navigate around in the browser.

There is a flag you can set, OFN_NO­CHANGE­DIR which was supposed to prevent this but I notice the MSDN docs have been updated at some point to say it doesn't work with GetOpenFileName.

You could try that but if it's true it doesn't work, the solution would be to save the current directory (use GetCurrentDirectory) before calling GetOpenFileName and then restore it afterwards using SetCurrentDirectory.

0
On

What I did to fix this problem with the help of Jonathan Potter:

1.) Saved the current directory of the SFML application.

LPCWSTR mainDirectory = GetCurrentD();

2.) Did what I had to do with the GetOpenFileName() function.

Tilemap t(file.GetWindowWidth(), file.GetWindowHeight(), file.GetTileWidth(), file.GetTileHeight(), file.GetScale(), file.GetTop(), OpenFile());

3.) Then restored the directory back to the original starting directory I had saved.

if (!SetCurrentDirectory(mainDirectory))
    errorHandle(L"Didn't Set Directory");