|
-
July 18th, 2003, 04:35 AM
#1
Why doesn't this work?
Why doesn't the BROWSE dialog appear?
Code:
OPENFILENAME file_data;
file_data.lStructSize = 50;
file_data.hwndOwner = hwnd;
file_data.hInstance = hInst;
file_data.lpstrFilter = NULL;
file_data.lpstrCustomFilter = NULL;
file_data.nMaxCustFilter = 40;
file_data.nFilterIndex = NULL;
file_data.lpstrFile = NULL;
file_data.nMaxFile = 256;
file_data.lpstrFileTitle = NULL;
file_data.nMaxFileTitle = NULL;
file_data.lpstrInitialDir = NULL;
file_data.lpstrTitle = NULL;
file_data.Flags = OFN_ALLOWMULTISELECT;
file_data.nFileOffset = NULL;
file_data.nFileExtension = NULL;
file_data.lpstrDefExt = NULL;
file_data.lCustData = NULL;
file_data.lpfnHook = NULL;
file_data.lpTemplateName = NULL;
GetOpenFileName(&file_data);
MessageBox(hwnd, file_data.lpstrFile, "the file", 0);
Borland C++ 5.5 forever!
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.TheDP.tk
-
July 18th, 2003, 04:45 AM
#2
You should always check return codes. If GetOpenFileName returns FALSE you can obtain error information through:
Code:
DWORD CommDlgExtendedError(VOID).
It look suspicous that you set lStructSize to 50. Are you sure that
Code:
sizeof(OPENFILENAME) == 50
?
-
July 18th, 2003, 05:12 AM
#3
Well...there are several errors in your code...
Code:
file_data.lStructSize = 50;
As already mentioned...this flag should be set in the following way:
Windows NT 4.0: In an application that is compiled with WINVER and _WIN32_WINNT >= 0x0500, use OPENFILENAME_SIZE_VERSION_400 for this member.
Windows 2000/XP: Use sizeof (OPENFILENAME) for this parameter.
Code:
file_data.hInstance = hInst;
You are setting an instance handle, however, this member is ignored as long as neither the 'OFN_ENABLETEMPLATEHANDLE' nor the 'OFN_ENBALETEMPLATE' flag is set within the 'Flags' member which is not the case in your example...
Code:
file_data.lpstrCustomFilter = NULL;
file_data.nMaxCustFilter = 40;
With the two lines you are specifying that 'lpstrCustomFilter' points to an allocated memory buffer of 40 bytes which is not the case...this member gets ignored.
Code:
file_data.lpstrFile = NULL;
file_data.nMaxFile = 256;
With the two lines you are specifying that 'lpstrFile' points to an allocated memory buffer of 256 bytes which is not the case...
Code:
MessageBox(hwnd, file_data.lpstrFile, "the file", 0);
With this line you are trying to display the selected filename. However, as I mentioned before you did not supply a valid buffer to the member 'lpstrFile' which you are trying to dereference now. This most-likely will crash your application immediately...
-
July 18th, 2003, 09:41 AM
#4
Can anyone please give me an example?
Borland C++ 5.5 forever!
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.TheDP.tk
-
July 18th, 2003, 03:22 PM
#5
Originally posted by Devil Panther
Can anyone please give me an example?
Code:
char szExtBuffer[MAX_PATH] = "";
OPENFILENAME ofn;
strcpy(szExtBuffer, "*.txt; *.log");
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = 0;
ofn.hInstance = 0;
ofn.lpstrFilter = 0;
ofn.lpstrCustomFilter = 0;
ofn.nMaxCustFilter = 0;
ofn.nFilterIndex = 0;
ofn.lpstrFile = szExtBuffer;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFileTitle = 0;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = 0;
ofn.lpstrTitle = "Open file";
ofn.Flags = 0;
ofn.nFileOffset = 0;
ofn.nFileExtension = 0;
ofn.lpstrDefExt = 0;
ofn.lCustData = 0;
ofn.lpfnHook = 0;
ofn.lpTemplateName = 0;
// Open dialog
if(::GetOpenFileName(&ofn) == FALSE)
[COLOR=#008800]// Cancel pressed
-
July 18th, 2003, 05:38 PM
#6
****... it fails, and doesn't show any dialog box... why???
btw, I'm using borland C++ 5.5.
Borland C++ 5.5 forever!
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.TheDP.tk
-
July 18th, 2003, 05:45 PM
#7
The following works correctly. Also, this should work on any Windows compiler.
Code:
#include <windows.h>
#include <stdio.h>
int main()
{
char szExtBuffer[MAX_PATH] = "";
OPENFILENAME ofn;
strcpy(szExtBuffer, "*.txt; *.log");
memset(&ofn, 0, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.lpstrFile = szExtBuffer;
ofn.nMaxFile = MAX_PATH;
ofn.lpstrTitle = "Open file";
// Open dialog
if(::GetOpenFileName(&ofn) == FALSE)
{
}
return 0;
}
Compiles cleanly, and displays the File open dialog.
Regards,
Paul McKenzie
-
July 19th, 2003, 03:56 AM
#8
It does not show me anything...
Borland C++ 5.5 forever!
"I don't suffer from insanity but enjoy every minute of it" - Edgar Allen Poe
http://www.TheDP.tk
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|