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. General Programming
  3. C / C++ / MFC
  4. Making a file not accessible by other applications

Making a file not accessible by other applications

Scheduled Pinned Locked Moved C / C++ / MFC
question
6 Posts 4 Posters 0 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.
  • V Offline
    V Offline
    vasu_sri
    wrote on last edited by
    #1

    i am working an application that uses a file for some purpose. i am creating that file by using CreateFile() function. Now, i want to make sure that no other application uses that File till me application closes. i.e, i want no other application to access the file (not even for reading) as long as my application is running. how can i do this?

    Regards, Srinivas

    P R D 3 Replies Last reply
    0
    • V vasu_sri

      i am working an application that uses a file for some purpose. i am creating that file by using CreateFile() function. Now, i want to make sure that no other application uses that File till me application closes. i.e, i want no other application to access the file (not even for reading) as long as my application is running. how can i do this?

      Regards, Srinivas

      P Offline
      P Offline
      prasad_som
      wrote on last edited by
      #2

      Use dwShareMode as NULL while using CreateFile API.

      Prasad Notifier using ATL | Operator new[],delete[][^]

      V 1 Reply Last reply
      0
      • V vasu_sri

        i am working an application that uses a file for some purpose. i am creating that file by using CreateFile() function. Now, i want to make sure that no other application uses that File till me application closes. i.e, i want no other application to access the file (not even for reading) as long as my application is running. how can i do this?

        Regards, Srinivas

        R Offline
        R Offline
        Roger Stoltz
        wrote on last edited by
        #3

        In addition to what Prasad said: if you're using MFC and CFile, you should use the CFile::shareExclusive flag when opening the file. Read more here[^].


        "It's supposed to be hard, otherwise anybody could do it!" - selfquote
        "High speed never compensates for wrong direction!" - unknown

        1 Reply Last reply
        0
        • P prasad_som

          Use dwShareMode as NULL while using CreateFile API.

          Prasad Notifier using ATL | Operator new[],delete[][^]

          V Offline
          V Offline
          vasu_sri
          wrote on last edited by
          #4

          No, it is not working for me. i don't know what mistake i am doing. here is the sample code of what i have written hFile = CreateFile ( strFileName,GENERIC_WRITE ,NULL,NULL,CREATE_ALWAYS ,FILE_ATTRIBUTE_NORMAL,NULL); after creation of the file, i write the data to that file. now i dont want any other application to access that File till i close my application. how can i do this? thanks..

          Regards, Srinivas

          P 1 Reply Last reply
          0
          • V vasu_sri

            No, it is not working for me. i don't know what mistake i am doing. here is the sample code of what i have written hFile = CreateFile ( strFileName,GENERIC_WRITE ,NULL,NULL,CREATE_ALWAYS ,FILE_ATTRIBUTE_NORMAL,NULL); after creation of the file, i write the data to that file. now i dont want any other application to access that File till i close my application. how can i do this? thanks..

            Regards, Srinivas

            P Offline
            P Offline
            prasad_som
            wrote on last edited by
            #5

            vasu_sri wrote:

            No, it is not working for me.

            How you are verifying this.

            vasu_sri wrote:

            now i dont want any other application to access that File till i close my application.

            Yes, by above code, no other application could access the file, till handle is closed.

            vasu_sri wrote:

            how can i do this?

            Its already shown.

            Prasad Notifier using ATL | Operator new[],delete[][^]

            1 Reply Last reply
            0
            • V vasu_sri

              i am working an application that uses a file for some purpose. i am creating that file by using CreateFile() function. Now, i want to make sure that no other application uses that File till me application closes. i.e, i want no other application to access the file (not even for reading) as long as my application is running. how can i do this?

              Regards, Srinivas

              D Offline
              D Offline
              DLChambers
              wrote on last edited by
              #6
                HANDLE hFile = CreateFile(   
                               psFile,
                               GENERIC\_READ | GENERIC\_WRITE,
                               0,   // No sharing
                               NULL,
                               OPEN\_EXISTING,
                               FILE\_ATTRIBUTE\_NORMAL,
                               NULL);
                DWORD dwErr = GetLastError();
                // NOTE: Error 5 likely indicates file is marked read-only
              

              ...

                CloseHandle(hFile); // Unlock it
              
              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