添加以下 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/