Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Other Discussions
  3. Clever Code
  4. MFC CFile::Open Flags

MFC CFile::Open Flags

Scheduled Pinned Locked Moved Clever Code
helpc++questionlounge
2 Posts 2 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lost User
    wrote on last edited by
    #1

    I recently had a problem reported to me where, seemingly at random, a service application was failing to open a file, with an "Access denied" error being logged. The application in question relies heavily on threads. I eventually traced the problem to the following line of (MFC) code:

    CFile file;
    file.Open(lpszFileName, CFile::modeRead);
    ...

    The problem occurred when two threads attempted to open the same file at the same time. The bug? I'd forgotten to add any share flags! This meant that if thread A opened the file for reading, thread B would then fail. Ack. I guess I am used to using STL streams (such as std::ifstream), which, by default, will allow other processes/threads to open a file by specifying a more useful default share mode. To fix the bug I had to add a flag such as CFile::shareDenyWrite, so other processes could still open the file for reading.

    CFile file;
    file.Open(lpszFileName, CFile::modeRead|CFile::shareDenyWrite);
    ...

    This may be obvious, but to a noob, or to someone that hasn't used MFC or Win32 ::CreateFile in a long time, it might bite you on the backside too. :)


    Kicking squealing Gucci little piggy.

    B 1 Reply Last reply
    0
    • L Lost User

      I recently had a problem reported to me where, seemingly at random, a service application was failing to open a file, with an "Access denied" error being logged. The application in question relies heavily on threads. I eventually traced the problem to the following line of (MFC) code:

      CFile file;
      file.Open(lpszFileName, CFile::modeRead);
      ...

      The problem occurred when two threads attempted to open the same file at the same time. The bug? I'd forgotten to add any share flags! This meant that if thread A opened the file for reading, thread B would then fail. Ack. I guess I am used to using STL streams (such as std::ifstream), which, by default, will allow other processes/threads to open a file by specifying a more useful default share mode. To fix the bug I had to add a flag such as CFile::shareDenyWrite, so other processes could still open the file for reading.

      CFile file;
      file.Open(lpszFileName, CFile::modeRead|CFile::shareDenyWrite);
      ...

      This may be obvious, but to a noob, or to someone that hasn't used MFC or Win32 ::CreateFile in a long time, it might bite you on the backside too. :)


      Kicking squealing Gucci little piggy.

      B Offline
      B Offline
      box
      wrote on last edited by
      #2

      Useful experience. Thanks.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups