Jump to content
  • Advertisement
Sign in to follow this  
Raeldor

Win32 GetOpenFileName not displaying dialog

This topic is 4723 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

Hi All, I am trying to get the Win32 GetOpenFileName call to work, but it doesn't bring up a dialog. I have worked through and my structure seems ok to me... anyone know what might be going on? I am running on Windows XP. Thanks
			// get filename to open
			char fullFilename[255], filenameOnly[255];
			OPENFILENAME openName;
			ZeroMemory(&openName, sizeof(OPENFILENAME));
			openName.lStructSize=sizeof(OPENFILENAME);
			openName.hwndOwner=g_hWnd;
			openName.lpstrFilter="*.xml";
			openName.lpstrFile=(LPSTR)&fullFilename;
			openName.nMaxFile=255;
			openName.lpstrFileTitle=(LPSTR)&filenameOnly;
			openName.nMaxFileTitle=255;
			openName.lpstrFileTitle="Open Terrain";
			openName.lpstrDefExt="xml";
			openName.Flags=OFN_FILEMUSTEXIST|OFN_PATHMUSTEXIST;
			GetOpenFileName((OPENFILENAME*)&openName);

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
Try to set the "openName.hwndOwner" to 0 and set the hInstance.

Biscuit.

Share this post


Link to post
Share on other sites
Also try initialising your character arrays - the lpstrFile parameter is used to initialise the dialog box so it might cause a problem if these arrays are not null'd to start with.

I seem to remember having similar problems with this kind of thing but unfortunately I can't remember the exact solution. Here's the example from Microsoft - see if this works and then change what you need to:

OPENFILENAME ofn; // common dialog box structure
char szFile[260]; // buffer for file name
HWND hwnd; // owner window
HANDLE hf; // file handle

// Initialize OPENFILENAME
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hwnd;
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 (GetOpenFileName(&ofn)==TRUE)
hf = CreateFile(ofn.lpstrFile, GENERIC_READ,
0, (LPSECURITY_ATTRIBUTES) NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL,
(HANDLE) NULL);

Share this post


Link to post
Share on other sites
The arrays pointed at by "openName.lpstrFile" and "openName.lpstrFileTitle" have to be cleared (zeroed)

Just zero your arrays and it should work.

Share this post


Link to post
Share on other sites
Quote:
Original post by JY
Also try initialising your character arrays - the lpstrFile parameter is used to initialise the dialog box so it might cause a problem if these arrays are not null'd to start with.


Yep. I was too slow.

Share this post


Link to post
Share on other sites
Also, this isn't going to work:

openName.lpstrFileTitle="Open Terrain";

maybe what you meant to do was:

openName.lpstrTitle="Open Terrain";

??

Share this post


Link to post
Share on other sites
The filter string looks wrong to me..

openName.lpstrFilter="*.xml";

Change to:

openName.lpstrFilter="XML Files\0*.xml\0";

If i remember correctly, the format of the filter string has to be [StringToDisplay][NULL][Filter][NULL][NULL].

Each filter is paired with a string that is displayed in the box at the bottom of the dialog and due to the null character being used as a seperator the string must be terminated with two null characters.

Share this post


Link to post
Share on other sites
Thanks, guys. There were a couple of errors in there, but the main one was the filenames not being initialized.

Share this post


Link to post
Share on other sites
You also shouldn't be passing the address of your character arrays...

char fullFilename[255]={0}, filenameOnly[255]={0};
openName.lpstrFile=(LPSTR)&fullFilename;
// should be as follows because your memory is already allocated...They just
// write into your allocated character array
openName.lpstrFile=(LPSTR)fullFilename;

// also
openName.lpstrFileTitle=(LPSTR)&filenameOnly;

// should be this, for the same reasons as above...
openName.lpstrFileTitle=(LPSTR)filenameOnly;



hth
moe.ron

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!