API函数 GetComputerName 获得计算机名称

解决方案 »

  1.   

    //==============================================================================
    //取得客户端‘主机名’‘IP地址’************************************************
    //==============================================================================
    procedure GetClientInfo(var ClientName, IPAddress: string);
    var WSAData: TWSAData;
        HostEnt: PHostEnt;
    begin
      {no error checking...}
      WSAStartup(2, WSAData);
      SetLength(ClientName, 255);
      Gethostname(PChar(ClientName), 255);
      SetLength(ClientName, StrLen(PChar(ClientName)));
      HostEnt := GetHostByName(PChar(ClientName));
      with HostEnt^ do IPAddress := Format('%d.%d.%d.%d',[Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
      WSACleanup;
    end;