Delphi 在 windows 7 64 上使用 LockFile

发布于 2024-09-25 01:37:44 字数 2091 浏览 2 评论 0原文

我发现,如果您从 64 位 Windows 7 计算机打开位于共享文件夹中的 32 位服务器上的文件,请读取它,锁定它,然后再次打开它。当您关闭所有打开的句柄时,文件实际上保持打开状态。

具体步骤是:

  1. 将一个长度在 7000 到 10000 字节之间的文件放置在 32 位 Windows 计算机上的共享文件夹中,我们使用的是 Windows Server 2003。

  2. 为 Win32 编译以下代码,以便它在 WOW64 下运行。请注意,为了简单起见,我错过了 try..finally、声明等。
    (请参阅下面的代码片段;当代码位于列表内时,StackOverflow 错误不会正确格式化代码)

  3. 在 64 位 Windows 计算机上运行应用程序。该文件必须位于 32 位计算机上,我们使用 Windows Server 2003,如果该文件位于 64 位服务器上,则不会出现该错误。

  4. 终止您的应用程序。

  5. 如果您现在打开服务器上的计算机管理器(控制面板 -> 计算机管理)并查看文件所在的共享文件夹中打开的文件,您将看到您的文件仍处于打开状态。

这是代码:

procedure CauseFileLockBug(FileName: PChar);
var
  FileHandle1: LongInt;
  FileHandle2: LongInt;
  Buffer: Pointer;
  BytesRead: Cardinal;
begin
  FileHandle1 := CreateFile(
    FileName, 
    GENERIC_READ or GENERIC_WRITE, 
    FILE_SHARE_READ or FILE_SHARE_WRITE, 
    nil, 
    OPEN_EXISTING, 
    FILE_FLAG_RANDOM_ACCESS, 
    0);

  if FileHandle1 > 0 then
  begin
    try
      GetMem(Buffer, 1);

      try
        if ReadFile(FileHandle1, Buffer^, 1, BytesRead, nil) then
        begin
          if LockFile(FileHandle1, 6217, 0, 1, 0) then
          begin
            try
              FileHandle2 := CreateFile(
                FileName, 
                GENERIC_READ or GENERIC_WRITE, 
                FILE_SHARE_READ or FILE_SHARE_WRITE, 
                nil, 
                OPEN_EXISTING, 
                FILE_FLAG_RANDOM_ACCESS, 
                0);

              if FileHandle2 > 0 then
              begin
                CloseHandle(FileHandle2);
              end;
            finally
              UnLockFile(FileHandle1, 6217, 0, 1, 0);
            end;
          end;
        end;
      finally
        FreeMem(Buffer);
      end;
    finally
      CloseHandle(FileHandle1);
    end;
  end;
end;

如果您在第二次打开文件时使用 FILE_FLAG_NO_BUFFERING 标志,或者如果您在锁定文件之前没有读取该文件,则不会出现该问题。

有没有人以前注意到过这个问题或者知道如何在不使用 FILE_FLAG_NO_BUFFERING 的情况下解决它?请注意,只有当 64 位 Windows 客户端在 32 位 Windows 计算机上以这种方式打开文件时才会发生这种情况,而 32 位或 64t 到 64 则不会发生这种情况。

I have found that if you open a file that resides on 32 bit server in a share folder from a 64 bit windows 7 machine, read it, lock it and then open it again. When you close all open handles the file actually remains open.

The exact steps are:

  1. Place a file that is between 7000 and 10000 bytes long in a shared folder on a 32 bit windows machine, we are using Windows Server 2003.

  2. Compile the following code for Win32 so that it runs under WOW64. Please note that I've missed out try..finally, declarations, etc for simplicity.
    (see code fragment below; a StackOverflow bug does not format code correctly when it is inside a list)

  3. Run the application on a 64 bit windows machine. the file must be on a 32 bit machine, we use windows server 2003, the bug does not occur if the file is on a 64 bit server.

  4. Terminate your application.

  5. If you now open up the computer manager on the server (control panel->computer management) and look at the opened files in the share folder where your file resides you will see that your file is still open.

This is the code:

procedure CauseFileLockBug(FileName: PChar);
var
  FileHandle1: LongInt;
  FileHandle2: LongInt;
  Buffer: Pointer;
  BytesRead: Cardinal;
begin
  FileHandle1 := CreateFile(
    FileName, 
    GENERIC_READ or GENERIC_WRITE, 
    FILE_SHARE_READ or FILE_SHARE_WRITE, 
    nil, 
    OPEN_EXISTING, 
    FILE_FLAG_RANDOM_ACCESS, 
    0);

  if FileHandle1 > 0 then
  begin
    try
      GetMem(Buffer, 1);

      try
        if ReadFile(FileHandle1, Buffer^, 1, BytesRead, nil) then
        begin
          if LockFile(FileHandle1, 6217, 0, 1, 0) then
          begin
            try
              FileHandle2 := CreateFile(
                FileName, 
                GENERIC_READ or GENERIC_WRITE, 
                FILE_SHARE_READ or FILE_SHARE_WRITE, 
                nil, 
                OPEN_EXISTING, 
                FILE_FLAG_RANDOM_ACCESS, 
                0);

              if FileHandle2 > 0 then
              begin
                CloseHandle(FileHandle2);
              end;
            finally
              UnLockFile(FileHandle1, 6217, 0, 1, 0);
            end;
          end;
        end;
      finally
        FreeMem(Buffer);
      end;
    finally
      CloseHandle(FileHandle1);
    end;
  end;
end;

The problem goes does not occur if you use the FILE_FLAG_NO_BUFFERING flag when opening the file the second time or if you do not read the file before locking it.

Has anyone noticed this before or know how to solve it, without using the FILE_FLAG_NO_BUFFERING? Please not that it only happens when a 64 bit windows client opens a file in this way on a 32 bit windows machine, it does not occur with 32 to bit or 64t to 64.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

℉絮湮 2024-10-02 01:37:44

好的,问题解决了。

看来 Nod32 x64 是导致该问题的原因。使用通配符 * 将文件夹中的所有可能路径(所有网络路径和映射驱动器)添加到排除列表中,然后重新启动电脑即可修复该问题。

无论如何,感谢您的帮助。

Ok Problem solved.

Seems like Nod32 x64 was causing the issue. Adding all possible paths into the folder (all network paths and mapped drives) to the exclusions list with wildcard * and then rebooting the PC has fixed it.

Anyway thanks for your help.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文