title: Locking Entire file using LockFileEx API in Windows
body: <p>It is possible to do lock particular offset using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx" rel="nofollow">LockFileEx</a> API in windows using C++, I tried this and i got successful result. </p>
  
  <p>But I tried to lock entire file using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx" rel="nofollow">LockFileEx</a>, I failed to do. i didn't found any document on website how to do full file locking using LockfileEX. </p>
  
  <pre><code>    ifile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_ALWAYS, 0, NULL);
  
      if (ifile == INVALID_HANDLE_VALUE) {
          printf("CreateFile failed (%d)\n", GetLastError());
          return 1;
      }
  
      OVERLAPPED overlapvar;
      overlapvar.Offset = 0; 
      overlapvar.OffsetHigh = 0;
  
      success = LockFileEx(ifile, LOCKFILE_EXCLUSIVE_LOCK | LOCKFILE_FAIL_IMMEDIATELY, 0, 0, 0, &amp;overlapvar);
  </code></pre>
  
  <p>I tried with above code in windows but it is not taking lock on entire file. In my windows code i gave <code>overlapvar.Offset = 0</code> to say start of the file and <code>5th argument of LockFileEx to 0</code> to lock till end of the file. </p>
  
  <p>I tried the same method in <strong>linux</strong> using <strong>fcntl</strong> like below.</p>
  
  <pre><code>struct  flock   param ;
  param.l_type = F_RDLCK ;
  param.l_whence = 0 ;
  param.l_start = 0 ; //start of the file
  param.l_len = 0 ; // 0 means end of the file
  
  fcntl(FileFd, F_SETLKW, &amp;param)
  </code></pre>
  
  <p>based on the two variable <code>param.l_start</code> and <code>param.l_len</code> we used to decide on which offset to take the lock. by setting these two variable to <strong>0</strong> help us to take lock from file starting to end in <strong>Linux</strong>.</p>
  
  <p>I expecting same behaviour in Windows using <a href="http://msdn.microsoft.com/en-us/library/windows/desktop/aa365203(v=vs.85).aspx" rel="nofollow">LockFileEx</a> or any other API. </p>
  
  <p><em>Is it possible to do full file lock in windows using LockFileEx ?</em></p>
  
  <p><em>Is there any other API available to do full file lock ?</em></p>
  
  <p><em>Is there any other method to do the same in windows?</em></p>
  
  <p>Thanks.</p>