Trying to create a "File Open" dialog which is opened in response to a button clicked in a View

Hi,

As the title states -- I have a view (actually several) that I have a button that I hope will open a 'file open' dialog so the user can use it to locate the file he/she is looking for.  I have added the button and used VC++ to map the button click to run the following function:

void CAlbumView::OnAddCover()
{
                                                                                               
        OPENFILENAME ofn;       // common dialog box structure
        char szFile[260];       // buffer for file name
        HWND hwnd = NULL;              // owner window
        HANDLE hf;              // file handle
                                                                                               
        // Initialize OPENFILENAME
        ZeroMemory(&ofn, sizeof(OPENFILENAME));
        ofn.lStructSize = sizeof(OPENFILENAME);
        ofn.hwndOwner = hwnd;
        ofn.lpstrFile = szFile;
        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);
}

(Something I copyed from MSDN)

-- So the problem?

The dialog box never shows.  I am sure the code is beening executed, but nothing is happening...  :{

Anyone have any idea why not?

Nasa
nasa000Asked:
Who is Participating?
 
yukapapaConnect With a Mentor Commented:
Hi nasa000.

The OPENFILENAME::lpstrFile must point to an initialized string buffer. If the initizlization is not necessary, the first character in the buffer pointed by lpstrFile must be '\0'.

Try to define szFile as char szFile[260] = "".

I could not find out any other problems from your code.

bye.
0
 
jkrCommented:
Have you tried to call "GetLastError()" after calling "GetOpenFilename()" when the call fails?
0
 
nasa000Author Commented:
Thanks yukapapa,

making the

char szFile[260] = "".


change fixted the problem


Thanks Again
0
All Courses

From novice to tech pro — start learning today.