Delphi 2010错误E2010不兼容类型:'AnsiChar'和'Char'

时间:2022-03-01 16:12:51

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

我从Delphi杂志上下载了一些旧代码,当我在Delphi 2010中编译它时,我得到了一个E2010不兼容的类型:“AnsiChar”和“Char”。

How do I resolve this error?

如何解决这个错误?

pAddr := inet_ntoa(AddrIn.sin_addr);

pAddr:= inet_ntoa(AddrIn.sin_addr);

pAddr defined as PChar
inet_ntoa is a function that returns PAnsiChar

pAddr定义为PChar inet_ntoa是一个返回PAnsiChar的函数。

2 个解决方案

#1


3  

Use an AnsiString and a String to safely perform the necessary casts.

使用一个AnsiString和一个字符串来安全地执行必要的转换。

MyAnsiString := AnsiString(inet_ntoa(AddrIn.sin_addr));
MyString := String(MyAnsiString);
pAddr := PChar(MyString);

#2


2  

That depends on what you're trying to do with it. Are you using the address yourself, or are you passing it to external code?

这取决于你要怎么处理它。您是自己使用地址,还是将其传递给外部代码?

If you're using it yourself, try thoiz_vd's answer. As a general rule, keep as much of your internal string processing on the string type as you can. It'll save you a lot of hassle.

如果你自己使用它,试试thoiz_vd的答案。一般来说,尽可能多地保留字符串类型的内部字符串处理。这样可以省去很多麻烦。

On the other hand, if you're passing it to an external routine, like something in the Windows API, you have to make sure the data is in the format that the API is expecting. This is a bit less clear-cut than the first case, because when Delphi transitioned from AnsiString to UnicodeString as the fundamental string type, they redid a lot of the winapi headers in the Windows unit to resolve to equivalent widechar versions of the routines that took strings.

另一方面,如果将其传递给外部例程,比如Windows API中的某些内容,则必须确保数据符合API所期望的格式。这比第一个例子要简单一些,因为当Delphi从AnsiString过渡到UnicodeString作为基本的字符串类型时,他们重新在Windows单元中重新做了很多winapi头,以解析使用字符串的例程的等效widechar版本。

So check what you're sending it to. If it requires a PChar, use thoiz_vd's answer. But if it's expecting a PAnsiChar, redeclare pAddr as PAnsiChar.

检查一下你发送的是什么。如果需要PChar,请使用thoiz_vd的答案。但是如果它期望一个PAnsiChar,则重新声明pAddr为PAnsiChar。

#1


3  

Use an AnsiString and a String to safely perform the necessary casts.

使用一个AnsiString和一个字符串来安全地执行必要的转换。

MyAnsiString := AnsiString(inet_ntoa(AddrIn.sin_addr));
MyString := String(MyAnsiString);
pAddr := PChar(MyString);

#2


2  

That depends on what you're trying to do with it. Are you using the address yourself, or are you passing it to external code?

这取决于你要怎么处理它。您是自己使用地址,还是将其传递给外部代码?

If you're using it yourself, try thoiz_vd's answer. As a general rule, keep as much of your internal string processing on the string type as you can. It'll save you a lot of hassle.

如果你自己使用它,试试thoiz_vd的答案。一般来说,尽可能多地保留字符串类型的内部字符串处理。这样可以省去很多麻烦。

On the other hand, if you're passing it to an external routine, like something in the Windows API, you have to make sure the data is in the format that the API is expecting. This is a bit less clear-cut than the first case, because when Delphi transitioned from AnsiString to UnicodeString as the fundamental string type, they redid a lot of the winapi headers in the Windows unit to resolve to equivalent widechar versions of the routines that took strings.

另一方面,如果将其传递给外部例程,比如Windows API中的某些内容,则必须确保数据符合API所期望的格式。这比第一个例子要简单一些,因为当Delphi从AnsiString过渡到UnicodeString作为基本的字符串类型时,他们重新在Windows单元中重新做了很多winapi头,以解析使用字符串的例程的等效widechar版本。

So check what you're sending it to. If it requires a PChar, use thoiz_vd's answer. But if it's expecting a PAnsiChar, redeclare pAddr as PAnsiChar.

检查一下你发送的是什么。如果需要PChar,请使用thoiz_vd的答案。但是如果它期望一个PAnsiChar,则重新声明pAddr为PAnsiChar。