How to convert from PAnsichar to PWidechar?

11,884

You don't want to convert from PAnsiChar to PWideChar. On your Unicode Delphi your PChar maps to PWideChar. But gethostbyname receives PAnsiChar. You need to convert from Unicode to ANSI.

Code it like this:

phe := gethostbyname(PAnsiChar(AnsiString(AIP)));

In other words, convert your string to AnsiString, and then cast as PAnsiChar. Personally I'd declare the AIP parameter to be AnsiString.

procedure TranslateStringToTInAddr(const AIP: AnsiString; var AInAddr);

And then write the call to gethostbyname like so:

phe := gethostbyname(PAnsiChar(AIP));

That untyped var parameter looks dubious to me. I see no compelling reason for its use. What's wrong with declaring it to be of type TIPAddr? Your FillChar is somewhat dubious. How can you use SizeOf on an untyped parameter?

Share:
11,884
Xenon Xe
Author by

Xenon Xe

Updated on June 05, 2022

Comments

  • Xenon Xe
    Xenon Xe almost 2 years

    I am implementing Ping function using windows API in delphi-xe3 from here (http://delphi.about.com/od/internetintranet/l/aa081503a.htm).

    I am having problem with the following function.It displays error incompatible type Pansichar and Pwidechar.I replaced Pchar with PAnsichar now it displays exception 'Error getting IP from HostName'.

    I am testing it with localhost.

    Please guide whats the proper conversion.

    const ADP_IP = '127.0.0.1';
    
    procedure TranslateStringToTInAddr(AIP: string; var AInAddr);
    var
      phe: PHostEnt;
      pac: PChar;
      GInitData: TWSAData;
    begin
      WSAStartup($101, GInitData);
      try
        phe := GetHostByName(PChar(AIP));
        if Assigned(phe) then
        begin
          pac := phe^.h_addr_list^;
          if Assigned(pac) then
          begin
            with TIPAddr(AInAddr).S_un_b do begin
              s_b1 := Byte(pac[0]);
              s_b2 := Byte(pac[1]);
              s_b3 := Byte(pac[2]);
              s_b4 := Byte(pac[3]);
            end;
          end
          else
          begin
            raise Exception.Create('Error getting IP from HostName');
          end;
        end
        else
        begin
          raise Exception.Create('Error getting HostName');
        end;
      except
        FillChar(AInAddr, SizeOf(AInAddr), #0);
      end;
      WSACleanup;
    end;
    
  • Xenon Xe
    Xenon Xe over 10 years
    thanks @David , i did it ur way .now it didnt displayed any error but no message is either shown .i just took the code from the site and tried to compile it ,i m not much familiar with windows api's. so i dont know why its better to use var AIPaddr or something else.
  • David Heffernan
    David Heffernan over 10 years
    Your code doesn't display messages. Anyway, my answer tells you what was wrong with your call to gethostbyname. The code in my answer does that correctly. Please don't expect me to debug the rest of your program. I answered the question that you asked. That's how SO works.
  • Xenon Xe
    Xenon Xe over 10 years
    No i meant i implemented the whole code given in the link i pasted and it dispalys message.delphi.about.com/od/internetintranet/l/aa081503a.htm .Thanks a lot for your help, yes it did removed the error.
  • AlexSC
    AlexSC over 10 years
    @XenonXe: by message I understand your saying that not exceptions are being raised, so what you are saying is that the gethostbyname() function worked properly. However, I agree with @David that the var parameter should be typed as TIpAddr, since your code depends on that to work. The FillChar in the last exception block is misterious to me. Could you explain it?
  • David Heffernan
    David Heffernan over 10 years
    @AlexSC The code was copied verbatim from the linked article which looks dubious to me. Your guess is as good as mine.
  • Xenon Xe
    Xenon Xe over 10 years
    No i cant explain it coz its not my code. i need a ping function and tried this windows function. i m myself trying to understand the code. i need a little guidance here.should i implement it myself or use windows api?
  • Xenon Xe
    Xenon Xe over 10 years
    I think i should post a new question for this guidance .Thanks a lot all .