Delphi 2010错误E2010不兼容的类型:Char'和'AnsiChar' [英] Delphi 2010 Error E2010 Incompatible types: Char' and 'AnsiChar'

查看:96
本文介绍了Delphi 2010错误E2010不兼容的类型:Char'和'AnsiChar'的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Delphi加载了一些代码,当我在Delphi 2010中对其进行编译时,我得到了E2010不兼容的类型:"Char"和"AnsiChar".

I loaded up some code from Delphi and when I compile it inside Delphi 2010 I get an E2010 Incompatible types: 'Char' and 'AnsiChar'.

如何解决此错误?请帮助

How do I resolve this error? help please

function TFKirimEmail.ChAnsiToWide(const StrA: AnsiString): WideString;
var
  nLen: integer;
begin
  Result := StrA;
  if Result <> '' then
  begin
    nLen := MultiByteToWideChar(GetACP(), 1, PChar(@StrA[1]), -1, nil, 0);
    SetLength(Result, nLen - 1);
    if nLen > 1 then
      MultiByteToWideChar(GetACP(), 1, PChar(@StrA[1]), -1, PWideChar(@Result[1]), nLen - 1);
  end;
end;

推荐答案

在Delphi 2009及更高版本中,(P)Char (P)WideChar 的别名,而在早期版本中,它是(P)AnsiChar 的别名.这就是为什么您会遇到编译器错误.

In Delphi 2009 and later, (P)Char is an alias for (P)WideChar, whereas it was an alias for (P)AnsiChar in earlier versions. That is why you are getting a compiler error.

MultiByteToWideChar()的第三个参数在所有版本的Delphi中都要求一个 PAnsiChar .因此,只需将 PChar 更改为 PAnsiChar .考虑到 StrA AnsiString 而不是 String (这是D2009 +中 UnicodeString 的别名),这将很好地工作,因此它们将匹配.

The third parameter of MultiByteToWideChar() expects a PAnsiChar in all versions of Delphi. So simply change PChar to PAnsiChar. Which will work fine considering that StrA is AnsiString and not String (which is an alias for UnicodeString in D2009+), so they will match.

话虽如此,您应该使用 CP_ACP 常量而不是 GetACP()函数,删除对 Result <的冗余分配(和转换)/code>,然后调用 MultiByteToWideChar(),删除不必要的字符索引,并删除不必要的null终止符处理:

That being said, you should be using the CP_ACP constant instead of the GetACP() function, remove the redundant assignment (and conversion) to Result before calling MultiByteToWideChar(), remove unnecessary character indexing, and remove unnecessary null terminator handling:

function TFKirimEmail.ChAnsiToWide(const StrA: AnsiString): WideString;
var
  nLen: integer;
begin
  Result := '';
  nLen := MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PAnsiChar(StrA), Length(StrA), nil, 0);
  if nLen > 0 then
  begin
    SetLength(Result, nLen);
    MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, PAnsiChar(StrA), Length(StrA), PWideChar(Result), nLen);
  end;
end;

话虽如此,请不要在D2009 +中将 WideString 用于非ActiveX工作. UnicodeString 效率更高.

With that said, don't use WideString in D2009+ for non-ActiveX work. UnicodeString is more efficient.

function TFKirimEmail.ChAnsiToWide(const StrA: AnsiString): UnicodeString;

最后,由于您将 CodePage 设置为ACP,并且将 dwFlags 参数设置为 MB_PRECOMPOSED (如果没有其他标志,则为默认设置)指定),您只需消除所有这些代码,然后让RTL为您处理转换,因为默认情况下它在内部使用这些相同的设置:

Lastly, since you are setting the CodePage to ACP and the dwFlags parameter to MB_PRECOMPOSED (which is the default if no other flags are specified), you could just eliminate all this code and let the RTL handle the conversion for you, since it uses those same settings internally by default:

function TFKirimEmail.ChAnsiToWide(const StrA: AnsiString): WideString;
begin
  Result := WideString(StrA);
end;

或者:

function TFKirimEmail.ChAnsiToWide(const StrA: RawByteString): UnicodeString;
begin
  Result := UnicodeString(StrA);
end;

在这种情况下,您的 ChAnsiToWide()函数将变得多余,可以完全消除.

In which case, your ChAnsiToWide() function becomes redundant and can be eliminated completely.

这篇关于Delphi 2010错误E2010不兼容的类型:Char'和'AnsiChar'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