I loaded up some code from Delphi and when I compile it inside Delphi 2010 I get an E2010 Incompatible types: 'Char' and 'AnsiChar'.
我从Delphi中加载了一些代码,当我在Delphi 2010中编译它时,我得到了一个E2010不兼容的类型:“Char”和“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;
1 个解决方案
#1
3
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.
在Delphi 2009和以后的版本中,(P)Char是(P)WideChar的别名,而在早期版本中,它是(P)AnsiChar的别名。这就是为什么会出现编译错误。
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.
MultiByteToWideChar()的第三个参数期望在Delphi的所有版本中都有PAnsiChar。简单地把PChar改成PAnsiChar。考虑到StrA是AnsiString而不是String(在D2009+中是UnicodeString的别名),所以它们将匹配。
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:
也就是说,您应该使用CP_ACP常量而不是GetACP()函数,在调用MultiByteToWideChar()之前删除冗余赋值(和转换),删除不必要的字符索引,并删除不必要的空终止符处理:
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;
With that said, don't use WideString
in D2009+ for non-ActiveX work. UnicodeString
is more efficient.
也就是说,不要在D2009+中使用WideString来进行非activex工作。UnicodeString更为高效。
function TFKirimEmail.ChAnsiToWide(const StrA: AnsiString): UnicodeString;
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:
最后,由于您将CodePage设置为ACP, dwFlags参数设置为mb_precomposition(如果没有指定其他标志,这是默认设置),您可以删除所有这些代码,让RTL为您处理转换,因为它在内部默认情况下使用了相同的设置:
function TFKirimEmail.ChAnsiToWide(const StrA: AnsiString): WideString;
begin
Result := WideString(StrA);
end;
Or:
或者:
function TFKirimEmail.ChAnsiToWide(const StrA: RawByteString): UnicodeString;
begin
Result := UnicodeString(StrA);
end;
In which case, your ChAnsiToWide()
function becomes redundant and can be eliminated completely.
在这种情况下,您的ChAnsiToWide()函数变得多余,并且可以完全消除。
#1
3
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.
在Delphi 2009和以后的版本中,(P)Char是(P)WideChar的别名,而在早期版本中,它是(P)AnsiChar的别名。这就是为什么会出现编译错误。
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.
MultiByteToWideChar()的第三个参数期望在Delphi的所有版本中都有PAnsiChar。简单地把PChar改成PAnsiChar。考虑到StrA是AnsiString而不是String(在D2009+中是UnicodeString的别名),所以它们将匹配。
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:
也就是说,您应该使用CP_ACP常量而不是GetACP()函数,在调用MultiByteToWideChar()之前删除冗余赋值(和转换),删除不必要的字符索引,并删除不必要的空终止符处理:
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;
With that said, don't use WideString
in D2009+ for non-ActiveX work. UnicodeString
is more efficient.
也就是说,不要在D2009+中使用WideString来进行非activex工作。UnicodeString更为高效。
function TFKirimEmail.ChAnsiToWide(const StrA: AnsiString): UnicodeString;
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:
最后,由于您将CodePage设置为ACP, dwFlags参数设置为mb_precomposition(如果没有指定其他标志,这是默认设置),您可以删除所有这些代码,让RTL为您处理转换,因为它在内部默认情况下使用了相同的设置:
function TFKirimEmail.ChAnsiToWide(const StrA: AnsiString): WideString;
begin
Result := WideString(StrA);
end;
Or:
或者:
function TFKirimEmail.ChAnsiToWide(const StrA: RawByteString): UnicodeString;
begin
Result := UnicodeString(StrA);
end;
In which case, your ChAnsiToWide()
function becomes redundant and can be eliminated completely.
在这种情况下,您的ChAnsiToWide()函数变得多余,并且可以完全消除。