Click here to Skip to main content
15,899,475 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
QuestionHow to Check the iterator is valid? Pin
jbiaojerry7-Sep-11 23:45
jbiaojerry7-Sep-11 23:45 
AnswerRe: How to Check the iterator is valid? Pin
MicroVirus8-Sep-11 4:07
MicroVirus8-Sep-11 4:07 
AnswerRe: How to Check the iterator is valid? Pin
Stephen Hewitt12-Sep-11 6:18
Stephen Hewitt12-Sep-11 6:18 
GeneralRe: How to Check the iterator is valid? Pin
jbiaojerry18-Sep-11 14:00
jbiaojerry18-Sep-11 14:00 
GeneralRe: How to Check the iterator is valid? Pin
darrendavis30-Sep-11 7:15
darrendavis30-Sep-11 7:15 
QuestionStreambuf problems Pin
Cold_Fearing_Bird6-Sep-11 20:33
Cold_Fearing_Bird6-Sep-11 20:33 
AnswerRe: Streambuf problems [modified] Pin
Orjan Westin6-Sep-11 23:24
professionalOrjan Westin6-Sep-11 23:24 
QuestionHow to use the hook function of Windows FileOpen dialog Pin
clever1015-Sep-11 21:13
clever1015-Sep-11 21:13 
hi,everyone.

When using the Windows FileOpen dialog with multiple selection do you ever wonder how much memory you have to allocate for the buffer. Is one kilobyte going to be enough? Or should you make it ten? How about one Megabyte just to be safe?

It seems that no matter what you choose, you are either going to waste a whole bunch of memory just to be safe, or your user is going to select a bunch of files only to find their selection didn't work because your buffer was too small.

one way is Derive the CFileDialog class , the article as follow introduce it:

Multiple Selection in a File Dialog[]

but I have the other method: use the hook function of CFileDialog. I write some code as follow:

<pre lang="c++">
UINT_PTR CALLBACK MyOFNHookProc( HWND hdlg,  // handle to child dialog box
        UINT uiMsg,  // message identifier
        WPARAM wParam, // message parameter
        LPARAM lParam // message parameter
        )
{
 int nResult = FALSE;

 if (hdlg == NULL) 
  return 0;
#ifdef _DEBUG
 // from "_AfxCommDlgProc()" of the file "dlgcomm.cpp"
 _AFX_THREAD_STATE* pThreadState = AfxGetThreadState();
 // =_AfxActivationWndProc(hwnd,uint,uint,long)
 if (pThreadState->m_pAlternateWndInit != NULL) 
  pThreadState->m_pAlternateWndInit = NULL;
#endif


 switch(uiMsg)
 {

 case WM_NOTIFY:
  {
   LPOFNOTIFY pOfn = (LPOFNOTIFY)lParam;
   switch(pOfn->hdr.code)
   {
   case CDN_SELCHANGE:
    {
     TCHAR dummy_buffer;

     // Get the required size for the 'files' buffer
     HWND hOwner = GetParent(hdlg);
     HWND hParent = GetParent(hOwner);
     UINT nfiles = CommDlg_OpenSave_GetSpec(hOwner, &dummy_buffer, 1);

     // Get the required size for the 'folder' buffer
     int cbLength = CommDlg_OpenSave_GetSpec(GetParent(hdlg), NULL, 0);

     cbLength += _MAX_PATH;

     if(cbLength>(pOfn->lpOFN)->nMaxFile)
     {

      if((pOfn->lpOFN)->lpstrFile)
       HeapFree(GetProcessHeap(),
       0,
       (pOfn->lpOFN)->lpstrFile);

      (pOfn->lpOFN)->lpstrFile = (LPTSTR) HeapAlloc(GetProcessHeap(),
       HEAP_ZERO_MEMORY,
       cbLength);
            (pOfn->lpOFN)->nMaxFile = cbLength;
     }

     nResult = TRUE;
     break;
    }
   default:
    break;
   }
   break;
  }
 default:
  break;
 }
 return nResult;
}

