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. Lock a file

Lock a file

Scheduled Pinned Locked Moved C / C++ / MFC
csharpc++
14 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.
  • L Lost User

    Iain Wiseman wrote:

    and found xerces-c was not able to access the file for reading in the same process.

    You really need to explain that in technical terms, i.e. what code you used and what errors you received. I'm not sure about item 1, but 2,3 and 4 are quite straightforward in Windows.

    One of these days I'm going to think of a really clever signature.

    I Offline
    I Offline
    Iain Wiseman
    wrote on last edited by
    #3

    I want to disable other processing from locking the file. When I use CreateFile, this works except it also prevents Xerces from reading the file so the code looks a little like this

    m_Handle = CreateFile(
    TEXT(inFullyQualifiedPath.c_str()),
    GENERIC_ALL,
    0,
    NULL,
    OPEN_EXISTING,
    NULL,
    NULL);

    m\_ConfigFileParser = new xercesc::XercesDOMParser;
    m\_ConfigFileParser->parse(m\_Filename.c\_str());
    

    The reading of the file fails because Xerces cannot open the file.

    I 1 Reply Last reply
    0
    • I Iain Wiseman

      I want to disable other processing from locking the file. When I use CreateFile, this works except it also prevents Xerces from reading the file so the code looks a little like this

      m_Handle = CreateFile(
      TEXT(inFullyQualifiedPath.c_str()),
      GENERIC_ALL,
      0,
      NULL,
      OPEN_EXISTING,
      NULL,
      NULL);

      m\_ConfigFileParser = new xercesc::XercesDOMParser;
      m\_ConfigFileParser->parse(m\_Filename.c\_str());
      

      The reading of the file fails because Xerces cannot open the file.

      I Offline
      I Offline
      Iain Wiseman
      wrote on last edited by
      #4

      Solved by using

      GENERIC_READ and FILE_SHARE_READ | FILE_SHARE_WRITE

      L 1 Reply Last reply
      0
      • I Iain Wiseman

        Solved by using

        GENERIC_READ and FILE_SHARE_READ | FILE_SHARE_WRITE

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #5

        Iain Wiseman wrote:

        FILE_SHARE_READ | FILE_SHARE_WRITE

        You do understand that those options allow other processes to read and write the file at the same time.

        One of these days I'm going to think of a really clever signature.

        I 1 Reply Last reply
        0
        • L Lost User

          Iain Wiseman wrote:

          and found xerces-c was not able to access the file for reading in the same process.

          You really need to explain that in technical terms, i.e. what code you used and what errors you received. I'm not sure about item 1, but 2,3 and 4 are quite straightforward in Windows.

          One of these days I'm going to think of a really clever signature.

          S Offline
          S Offline
          Sarath C
          wrote on last edited by
          #6

          Don't you read the file if the access is denied to delete it? Otherwise, simply try to delete the file using DeleteFile. It will give you ERROR_ACCESSDENIED return code if it fails. Alternatively you can check the File Security information Check Here[^]

          -Sarath.

          My blog - iSpeak code

          Rate the answers and close your posts if it's answered

          L 1 Reply Last reply
          0
          • L Lost User

            Iain Wiseman wrote:

            FILE_SHARE_READ | FILE_SHARE_WRITE

            You do understand that those options allow other processes to read and write the file at the same time.

            One of these days I'm going to think of a really clever signature.

            I Offline
            I Offline
            Iain Wiseman
            wrote on last edited by
            #7

            If this isn't correct then is there something that is? It seemed to work when I ran a separate process with the same code and put a pause in it. I totally sure you are right. If you do know the right combination I would be grateful. It seemed such a simple thing to want to do. Iain

            L 1 Reply Last reply
            0
            • S Sarath C

              Don't you read the file if the access is denied to delete it? Otherwise, simply try to delete the file using DeleteFile. It will give you ERROR_ACCESSDENIED return code if it fails. Alternatively you can check the File Security information Check Here[^]

              -Sarath.

              My blog - iSpeak code

              Rate the answers and close your posts if it's answered

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #8

              I don't think this should be addressed to me.

              One of these days I'm going to think of a really clever signature.

              1 Reply Last reply
              0
              • I Iain Wiseman

                If this isn't correct then is there something that is? It seemed to work when I ran a separate process with the same code and put a pause in it. I totally sure you are right. If you do know the right combination I would be grateful. It seemed such a simple thing to want to do. Iain

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #9

                Sorry, I'm not sure exactly what your problem is. You said it works now, I was just pointing out that those options mean you do not have exclusive access to the file.

                One of these days I'm going to think of a really clever signature.

                I 1 Reply Last reply
                0
                • L Lost User

                  Sorry, I'm not sure exactly what your problem is. You said it works now, I was just pointing out that those options mean you do not have exclusive access to the file.

                  One of these days I'm going to think of a really clever signature.

                  I Offline
                  I Offline
                  Iain Wiseman
                  wrote on last edited by
                  #10

                  I am simply after in one process being able to prevent someone else opening a file whilst I am reading it. I do not have control over the opening of the file as this is done within the Xerces library. The file is an xml file hence the use of xerces. Once I have completed the read I want to sometimes delete the file. Ideally I would have like to in my process Check I have exclusive rights to the file Read it using xerces Delete it

                  S L 2 Replies Last reply
                  0
                  • I Iain Wiseman

                    I am simply after in one process being able to prevent someone else opening a file whilst I am reading it. I do not have control over the opening of the file as this is done within the Xerces library. The file is an xml file hence the use of xerces. Once I have completed the read I want to sometimes delete the file. Ideally I would have like to in my process Check I have exclusive rights to the file Read it using xerces Delete it

                    S Offline
                    S Offline
                    SandipG
                    wrote on last edited by
                    #11

                    A Simple work around to your problem would be use your original code to get the exclusive access to file, Read the file and create a temporary file which you can pass it to Xerces.. later delete the temp file.. and if you want delete even the source/original file. Other alternatives could be to check if xerces takes text(some temp buffer) as input instead of file name.. so you can read the file in buffer and pass it to xerces.

                    1 Reply Last reply
                    0
                    • I Iain Wiseman

                      This looked easy but it wasn't I want to 1/ Test if I have rights to delete a file 2/ lock it 3/ read it 4/ delete it This is all in one process Gave up on 1/ but tried to use CreateFile to lock it and found xerces-c was not able to access the file for reading in the same process. Any pointers (no pun intended). Not .net just C++/Win32 Thanks Iain

                      S Offline
                      S Offline
                      Sarath C
                      wrote on last edited by
                      #12

                      Don't you read the file if the access is denied to delete it? Otherwise, simply try to delete the file using DeleteFile. It will give you ERROR_ACCESSDENIED return code if it fails. Alternatively you can check the File Security information Check Here[^]

                      -Sarath.

                      My blog - iSpeak code

                      Rate the answers and close your posts if it's answered

                      1 Reply Last reply
                      0
                      • I Iain Wiseman

                        I am simply after in one process being able to prevent someone else opening a file whilst I am reading it. I do not have control over the opening of the file as this is done within the Xerces library. The file is an xml file hence the use of xerces. Once I have completed the read I want to sometimes delete the file. Ideally I would have like to in my process Check I have exclusive rights to the file Read it using xerces Delete it

                        L Offline
                        L Offline
                        Lost User
                        wrote on last edited by
                        #13

                        Iain Wiseman wrote:

                        I do not have control over the opening of the file as this is done within the Xerces library.

                        In that case there is nothing you can do to gain exclusive access to it. I have no experience of the Xerces library, but are you sure there is no option to pass the handle of a previously opened file into it?

                        One of these days I'm going to think of a really clever signature.

                        I 1 Reply Last reply
                        0
                        • L Lost User

                          Iain Wiseman wrote:

                          I do not have control over the opening of the file as this is done within the Xerces library.

                          In that case there is nothing you can do to gain exclusive access to it. I have no experience of the Xerces library, but are you sure there is no option to pass the handle of a previously opened file into it?

                          One of these days I'm going to think of a really clever signature.

                          I Offline
                          I Offline
                          Iain Wiseman
                          wrote on last edited by
                          #14

                          I did in the end copy the file to a temporary file which gets destroyed on destruction. Thanks all for your help.

                          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