procedure TForm1.Button1Click(Sender: TObject);
var
  phost : PHostEnt;
  s : pchar;
  buf:string;
begin
  {获得计算机名}
  getmem(s,1024);
  try
    strpcopy(s,edit1.text);
    phost := GetHostByName(s);
    if phost<>nil then
        begin
            buf:=phost^.h_Name;
            edit2.text:=copy(buf,1,pos('.',buf)-1);
        end
    else
        edit2.text:='Failed';
  finally
    freemem(s);
  end;
end;

解决方案 »

  1.   

    http://www.codelphi.com/channel/jsjn/list.asp?ano=24
      

  2.   


    procedure TForm1.Button1Click(Sender: Tobject);
    var
      ComputerName:pchar;
      Size: Cardinal;
      Re:Boolean;
    begin
    Size := MAX_COMPUTERNAME_LENGTH+1;
      Getmem(ComputerName,Size);
      {retrieve computer name}
      Re:=GetComputerName(ComputerName, Size);
      if re then
        Edit1.Text := StrPas(Computername)
      else Showmessage('Computer Name Not Found');
      Freemem(ComputerName);
    end;
      

  3.   

    关于取本地计算机的IP地址及计算机名的看法 
    大体步骤:
       1:先创建一个窗体。
       2:将一个label1,一个按扭 bitbtn1,bittn2 放入窗体。
       3:对BITBTN1  的ONCLICK 事件编程。
        编程如下:
        .......
        var
          tp:ttcpclient;
          strname,straddr:string;
        begin
           tp:=ttcpclient.create(self);
           tp.close;
           tp.open;
           strname:=tp.LocalHostName;
           straddr:=tp.LocalHostAddr;
           label1.caption:=strname+' IP: '+straddr;
           tp.close;
        end;
        
        bitbtn2 的onclick 事件如下:
         .....
         begin
             close;
         end;    如上程序,运行后,单击bitbtn1 按扭, label1 将显示出本地计算机的 IP地址 和计算机名。(完毕)    
      

  4.   

    GetComputerName(ComputerName, Size);这个函数是需要使用socket组件的吧?
      

  5.   

    var
       name:pchar;
       size:dword;
    begin
       size:=255;   
       getmem(name,size);;
       getcomputername(name,size);
       showmessage(name);
       freemem(name);
       end;
      

  6.   

    wVersionRequested := MAKEWORD( 2, 0 );
     err := WSAStartup( wVersionRequested, wsaData );
     gHostName:='default';
     if ( err = 0 ) then
     if getHostName(HostName,10)=0 then
     gHostName := HostName;
    WSACleanup();
    //原始代码,经过试用的哦。
    记得在uses里面 家,winsock.
      

  7.   

    wVersionRequested := MAKEWORD( 2, 0 );
     err := WSAStartup( wVersionRequested, wsaData );
     gHostName:='default';
     if ( err = 0 ) then
     if getHostName(HostName,10)=0 then
     gHostName := HostName;
    WSACleanup();
    //原始代码,经过试用的哦。
    记得在uses里面 家,winsock.
      

  8.   

    使用tcp组件,localhost方法!很简单!