子兮子兮 子兮子兮

No can, but will.

目录
Delphi 2006 代码升级到 Delphi 11 注意事项
/    

Delphi 2006 代码升级到 Delphi 11 注意事项

SendMessage 注意事项

设置消息长度区别

  • Delphi 2006 设置消息长度:

    copyData.cbData := Length(msgStr) + 1;
    
  • Delphi 11 设置消息长度:

    copyData.cbData := Length(TEncoding.ANSI.GetBytes(msgStr)) + 2;
    

在 Delphi 11 中直接使用 Length(msgStr) 获取消息长度接收时消息内容会不完整!

设置消息内容区别

  • Delphi 2006 设置消息内容:

    StrCopy(copyData.lpData, PChar(msgStr));
    
  • Delphi 11 设置消息内容:

    需要在 uses 代码块中引用 System.AnsiStrings

    System.AnsiStrings.StrCopy(sdata.lpData, PAnsiChar(AnsiString(msgStr)));
    

接收消息注意事项

获取 TWMCopyData 字符串内容区别

  • Delphi 2006 获取消息内容:

    InStr := StrPas(msg.CopyDataStruct^.lpData);
    
  • Delphi 11 获取消息内容:

    需要在 uses 代码块中引用 System.AnsiStrings

    InStr := String(System.AnsiStrings.StrPas(msg.CopyDataStruct^.lpData));
    

    或者直接使用 PChar

    InStr := PChar(msg.CopyDataStruct^.lpData);
    

在 Delphi 11 中接收消息直接使用 Delphi 2006 的代码会报以下错误:

[dcc32 Error] XXX.pas(999): E2251 Ambiguous overloaded call to 'StrPas'
  System.SysUtils.pas(12567): Related method: function StrPas(const PAnsiChar): AnsiString;
  System.SysUtils.pas(12573): Related method: function StrPas(const PWideChar): string;

或者:

[dcc32 Error] XXX.pas(999): E2251 Ambiguous overloaded call to 'StrPas'
  System.AnsiStrings.pas(4122): Related method: function StrPas(const PAnsiChar): AnsiString;
  System.SysUtils.pas(12567): Related method: function StrPas(const PAnsiChar): AnsiString;
  System.SysUtils.pas(12573): Related method: function StrPas(const PWideChar): string;