我C是不好,不过汇编不错。我不明白的是你说的这句话:"delphi中传入的参数原封不动的传入到MessageBox函数里,结果照样出错。您知道,MessageBox函数是必然会将该值作为字符缓冲区的首址"。怎么我编同样的程序就没有任何错误,我想只可能是您有粗心的错误。另外,fxn(PChar(String('a constant string')))和直接写fxn('a constant string')是没有区别的,如果有一个常量const s = 'a constant string',或者const s:PChar = 'a constant string',或者const s: array [0..255] of char = 'a constant string',则fxn(s)都是正确的。只有当s的类型为显式的string({$H+}时等同于AnsiString),需要用PChar(s)的形式强制转换(注:改转换的目的仅是为了骗过编译器,因为s作为指针时其目标地址的布局和PChar是完全兼容的)。
procedure TForm1.Button1Click(Sender: TObject);
begin
// asm int 3; end; // comment/uncomment this line to trigger breakpoint here
Windows.MessageBox(Handle,sConst,'Test',MB_OK);
Windows.MessageBox(Handle,pcConst,'Test',MB_OK);
Windows.MessageBox(Handle,caConst0,'Test',MB_OK); // cause incorrect tail
Windows.MessageBox(Handle,caConst1,'Test',MB_OK);
Windows.MessageBox(Handle,PChar(strConst),'Test',MB_OK);
Windows.MessageBox(Handle,@cConst,'Test',MB_OK); // cause incorrect tail
Windows.MessageBox(Handle,@nullChar,'Test',MB_OK);
Windows.MessageBox(Handle,nil,'Test',MB_OK);
end;