Solved

Win32 API - Get filename in OpenFile dialog

Posted on 2006-11-12
2
2,888 Views
Last Modified: 2013-11-20
    In Win32 API, I get the path and filename the user select by sending the message
CDM_GETFILEPATH:

          SendMessage(hListbox, CDM_GETFILEPATH, iSize, (LPARAM) szFileName)

 in which hListBox is the HANDLE of listbox in OpenFile dialog.

    I have not been successful yet. Pls help me. Thks so much and Grade A for the solution.


0
Comment
Question by:Harry_Kewell
2 Comments
 
LVL 22

Accepted Solution

by:
mahesh1402 earned 100 total points
ID: 17927779
Following are details for CDM_GETFILEPATH :
http://msdn2.microsoft.com/en-us/library/ms646847.aspx

The CDM_GETFILEPATH message retrieves the path and file name of the selected file in an Explorer-style Open or Save As dialog box. The dialog box must have been created with the OFN_EXPLORER flag; otherwise, the message fails.

You can  use the CDM_GETFILEPATH message to get the full path of the selected file like following :

// Called when the selected file has changed
BOOL CMyFileDialog::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
      OFNOTIFY* pOFN = (OFNOTIFY*)lParam;

      if (pOFN->hdr.code == CDN_SELCHANGE)
               {
            // Selection changed
            // Get the filename
            TCHAR szFileName[MAX_PATH];
            ::ZeroMemory(szFileName, MAX_PATH);
            GetParent()->SendMessage(CDM_GETFILEPATH, MAX_PATH, (LPARAM)szFileName);
            ProcessFile(szFileName);
      }

      // Pass to base class
      return CFileDialog ::OnNotify(wParam, lParam, pResult);
}

-MAHESH
0
 
LVL 8

Assisted Solution

by:YoungBonzi
YoungBonzi earned 50 total points
ID: 17949405
http://msdn2.microsoft.com/en-us/library/ms646829.aspx#open_file

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);
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 85
ShiftLeft challenge 21 76
NotAlone Challenge 20 80
fix34  challenge 9 105
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now