CopyMemory导致Win8上的访问冲突

时间:2013-04-10 15:50:40

标签: delphi 64-bit

我有一段代码使用Delphi XE3编译成64位COM DLL。

function TRPMFileReadStream.Read(var Buffer; const Count: Longint): Longint;
begin
  if ((Self.FPosition >= 0) and (Count > 0)) then
  begin
    Result := Self.FSize - Self.FPosition;
    if ((Result > 0) and (Result >= Count)) then
    begin
      if (Result > Count) then
      begin
        Result := Count;
      end;
      CopyMemory(
        Pointer(@Buffer),
        Pointer(LongWord(Self.FMemory) + Self.FPosition),
        Result
      );
      Inc(Self.FPosition, Result);
      Exit;
    end;
  end;
  Result := 0;
end;

在Win7-64bit上,以上工作正常。 但在Win8-64bit上,相同的DLL文件将在CopyMemory上抛出Access Violation。 CopyMemory在WinAPI.windows单元中实现。

就是这样。

procedure CopyMemory(Destination: Pointer; Source: Pointer; Length: NativeUInt);
begin
  Move(Source^, Destination^, Length);
end;

有什么想法吗?感谢。

2 个答案:

答案 0 :(得分:7)

此时:

Pointer(LongWord(Self.FMemory) + Self.FPosition)

将64位指针截断为32位。因此访问违规。相反,你需要

Pointer(NativeUInt(Self.FMemory) + Self.FPosition)

你的代码在Win7上也是如此,但不知怎的,你运气不好,只用地址<指针运行这段代码。 4GB。

您应该运行一些自上而下的内存分配测试来清除任何其他此类错误。

答案 1 :(得分:4)

David指出了问题的根源 - 你的指针类型转换对于64位是错误的。更好的解决方案是使用指针算法,让编译器为您处理指针大小:

CopyMemory(
  @Buffer,
  PByte(Self.FMemory) + Self.FPosition,
  Result
);

或者:

Move(
  (PByte(Self.FMemory) + Self.FPosition)^,
  Buffer,
  Result
);