// 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);
Win32 GetOpenFileName not displaying dialog
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
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);
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);
The arrays pointed at by "openName.lpstrFile" and "openName.lpstrFileTitle" have to be cleared (zeroed)
Just zero your arrays and it should work.
Just zero your arrays and it should work.
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.
Also, this isn't going to work:
openName.lpstrFileTitle="Open Terrain";
maybe what you meant to do was:
openName.lpstrTitle="Open Terrain";
??
openName.lpstrFileTitle="Open Terrain";
maybe what you meant to do was:
openName.lpstrTitle="Open Terrain";
??
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.
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.
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...
hth
moe.ron
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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement