子兮子兮 子兮子兮

No can, but will.

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

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

SendMessage 注意事项

设置消息长度区别

  • Delphi 2006 设置消息长度:

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

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

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

设置消息内容区别

  • Delphi 2006 设置消息内容:

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

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

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

接收消息注意事项

获取 TWMCopyData 字符串内容区别

  • Delphi 2006 获取消息内容:

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

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

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

    或者直接使用 PChar

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

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

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

或者:

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



我的博客即将同步至腾讯云+社区,邀请大家一同入驻:https://cloud.tencent.com/developer/support-plan?invite_code=wh4u6zpyhe1d