// call function
void CMultiSelectDlg::OnButton1() 
{
  #define NAMEBUF 1024

 TCHAR szFilters[]= _T("MyType Files (*.doc)|*.doc||");

   CFileDialog fileDlg(TRUE, _T("doc"), _T("*.doc"),
  OFN_FILEMUSTEXIST | OFN_HIDEREADONLY|OFN_ALLOWMULTISELECT, szFilters);

   fileDlg.m_ofn.lpstrFile= (LPTSTR) HeapAlloc(GetProcessHeap(),
  HEAP_ZERO_MEMORY,
  NAMEBUF);

 fileDlg.m_ofn.nMaxFile = NAMEBUF;   // redefine nMaxFile 
 fileDlg.m_ofn.lpfnHook = (LPOFNHOOKPROC)MyOFNHookProc;
  
 INT_PTR ret = fileDlg.DoModal();
 if (ret == IDOK)
 {
  int width = 0;
  CString str;
  CDC *pDC = m_listbox.GetDC();
  int saved = pDC->SaveDC();
  pDC->SelectObject(GetFont());

  UINT count = 0;

  POSITION pos = fileDlg.GetStartPosition();
  while (pos)
  {
   str = fileDlg.GetNextPathName(pos);
   m_listbox.AddString(str);
   CSize size(0, 0);
   size = pDC->GetTextExtent(str);
   width = width > size.cx ? width : size.cx;
   ++count;
  }
  pDC->RestoreDC(saved);
  ReleaseDC(pDC);
  m_listbox.SetHorizontalExtent(width + 5);

  str.Format(_T("%u files selected"), count);
  m_static.SetWindowText(str);
 }
 DWORD dwCode = CommDlgExtendedError();
 if (FNERR_BUFFERTOOSMALL==dwCode)
 {
  int i =0;
 }

 HeapFree(GetProcessHeap(),0,
  (fileDlg.m_ofn.lpstrFile));
}


the code Reference the article as follow :

How To Handle FNERR_BUFFERTOOSMALL in Windows[]

but I found when the file names buffer is enough large , it run successly; but when when the file names buffer is not enough large (it means the program will enter if(cbLength>(pOfn->lpOFN)->nMaxFile) ), when after run INT_PTR ret = fileDlg.DoModal(); the value of ret is IDCANCEL. I catch the error code, it is FNERR_BUFFERTOOSMALL . In the end even I allocate a enough large memory ,the error code still is FNERR_BUFFERTOOSMALL . Why???
AnswerRe: How to use the hook function of Windows FileOpen dialog Pin
MicroVirus6-Sep-11 1:35
MicroVirus6-Sep-11 1:35 
GeneralRe: How to use the hook function of Windows FileOpen dialog Pin
clever10110-Sep-11 1:07
clever10110-Sep-11 1:07 
QuestionMemory leak problem. Pin
sirtimid3-Sep-11 8:39
sirtimid3-Sep-11 8:39 
AnswerRe: Memory leak problem. Pin
Stephen Hewitt4-Sep-11 1:33
Stephen Hewitt4-Sep-11 1:33 
GeneralRe: Memory leak problem. Pin
sirtimid4-Sep-11 10:19
sirtimid4-Sep-11 10:19 
QuestionGetting AppData Directory Path Pin
jwalker3432-Sep-11 17:02
jwalker3432-Sep-11 17:02 
AnswerRe: Getting AppData Directory Path Pin
Richard Andrew x643-Sep-11 10:19
professionalRichard Andrew x643-Sep-11 10:19 
GeneralRe: Getting AppData Directory Path Pin
jwalker3435-Sep-11 9:52
jwalker3435-Sep-11 9:52 
GeneralRe: Getting AppData Directory Path Pin
Richard Andrew x645-Sep-11 10:13
professionalRichard Andrew x645-Sep-11 10:13 
AnswerRe: Getting AppData Directory Path Pin
MicroVirus6-Sep-11 1:28
MicroVirus6-Sep-11 1:28 
GeneralRe: Getting AppData Directory Path Pin
jwalker3436-Sep-11 5:44
jwalker3436-Sep-11 5:44 
GeneralRe: Getting AppData Directory Path Pin
MicroVirus6-Sep-11 15:31
MicroVirus6-Sep-11 15:31 
GeneralRe: Getting AppData Directory Path Pin
jwalker3437-Sep-11 16:03
jwalker3437-Sep-11 16:03 
Questionhelp me!!! Pin
zz526335731-Aug-11 5:30
zz526335731-Aug-11 5:30 
QuestionHow to use forms / dialog boxes with an ATL Project / BHO? Pin
abetterword23-Aug-11 4:22
abetterword23-Aug-11 4:22 
AnswerRe: How to use forms / dialog boxes with an ATL Project / BHO? Pin
MicroVirus6-Sep-11 2:06
MicroVirus6-Sep-11 2:06 
QuestionFailure to Recognise Base Class [modified] Pin
Bram van Kampen19-Aug-11 13:47
Bram van Kampen19-Aug-11 13:47 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.