Question

After adding the following Delphi function, I'm receiving an error regarding data-type misalignment: Project ... faulted with message: 'datatype misalignment at 0x77a7d7d8'. Process Stopped. Use Step or Run to continue.

The function I've added is below. Note that the function actually completes successfully, although only the timestamp is actually written to the file.

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;

EDIT: Added more assembly output.

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
Was it helpful?

Solution

Char is AnsiChar (SizeOf(Char)=1) in Delphi 2007 and earlier, but is WideChar (SizeOf(Char)=2) in Delphi 2009 and later.

GetTempPath() expects the first parameter to specify the number of characters that your buffer can hold, but you are specifying the number of bytes instead.

In Delphi 2007 and earlier, SizeOf(tempFolderChars) and Length(tempFolderChars) will be the same value, but in Delphi 2009 and later they will not be the same. In that latter case, you are telling GetTempPath() that you can accept twice as many characters as you really can.

You need to change SizeOf(tempFolderChars) to Length(tempFolderChars). You also need to pay attention to the return value of GetTempPath(), as it tells you how many characters were actually written into the buffer.

Try this instead:

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top