2014-03-04 78 views
1

添加以下德尔福功能后,我收到有关数据类型错位的错误: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 
+0

能否请您发布的内容是在地址'0x77a7d7d8'?也许堆栈跟踪? –

+1

我无法使用您的代码重现您的问题(复制/直接粘贴到D2007项目中)。你使用的是什么Delphi版本和操作系统? –

+1

IIRC,Delphi的版本非常重要,因为简单的类型转换并不总是有效。在旧版Delphi中,您需要使用StrPas。另一个有用的信息是你传递的msg的内容。 – Graymatter

回答

5

CharAnsiChar在2007年德尔福(SizeOf(Char)=1)以及更早版本,但是是WideCharSizeOf(Char)=2)在2009年和以后德尔福。

GetTempPath()预期的第一个参数指定数目的字符,你的缓冲区可以容纳的,但你指定字节数代替。

在德尔福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; 
+0

我实际上已经知道我的C++ Win32 API时代的'SomeFuncA'和'SomeFuncW'情况,但我只知道这适用于(我尽管是)*整型*类型'Char'('AnsiChar' vs'WideChar')。谢谢。 – magnus