Results 1 to 20 of 23

Thread: Lock a file

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Lock a file

    Hi,

    is there any way I can lock a file with Qt libraries?
    For "lock" I mean that when my app opens the file for writing, no other process can write on it. This happens for many Windows applications working with files.
    I've tried QtLockedFile, but it only works among processes using QtLockedFile to access the file.

    I've tried to call the native Windows API LockFile but it does not work because I can't give it a valid HANDLE to the file (QFile::handle() returns rubbish).

    How can I fix this?

    Thank you
    Giacomo

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Lock a file

    Quote Originally Posted by euthymos View Post
    I've tried to call the native Windows API LockFile but it does not work because I can't give it a valid HANDLE to the file (QFile::handle() returns rubbish).
    Did you cast this rubbish to a proper type?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Re: Lock a file

    Quote Originally Posted by wysota View Post
    Did you cast this rubbish to a proper type?
    Rubbish was not meant to be offensive, but ironic

    However, I casted it to HANDLE, which is declared in windows.h

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Lock a file

    I didn't think it was offensive :-) Casting it to HANDLE should have worked. What exactly happens when you try to use the handle?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Re: Lock a file

    Quote Originally Posted by wysota View Post
    I didn't think it was offensive :-) Casting it to HANDLE should have worked. What exactly happens when you try to use the handle?
    It could have been offensive towards the developers...

    However, if I cast it to HANDLE and pass it to LockFile(HANDLE, DWORD, DWORD, DWORD, DWORD) Windows API, the function returns false. GetLastError() returns 6, which is "invalid handle" error code.
    I think this leaves no room to other possibilities: the handle is not valid.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Lock a file

    Maybe you need to pass the result through some other function first?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    8
    Thanked 133 Times in 128 Posts

    Default Re: Lock a file

    newbie reply...
    Win32 CreateFile() to open file in locked mode;
    QFile open again
    QFile close
    CloseFile (or whatever win api).

    i really dont think that will work but just had this tickling in my mid..

  8. #8
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Re: Lock a file

    MrDeath: I'm a novice, too. I've been programming with Windows' APIs for a short time, and then in Java. Now I'm learning C++/Qt.

    That solution might work fine. I think I'll try it when I get back to development machine.
    However, I just can't see why QFile::handle() returns an invalid handle.

    wysota: I don't know if I have to pass it to another function, the docs say nothing about that.

    EDIT: there was a function to call! _get_osfhandle(int fd)
    I've learnt that thanks to Qxt guys. See http://doc.libqxt.org/0.5.0/classQxtFileLock.html
    Last edited by euthymos; 2nd June 2009 at 16:28.

  9. #9
    Join Date
    May 2009
    Posts
    63
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    12
    Thanked 2 Times in 2 Posts

    Default Re: Lock a file

    1. Win32 CreateFile() to open file in locked mode;
    2. QFile open again
    3. QFile close
    4. CloseFile (or whatever win api).

    How can step 2 work? - it is trying to open a locked file.

  10. #10
    Join Date
    Jun 2006
    Posts
    10
    Thanks
    1

    Default Re: Lock a file

    Quote Originally Posted by jonks View Post
    How can step 2 work? - it is trying to open a locked file.
    Actually I don't know... but maybe, because it's the same application and the same thread accessing the file, it can be opened after the lock. Just guessing.

    However, I solved with a great "native" solution inspired by Qxt's "QxtFileLock" class (http://doc.libqxt.org/0.5.0/classQxtFileLock.html). You should check this Qxt thing out, it's really good.

  11. #11
    Join Date
    Jun 2007
    Location
    India
    Posts
    1,042
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows
    Thanks
    8
    Thanked 133 Times in 128 Posts

    Default Re: Lock a file

    Quote Originally Posted by euthymos View Post
    Actually I don't know... but maybe, because it's the same application and the same thread accessing the file, it can be opened after the lock. Just guessing.
    that was what i thought. Did it worked? i am on linux so cant test it.

    Quote Originally Posted by euthymos View Post
    However, I solved with a great "native" solution inspired by Qxt's "QxtFileLock" class (http://doc.libqxt.org/0.5.0/classQxtFileLock.html). You should check this Qxt thing out, it's really good.
    this website is blocked at my office. can u please copy and paste the solution. thx..

Similar Threads

  1. Read binary from file
    By weldpua2008 in forum Newbie
    Replies: 2
    Last Post: 3rd April 2009, 23:50
  2. Can you specify a file engine?
    By skimber in forum Qt Programming
    Replies: 2
    Last Post: 18th September 2008, 15:54
  3. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Exclusive file creation (Samba shares)
    By marcel in forum General Programming
    Replies: 1
    Last Post: 13th April 2007, 12:26

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.