我有一段代码,使用 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-64位上,以上工作正常。 但在Win8-64位上,相同的DLL文件将在CopyMemory上抛出访问冲突。 CopyMemory是在WinAPI.windows单元中实现的。
是这样的。
procedure CopyMemory(Destination: Pointer; Source: Pointer; Length: NativeUInt);
begin
Move(Source^, Destination^, Length);
end;
有什么想法吗?谢谢。
最佳答案
此时:
Pointer(LongWord(Self.FMemory) + Self.FPosition)
您将 64 位指针截断为 32 位。因此出现了访问冲突。相反,你需要
Pointer(NativeUInt(Self.FMemory) + Self.FPosition)
您的代码在 Win7 上同样损坏,但不知何故,您很不幸,仅使用地址 < 4GB 的指针运行此代码。
您应该运行一些自上而下的内存分配测试来清除任何其他此类错误。
关于delphi - CopyMemory 导致 Win8 上的访问冲突,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15930608/