Click here to Skip to main content
15,882,830 members
Home / Discussions / ATL / WTL / STL
   

ATL / WTL / STL

 
GeneralRe: Write REG_MULTI_SZ value in registry Pin
ThatsAlok3-Oct-11 23:55
ThatsAlok3-Oct-11 23:55 
GeneralRe: Write REG_MULTI_SZ value in registry Pin
Richard MacCutchan4-Oct-11 1:19
mveRichard MacCutchan4-Oct-11 1:19 
QuestionHow to disable warning popup in ActiveX Pin
Cpp_Com19-Sep-11 18:49
Cpp_Com19-Sep-11 18:49 
AnswerRe: How to disable warning popup in ActiveX Pin
KingsGambit19-Sep-11 20:19
KingsGambit19-Sep-11 20:19 
GeneralRe: How to disable warning popup in ActiveX Pin
Cpp_Com20-Sep-11 19:07
Cpp_Com20-Sep-11 19:07 
GeneralRe: How to disable warning popup in ActiveX Pin
KingsGambit20-Sep-11 20:36
KingsGambit20-Sep-11 20:36 
GeneralRe: How to disable warning popup in ActiveX Pin
Cpp_Com20-Sep-11 23:38
Cpp_Com20-Sep-11 23:38 
AnswerRe: How to disable warning popup in ActiveX Pin
Abhi Lahare3-Oct-11 6:24
Abhi Lahare3-Oct-11 6:24 
QuestionCan't call DestroyWindow in CView::OnActivateView when activated with Aero preview Pin
Member 823191212-Sep-11 15:33
Member 823191212-Sep-11 15:33 
AnswerRe: Can't call DestroyWindow in CView::OnActivateView when activated with Aero preview Pin
Member 823191214-Sep-11 3:22
Member 823191214-Sep-11 3:22 
GeneralRe: Can't call DestroyWindow in CView::OnActivateView when activated with Aero preview Pin
MicroVirus14-Sep-11 3:32
MicroVirus14-Sep-11 3:32 
QuestionHow to create a ActiveX document server to render a txt file Pin
shelok10-Sep-11 17:17
shelok10-Sep-11 17:17 
AnswerRe: How to create a ActiveX document server to render a txt file Pin
Richard MacCutchan10-Sep-11 21:09
mveRichard MacCutchan10-Sep-11 21:09 
GeneralRe: How to create a ActiveX document server to render a txt file Pin
shelok10-Sep-11 23:19
shelok10-Sep-11 23:19 
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 

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.