Win32 GetOpenFileName not displaying dialog

Started by
7 comments, last by moeron 18 years, 10 months ago
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);

Advertisement
Try to set the "openName.hwndOwner" to 0 and set the hInstance.

Biscuit.
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);

"Absorb what is useful, reject what is useless, and add what is specifically your own." - Lee Jun Fan
The arrays pointed at by "openName.lpstrFile" and "openName.lpstrFileTitle" have to be cleared (zeroed)

Just zero your arrays and it should work.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Also, this isn't going to work:

openName.lpstrFileTitle="Open Terrain";

maybe what you meant to do was:

openName.lpstrTitle="Open Terrain";

??
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
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.
Progress is born from the opportunity to make mistakes.

My prize winning Connect 4 AI looks one move ahead, can you beat it? @nickstadb
Thanks, guys. There were a couple of errors in there, but the main one was the filenames not being initialized.
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 arrayopenName.lpstrFile=(LPSTR)fullFilename;// alsoopenName.lpstrFileTitle=(LPSTR)&filenameOnly;// should be this, for the same reasons as above...openName.lpstrFileTitle=(LPSTR)filenameOnly;


hth
moe.ron
moe.ron

This topic is closed to new replies.

Advertisement