title: Duplicate file handle to avoid Sharing Violation Error Message
body: <p>I´m trying to work on a incremental backup system and when I run my script, I always get the "Cannot open file due to sharing violation" as the file I´m trying to open to do the incremental backup is already in use by another process.
  I know I can kill this process, free up the file and do the incremental backup, but that´s something I really have to avoid.
  I´ve read that with the Win32 API I could duplicate the file handler, so how should I do.</p>
  
  <p>Here is a piece of my code:</p>
  
  <pre><code>FILE *GetFileHandle(WIN32_FIND_DATA *pWfdStruct, BOOL bWrite){
  
  FILE *fFile;
  DWORD nGLE;
  
  fFile = fopen(pWfdStruct-&gt;cFileName, "rb");
  if (!fFile)
  {
      nGLE = GetLastError();
  
      if (nGLE == ERROR_SHARING_VIOLATION)    // 32
      {
          char szCurDir[8192];
  
          GetCurrentDirectory(8192, szCurDir);
          ODS("WARN: cannot open %s file due to sharing violation [fRenameFile: %s\\%s]\n",
              bWrite ? "dst" : "src", szCurDir, pWfdStruct-&gt;cFileName);
          return 0;
      }
  
      if (nGLE == ERROR_ACCESS_DENIED)        // 5
      {
          char szCurDir[8192];
  
          GetCurrentDirectory(8192, szCurDir);
          ODS("WARN: cannot open %s file, access denied [fRenameFile: %s\\%s]\n",
              bWrite ? "dst" : "src", szCurDir, pWfdStruct-&gt;cFileName);
          return 0;
      }
  
      if (nGLE == ERROR_FILE_NOT_FOUND)       // 2
      {
          char szCurDir[8192];
  
          GetCurrentDirectory(8192, szCurDir);
          ODS("WARN: cannot open %s file, file not present [fRenameFile: %s\\%s]\n",
              bWrite ? "dst" : "src", szCurDir, pWfdStruct-&gt;cFileName);
          return 0;
      }
  
      char szCurDir[8192];
      GetCurrentDirectory(8192, szCurDir);
  
      if (bWrite)
      {
          ODS("WARN: cannot open dst file [fRenameFile: %s\\%s] [GLE: %d]\n",
              szCurDir, pWfdStruct-&gt;cFileName, nGLE);
          return 0;
      }
  
      ODS("WARN: cannot open src file [fRenameFile: %s\\%s] [GLE: %d] trying alt name [%s]\n",
          szCurDir, pWfdStruct-&gt;cFileName, nGLE, pWfdStruct-&gt;cAlternateFileName);
      ReportSystemError("GetFileHandle", nGLE);
  
      __try
      {
          if (pWfdStruct-&gt;cAlternateFileName[0])
          {
              fFile = fopen(pWfdStruct-&gt;cAlternateFileName, "rb");
          }
      }
      __except(EXCEPTION_EXECUTE_HANDLER)
      {
          ODS("Exception caught\n");  // give up
      }
  
      if (!fFile)
      {
          nGLE = GetLastError();
          ReportSystemError("GetFileHandle 2nd try", nGLE);
          FATALODS("FATAL error, cannot open src file [%s] [GLE: %d]", pWfdStruct-&gt;cFileName, nGLE);
      }
      else
      {
          ODS("File: %s open success\n", pWfdStruct-&gt;cAlternateFileName);
      }
  }
  return fFile;} // GetFileHandle
  </code></pre>
  
  <p>Could you please help me?</p>