我有一段代码,使用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;
有任何想法吗?谢谢。
CopyMemory
,因为我觉得指针比无类型引用参数更容易理解,并且我每天都在多种语言中工作,所以拥有一个通用的API非常有帮助。此外,由于API直接将所有内容转发到内置函数,因此任何人选择哪个函数几乎没有区别。 - Rob KennedyCopyMemory
是内联的,编译器会发出相同的代码。因此,这取决于个人选择。 - David Heffernan