【发布时间】:2021-02-10 02:41:02
【问题描述】:
奇怪的是,这段在 Delphi X32 下运行良好的代码在 x64 编译时却无法运行。 对 GetLogicalProcessorInformation 的第一次调用只是返回代码 988(对内存位置的访问无效),我想知道为什么,以及可以采取什么措施来克服这个问题。
function GetLogicalProcessorInfo : TLogicalProcessorInformation;
var
i : Integer;
ReturnLength: DWORD;
Buffer : array of TSystemLogicalProcessorInformation;
begin
result.LogicalProcessorCount := 0;
result.NumaNodeCount := 0;
result.ProcessorCoreCount := 0;
result.ProcessorL1CacheCount := 0;
result.ProcessorL2CacheCount := 0;
result.ProcessorL3CacheCount := 0;
result.ProcessorPackageCount := 0;
SetLength(Buffer,256);
if not GetLogicalProcessorInformation(@Buffer[0], ReturnLength) then
begin
if GetLastError = ERROR_INSUFFICIENT_BUFFER then
begin
SetLength(Buffer,ReturnLength div SizeOf(TSystemLogicalProcessorInformation) + 1);
if not GetLogicalProcessorInformation(@Buffer[0], ReturnLength) then
RaiseLastOSError;
end
else
RaiseLastOSError;
end;
SetLength(Buffer, ReturnLength div SizeOf(TSystemLogicalProcessorInformation));
for i := 0 to High(Buffer) do begin
case Buffer[i].Relationship of
RelationNumaNode: Inc(result.NumaNodeCount);
RelationProcessorCore:
begin
Inc(result.ProcessorCoreCount);
result.LogicalProcessorCount := result.LogicalProcessorCount + CountSetBits(Buffer[i].ProcessorMask);
end;
RelationCache:
begin
if (Buffer[i].Cache.Level = 1) then Inc(result.ProcessorL1CacheCount)
else if (Buffer[i].Cache.Level = 2) then Inc(result.ProcessorL2CacheCount)
else if (Buffer[i].Cache.Level = 3) then Inc(result.ProcessorL3CacheCount);
end;
RelationProcessorPackage: Inc(result.ProcessorPackageCount);
else
raise Exception.Create('Error: Unsupported LOGICAL_PROCESSOR_RELATIONSHIP value.');
end;
end;
end;
【问题讨论】: