renodino has asked for the wisdom of the Perl Monks concerning the following question:

Just checking before I run down a rabbit hole...LockFile/LockFileEx are consipicuously absent from Win32API::File; is there a reason ? Perhaps a concern about Win32's mandatory locking (esp wrt locks persisting after crashes) ? Or is it just that no one's gotten around to implementing it yet ?

Replies are listed 'Best First'.
Re: Why no LockFile() in Win32API::File ?
by ikegami (Patriarch) on Apr 10, 2006 at 22:34 UTC
    I don't know why it's not in the module, but you can still call the function quite easily. You can use Win32::API to call LockFileEx. You can get the necessary file handle by opening the file using Win32API::File's createFile or createFile, or by using Win32API::File's GetOsFHandle.
Re: Why no LockFile() in Win32API::File ?
by BrowserUk (Patriarch) on Apr 11, 2006 at 01:37 UTC

    However, if you need the emulation, then it already exists in and is exported by perl58.dll under the name win32_flock. It can be access through Win32::API::Prototype as follows:

    Update: Changed use to require + import.

    #! perl -slw use strict; use Fcntl qw[ :flock ]; $|=1; if( $^O eq 'MSWin32' ) { { local $^W; require Win32::API::Prototype; Win32::API::Prototype->import( 'ApiLink' ); } ApiLink( 'perl58', 'int win32_flock(int fd, int oper)' ) or die $^ +E; our $flock; *flock = *win32_flock = *win32_flock; } die 'Already running' unless flock( DATA, LOCK_EX ); sleep 10; flock( DATA, LOCK_UN ) or die $!; __DATA__ stuff

    However, in my tests just now, when running a second copy of the above script, the call to flock( LOCK_EX ), causes the script to silently terminate, so that may not be so useful after all.

    I'm not sure whether

    1. I'm simply using the call incorrectly (which is possible as I've never needed flock());
    2. or if there is a bug in the implementation (at win32.c line 2246 in the 5.8.8 sources);
    3. Or there is a bug in the transition between perl and C via Win32::API(::Prototype);

    but maybe this and the pointer to the source will let someone who knows what they are doing work out which and, either point out my stupidity or raise a bug against the responsible component?


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      Isnt Win32_flock() used for perls flock() function?

      ---
      $world=~s/war/peace/g

        Could well be...:). As I've never had a use for it, and the OP was complaining of it's absence, I assumed it wasn't available. As I was in win32.c at the time and saw win32_flock() right there, I thought I would pass the info along.

        Now, perhaps you can explain why my attempt to use it doesn't work?


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Why no LockFile() in Win32API::File ?
by BrowserUk (Patriarch) on Apr 11, 2006 at 00:31 UTC

    There's simply no good reason for emulating cooperative locking, except maybe for compatiblilty with inferior systems :)


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: Why no LockFile() in Win32API::File ?
by demerphq (Chancellor) on Apr 11, 2006 at 08:28 UTC

    Why dont you patch it in? I think that would be a good addition to Win32API::File and I think tye would be happy to get a patch adding it. Which reminds me I wanted to put together a doc patch to it myself....

    ---
    $world=~s/war/peace/g