as we know from Delphi 2007 and below the strings are not Unicode
Char is 1 byte
AnsiChar 1 byte
from Delphi 2009 and above the strings are Unicode
Char is 2 byte
AnsiChar 1 byte
when i convert my Delphi code from Delphi6 to Delphi 10.2( unicode string) i faced problem of some functions take PAnsiChar to fill array of Char, but the pointer passed is PChar, in non Unicode Delphi i will not get any error
but i will get error in new Delphi example
procedure TswSocket.SetLocalHost(AHost : AnsiString);
var
HostIpAddress : TIPAddress;
Buffer: PChar;
begin
Buffer:=StrAlloc(Length(AHost)+1);
strpcopy(Buffer,AHost);
Buffer[Length(AHost)]:=#0;
bLocalHost:=StrAlloc(MAXGETHOSTSTRUCT);
hGetLocalHost:=WSAAsyncGetHostByName(Self.hWindow,WM_SOCKETGETHOSTBYNAME,Buffer,bLocalHost,MAXGETHOSTSTRUCT); // Buffer,bLocalHost must be PAnsiChar instead of PChar
WSAAsyncGetHostByName will fill bLocalHost
to fix the error i used
procedure TswSocket.SetLocalHost(AHost : AnsiString);
var
HostIpAddress : TIPAddress;
Buffer,temp_Buffer : PAnsiChar;
begin
Buffer:=AnsiStrAlloc(Length(AHost)+1);
strpcopy(Buffer,AHost);
Buffer[Length(AHost)]:=#0;
temp_Buffer:=AnsiStrAlloc(MAXGETHOSTSTRUCT);
bLocalHost:=StrAlloc(MAXGETHOSTSTRUCT);
hGetLocalHost:=WSAAsyncGetHostByName(Self.hWindow,WM_SOCKETGETHOSTBYNAME,Buffer,temp_Buffer,MAXGETHOSTSTRUCT); // no error : Buffer,temp_Buffer is PAnsiChar
now the result in temp_Buffer( Array of AnsiChar) but we need the result in bLocalHost (array of Char)
is there any function like this copy(PAnsiChar,PChar,size) copy content of PAnsiChar to PChar like array without lost data due the Unicode ,so if temp_Buffer ='some data' then bLocalHost must be ='some data' ?