title: Why can't I open a file for reading if (theoretically) I should be allowed?
body: <p>I have two projects in C: </p>
  
  <p>The first:</p>
  
  <pre><code>include windows.h
  include stdio.h
  include tchar.h
  
  int main()
  {
       HANDLE hFile = CreateFile("D:\\f.txt",
      GENERIC_WRITE,
      FILE_SHARE_READ,
      NULL,
      OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL,
      NULL);
  
      if(hFile == INVALID_HANDLE_VALUE)
          _tprintf("Error: CreateFile %d\n",GetLastError());
  
      Sleep(5000);
  
      return 0;
  }
  </code></pre>
  
  <p>The Second:</p>
  
  <pre><code>include windows.h
  include stdio.h
  include tchar.h
  
  int main()
  {
       HANDLE hFile = CreateFile("D:\\f.txt",
      GENERIC_READ,
      FILE_SHARE_READ,
      NULL,
      OPEN_EXISTING,
      FILE_ATTRIBUTE_NORMAL,
      NULL);
  
  if(hFile == INVALID_HANDLE_VALUE)
      _tprintf("Error: CreateFile %d\n",GetLastError());
  
              return 0;
  }
  </code></pre>
  
  <p>The first program is supposed to open the file for reading while allowing others to read from it.
  The second is supposed to open the file for reading.</p>
  
  <p>When I run the program, the second one give me error 32 (ERROR_SHARING_VIOLATION).</p>
  
  <p>I thought the whole point of FILE_SHARE_READ was to allow other threads/processes to open a file just for reading regardless of whether it's already open or not.</p>
  
  <p>Can anyone help me solve this problem?</p>
  
  <p>P.S. If the file were a mailslot, would that make any difference?</p>