var
  HostName: array[0..10] of char;
  PHE: PHostEnt;
 I: Integer;
begin
  I := GetHostName(HostName, Sizeof(HostName));
  PHE := GetHostByName(HostName);
end;为什么I的值为-1    为什么HostName的值为?    PHE的值是什么,我应该怎样得到他的值?

解决方案 »

  1.   

    GetHostName和GetHostByName这两个都是Winsock API,需要调用WSAStartup初始化,才能使用,使用完还需要调用WSACleanup;
      

  2.   

    uses
    winsock;
    procedure TForm1.Button1Click(Sender: TObject);
    var
      HostName: array[0..10] of char;
      PHE: PHostEnt;
      wsaDat:WSADATA;
      ConsultResult:String;
      I:Integer;
    begin  I:=WSAStartup(MAKEWORD(2,0),wsaDat);
      if I<>0 then exit;
      I := GetHostName(HostName, Sizeof(HostName));
      if I<>0 then raise Exception.CreateFmt('GetHostName Fail![Socket Error %s]',[WSAGetLastError()]);
      HostName:='sdf';
      PHE := GetHostByName(HostName);
      if phe=nil then raise Exception.CreateFmt('GetHostByName Fail![Socket Error %d]',[WSAGetLastError()]);
      WSACleanup();
      ConsultResult:=Format('%d.%d.%d.%d',
                                  [ord(phe.h_addr^[0]),
                                   ord(phe.h_addr^[1]),
                                   ord(phe.h_addr^[2]),
                                   ord(phe.h_addr^[3])]);
       ShowMessage(ConsultResult);
    end;