title: shell32.dll: access violation during GetOpenFileName new thread
body: <p>GetOpenFileName fails with access violation.  File must be on DESKTOP and have long name.
  Problem occurs only after first successful open of the file.  Problem occurs when mouse cursor hovers over file as tool tip about to be displayed.</p>
  
  <p>See the answer below.  I'm leaving the original problem description below.</p>
  
  <p>Mike D.</p>
  
  <p>=======================</p>
  
  <p>I'm using GetOpenFileName.  I sometimes get a access violation deep inside shell32.  The violation never occurs the first time this code is used, it often takes five or six tries.  Also it appears that if one selects a file in second or two after the open file window pops up, the violation does not occur.  Also, the call stack displayed when I debug this does not include any of my code.  It's as if some independent thread is waking up to do something.</p>
  
  <p>Any insights into how I might debug this greatly appreciated!</p>
  
  <p>I made a "hello" world app exhibiting the same behavior.  It, however, requires many more tries before it fails. It also seems that one has to switch directories before it will fail.</p>
  
  <p>The GOFN is done from a thread created just for that purpose.  Below is the code from the "hello world" app.</p>
  
  <pre><code>typedef struct 
  {
  public:
      HWND        hWnd;
      HINSTANCE   hInst;
  } def_params, *p_params;
  
  DWORD WINAPI ReadLogFile_DataRecorderThread (PVOID pvoid);
  
  void ReadLogFile_DataRecorder (HWND hWnd, HINSTANCE hInst)  // ***************************
  {
      static def_params Params;
  
      Params.hWnd = hWnd;
      Params.hInst = hInst;
  
      HANDLE T = CreateThread (NULL,0,ReadLogFile_DataRecorderThread,&amp;Params,0,NULL);
  
      CloseHandle (T);
  
      return;
  }
  
  DWORD WINAPI ReadLogFile_DataRecorderThread (PVOID pvoid)   
  {
      p_params P = (p_params) pvoid;
  
      HWND hWnd = P-&gt;hWnd;
      HINSTANCE hInst = P-&gt;hInst;
  
      char    ReadLogFileLastDir[256];
  
  //  static def_OpenFileHook Hook;
  
      OPENFILENAME    ofn;
      char            fn[MAX_PATH]="\0";
      char            filter[32]="Text Files\0*.TXT;\0\0";
      char            title[]="Open IMC Data Recorder Log File";
      char            defext[]="TXT";
      int             status;
  
  // Get File Name
  
      fn[0] = '\0';
      ReadLogFileLastDir[0] = '\0';
  
      ZeroMemory(&amp;ofn, sizeof(ofn));
  
      ofn.lStructSize         = sizeof(ofn);
      ofn.hwndOwner           = hWnd;
      ofn.hInstance           = hInst;
      ofn.hInstance           = (HINSTANCE) GetWindowLong (hWnd, GWL_HINSTANCE);
  
      ofn.lpstrFilter         = filter;
      ofn.nFilterIndex        = 0;
      ofn.lpstrCustomFilter   = NULL ;
      ofn.nMaxCustFilter      = 0 ;
      ofn.lpstrFile           = fn;
      ofn.nMaxFile            = sizeof(fn);
      ofn.lpstrFileTitle      = NULL;
  
      if (ReadLogFileLastDir[0] == '\0')
      {
          SHGetSpecialFolderPath (NULL,ReadLogFileLastDir,0x0005,false);
      };
      ofn.lpstrInitialDir = ReadLogFileLastDir;
      ofn.lpstrTitle          = title;
      ofn.Flags               = OFN_FILEMUSTEXIST  | 
                                OFN_PATHMUSTEXIST  | 
                                OFN_EXPLORER       | 
                                // OFN_ENABLETEMPLATE | 
                                OFN_ENABLESIZING   | 
                                // OFN_ENABLEHOOK     |
                                OFN_READONLY;
      ofn.lpstrDefExt         = NULL;
      ofn.lpfnHook            = NULL;         // Hook.DialogHook; // hook routine
      ofn.lCustData           = NULL;          // (long) &amp;Hook;       // data for hook routine
      ofn.lpTemplateName      = NULL;          // MAKEINTRESOURCE(IDD_HOOKFILEOPEN);
      ofn.nFileOffset         = 0 ;
      ofn.nFileExtension      = 0 ;
      ofn.lpstrDefExt         = defext;
  
      status = GetOpenFileName (&amp;ofn);
  
      int S;
  
      S = CommDlgExtendedError();
  
      return 0;
  }
  </code></pre>
  
  <p>When it fails, the call stack looks like this...</p>
  
  <pre><code>SHELL32! 7ca4e035()
  SHELL32! 7cb2dc16()
  SHELL32! 7cb2dd5a()
  SHELL32! 7cb27361()
  SHELL32! 7c9f40a3()
  BROWSEUI! 75f81b9a()
  SHLWAPI! 77f69548()
  NTDLL! 7c927545()
  NTDLL! 7c927583()
  NTDLL! 7c927645()
  NTDLL! 7c92761c()
  KERNEL32! 7c80b50b()
  </code></pre>
  
  <p>Sorry but I am unable to get symbols for these as I have an old Visual C++ :-(</p>
  
  <p>It appears to me that the problem occurs when the GOFN stuff is about to open the popup describing the file as the mouse cursor hovers over the file name.</p>
  
  <p><strong>The set of circumstances causing the problem are somewhat weird.  Experiments show one has to do the following in the GOFN window:</strong></p>
  
  <ul>
  <li>Open a file on the DESKTOP </li>
  <li>Hover over a long file name</li>
  </ul>
  
  <p>If I do this twice, it always fails.  The file name I used was </p>
  
  <p>IMCLOG_20120323_1658_-_20120324_0653_CST_+DST_E2_2_second.TXT</p>
  
  <p><strong>I tried the same thing with NOTEPAD and the same problem occurs!</strong></p>