Strange 'open file dialog' problem (C++, VS)
Posted: Fri Jan 20, 2012 10:06 am
Aw. I hate making new topics every time I stumble upon a new problem that I simply can't figure out myself.
We really should have a 'Quick questions' topic in here, to avoid all of the 'I have a small problem' topics.
Anyway, I have come across a problem that really kills my Map Editor. And the root of the problem is the Windows API's 'open file dialog' function.
I use the file dialog to get the path of the tilesheet I want to add. After that, I use the returned string to load that tilesheet into the program.
But when I do certain actions after using this file dialog, I get a crash. It doesn't matter what I use the returned string for; even when opening the file dialog but do nothing about the returned string, it will still crash (only when I select a file, and not clicking 'cancel').
This is the function I have created.
Have in mind that I am NOT familiar with the Windows API, so this is mostly copy-and-paste.
Here is the result of the crash:
A little background information
I have a small scripting system in my editor. I click 'Save script', and the new script is stored into the system. This works perfectly fine.
I also have a Tilesheet system, where users can click 'Add tilesheet', and the tilesheet is stored and can be used to draw the map.
The way I get the desired tilesheet into the system, is to open a file dialog, so I can browse to the tilesheet. This file dialog returns the name of the file as a string. Then, I use this string to point to the path where I should load up the new tilesheet from.
But after I have added a tilesheet (with the help of the file dialog), or simply just open a file dialog, selecting a file and not doing anything with the returned string, and then adding a new script, the program crashes.
The same happens when I try to pop up some text to the screen, after having the file dialog open.
We really should have a 'Quick questions' topic in here, to avoid all of the 'I have a small problem' topics.
Anyway, I have come across a problem that really kills my Map Editor. And the root of the problem is the Windows API's 'open file dialog' function.
I use the file dialog to get the path of the tilesheet I want to add. After that, I use the returned string to load that tilesheet into the program.
But when I do certain actions after using this file dialog, I get a crash. It doesn't matter what I use the returned string for; even when opening the file dialog but do nothing about the returned string, it will still crash (only when I select a file, and not clicking 'cancel').
This is the function I have created.
Have in mind that I am NOT familiar with the Windows API, so this is mostly copy-and-paste.
Code: Select all
// Declaration (window.h)
static string getFileName(char *filter = "PNG files (*.png)\0*.png\0\0", HWND owner = NULL);
// Defenition (window.cpp)
string Window::getFileName(char *filter, HWND owner) {
OPENFILENAME ofn;
char fileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = owner;
ofn.lpstrFilter = filter;
ofn.lpstrFile = fileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "";
string fileNameStr;
if (GetOpenFileName(&ofn)) {
fileNameStr = fileName;
}
return fileNameStr;
}
// In use
string str;
str = Window::getFileName();
A little background information
I have a small scripting system in my editor. I click 'Save script', and the new script is stored into the system. This works perfectly fine.
I also have a Tilesheet system, where users can click 'Add tilesheet', and the tilesheet is stored and can be used to draw the map.
The way I get the desired tilesheet into the system, is to open a file dialog, so I can browse to the tilesheet. This file dialog returns the name of the file as a string. Then, I use this string to point to the path where I should load up the new tilesheet from.
But after I have added a tilesheet (with the help of the file dialog), or simply just open a file dialog, selecting a file and not doing anything with the returned string, and then adding a new script, the program crashes.
The same happens when I try to pop up some text to the screen, after having the file dialog open.