Jump to content
  • Advertisement
Sign in to follow this  
TheBluMage

Really, really weird file dialog problem

This topic is 4827 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Okay, I don't program in C++ too often, but I thought I knew enough to get something this simple done. I want to open a file dialog and output the file path as a C++ standard string. I wrote the function using regular char buffers at first to make sure I was doing it right and all went fine. Now, I'm trying to switch from accepting a buffer pointer/size to accepting a string reference to write to and the function is acting very strangely. Here, the dialog will pop up the first time I call the function, but not the second:
bool ShowFileDialog(string& path2) {
	
	OPENFILENAME ofn;
	memset(&ofn, 0, sizeof(ofn));
	
	char title[40];
	char filter[] = "MP3 Files (*.mp3)\0*.mp3\0\0";
	char path[MAX_PATH];
	
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hInstance = GetModuleHandle(0);
	ofn.lpstrFilter = filter;
	ofn.lpstrFile = path;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrFileTitle = title;
	ofn.nMaxFileTitle = 40;
	ofn.Flags = OFN_FILEMUSTEXIST;
	ofn.lpstrDefExt = "mp3\0\0";
	
	if(GetOpenFileName(&ofn) != 0) {
		//Do nothing but return true for success
		return true;
	}
	
	return false;
	
}
Alright, that function doesn't actually output the path, but it will show the dialog (only one time, strangely enough). Now, I add one line of code after the GetOpenFileName call, and the dialog won't come up at all:
bool ShowFileDialog(string& path2) {
	
	OPENFILENAME ofn;
	memset(&ofn, 0, sizeof(ofn));
	
	char title[40];
	char filter[] = "MP3 Files (*.mp3)\0*.mp3\0\0";
	char path[MAX_PATH];
	
	ofn.lStructSize = sizeof(OPENFILENAME);
	ofn.hInstance = GetModuleHandle(0);
	ofn.lpstrFilter = filter;
	ofn.lpstrFile = path;
	ofn.nMaxFile = MAX_PATH;
	ofn.lpstrFileTitle = title;
	ofn.nMaxFileTitle = 40;
	ofn.Flags = OFN_FILEMUSTEXIST;
	ofn.lpstrDefExt = "mp3\0\0";
	
	if(GetOpenFileName(&ofn) != 0) {
		/*Still do nothing but return success, but if I add one more line of code to create
		a new string, the dialog never opens!*/
		string temp = "";  //Apparently, this line is the killer
		return true;
	}
	
	return false;
	
}
I thought this would be simple... Any help is very much appreciated.

Share this post


Link to post
Share on other sites
Advertisement
The data in the buffer pointed to by ofn.lpstrFile is used to initialize the dialog, and you are leaving it indeterminate before the call.

From the MSDN:
Quote:
lpstrFile
Pointer to a buffer that contains a file name used to initialize the File Name edit control. The first character of this buffer must be NULL if initialization is not necessary. When the GetOpenFileName or GetSaveFileName function returns successfully, this buffer contains the drive designator, path, file name, and extension of the selected file.


Instead of

char path[MAX_PATH];

use

char path[MAX_PATH] = {0};

Share this post


Link to post
Share on other sites
Thank you! After using C# and Java for so long I keep forgetting about the stack content being undefined. I'm eternally grateful!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!