我有一段代码,使用Delphi编译成64位的COM。
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上抛出访问冲突。在WinAPI.windows单元中实现了CopyMemory。
是这样的。
procedure CopyMemory(Destination: Pointer; Source: Pointer; Length: NativeUInt);
begin
Move(Source^, Destination^, Length);
end;有什么想法吗?谢谢。
发布于 2013-04-10 23:52:36
此时:
Pointer(LongWord(Self.FMemory) + Self.FPosition)您将64位指针截断为32位。因此出现了访问冲突。相反,您需要
Pointer(NativeUInt(Self.FMemory) + Self.FPosition)你的代码在Win7上同样崩溃了,但不知何故你很不幸,只用地址小于4 4GB的指针运行过这段代码。
您应该运行一些自上而下的内存分配测试,以清除任何其他此类错误。
发布于 2013-04-11 03:39:41
David指出了你问题的根源--你的指针类型转换对于64位来说是错误的。更好的解决方案是使用指针算法,让编译器为您处理指针大小:
CopyMemory(
@Buffer,
PByte(Self.FMemory) + Self.FPosition,
Result
);或者:
Move(
(PByte(Self.FMemory) + Self.FPosition)^,
Buffer,
Result
);https://stackoverflow.com/questions/15930608
复制相似问题