数据类型与德尔福不一致



添加以下Delphi函数后,我收到有关数据类型未对齐的错误:Project ... faulted with message: 'datatype misalignment at 0x77a7d7d8'. Process Stopped. Use Step or Run to continue.

我添加的功能如下。请注意,该函数实际上成功完成,尽管实际上只有时间戳写入文件。

procedure Log(msg : String);
var
  tempFolderChars : array [0..MAX_PATH] of Char;
  tempFolder : string;
  logFile : TextFile;
  dt : TDateTime;
begin
  GetTempPath(SizeOf(tempFolderChars), tempFolderChars);
  tempFolder := IncludeTrailingPathDelimiter(String(tempFolderChars));
  dt := Now();
  AssignFile(logFile, tempFolder + 'GenericHolding.txt');
  if FileExists(tempFolder + 'GenericHolding.txt') then
    Append(logFile)
  else
    ReWrite(logFile);
  Write(logFile, FormatDateTime('yyyy-mm-dd hh:nn:ss ', now));
  Write(logFile, msg);
  Write(logFile, #13, #10);
  CloseFile(logFile);
end;

编辑:添加了更多程序集输出。

ntdll.NtQueryInformationProcess:
77BAFAC8 B816000000       mov eax,$00000016
77BAFACD 33C9             xor ecx,ecx
77BAFACF 8D542404         lea edx,[esp+$04]
77BAFAD3 64FF15C0000000   call dword ptr fs:[$000000c0]
77BAFADA 83C404           add esp,$04
77BAFADD C21400           ret $0014
Char

Delphi 2007及更早版本中AnsiCharSizeOf(Char)=1),但在Delphi 2009及以后WideCharSizeOf(Char)=2)。

GetTempPath() 期望第一个参数指定缓冲区可以容纳的字符数,但您改为指定字节数

在 Delphi 2007 及更早版本中,SizeOf(tempFolderChars)Length(tempFolderChars) 的值相同,但在 Delphi 2009 及更高版本中,它们将不同。 在后一种情况下,你是在告诉GetTempPath(),你可以接受两倍于实际数量的字符。

您需要将SizeOf(tempFolderChars)更改为 Length(tempFolderChars) . 您还需要注意 GetTempPath() 的返回值,因为它告诉您实际写入缓冲区的字符数。

试试这个:

procedure Log(msg : String);
var
  tempFolderChars : array [0..MAX_PATH] of Char;
  tempFolder : string;
  len: DWORD;
  ...
begin
  len := GetTempPath(Length(tempFolderChars), tempFolderChars);
  if len = 0 then Exit;
  SetString(tempFolder, tempFolderChars, len);
  tempFolder := IncludeTrailingPathDelimiter(tempFolder);
  ...
end;

最新更新