delphi - Delphi 的数据类型不一致?

标签 delphi memory logging

添加以下 Delphi 函数后,我收到有关数据类型未对齐的错误:Project ... 出现错误,并显示消息:“0x77a7d7d8 处的数据类型未对齐”。进程已停止。使用“Step”或“Run”继续。

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

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 及更早版本中为 AnsiChar (SizeOf(Char)=1),但为 WideChar > (SizeOf(Char)=2) 在 Delphi 2009 及更高版本中。

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;

关于delphi - Delphi 的数据类型不一致?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22161516/

相关文章:

delphi - 如何实现从outlook邮件或thunderbird拖放到delphi表单?

delphi - 使用 Firemonkey 中的实时绑定(bind)将选定的组合框项链接回数据集

c# - 执行错误记录的简单方法?

delphi - VirtualStringTree 锁定列 0

json - Delphi: super 对象序列化自定义转换器/逆变器

IOS 应用程序因内存不足而被终止,但未收到内存警告

linux - 如何制定自由策略?

java - 在批处理模式下运行 matlab 时如何增加 java 堆内存大小

java - 错误 StatusLogger Log4j2 找不到日志记录实现。请将 log4j-core 添加到类路径中。使用 SimpleLogger 登录到控制台

java - Apache Commons Logging 的编程配置