思路:
1. 命令行net view可以获得局域网所有机器名
3. 程序中创建一个HostName.bat文件,写入
   @echo off
   net view>c:\temp.txt
4. 在程序中运行这个HostName.bat,运行结果就保存到了c:\temp.txt
   winexec('HostName.bat',sw_hide);
5. 程序中读出c:\temp.txt,放到一个memo中,就可以显示所有机器名
   Memo1.lines.LoadFormFile('c;\temp.txt');6. 通过机器名得到IP地址 :
interface
uses winsock;function procedure TForm1.nametoip(name:string):string;
var
WSAData: TWSAData;
HostEnt: PHostEnt;
begin
result:='';
WSAStartup(2, WSAData);
HostEnt := gethostbyname(PChar(name));
if HostEnt <> nil then
begin
with HostEnt^ do
result:= Format('%d.%d.%d.%d', [Byte(h_addr^[0]), Byte(h_addr^[1]), Byte(h_addr^[2]), Byte(h_addr^[3])]);
end;
WSACleanup;
end; 

解决方案 »

  1.   

    不要转移话题,我早就看过这个东东了,我是问为什么编译器提示 未说明标示符TNetResourceArray
      

  2.   

    对不起,我在Delphi中没有找到任何关于TNetResourceArray的定义。
    不信,你把光标放到它上面,按F1 ... 如果Delphi里有(它任何单元里的)就会有帮助,并告诉你在哪个单元里。
    你不是要IP吗,上面就够了。
      

  3.   

    还有,TWSAData ,delpji液提示找不到,怎么回事呀
      

  4.   

    你在命令行状态下输入net view>c:\temp.txt,打开c:\temp.txt看一下...
    什么都不用说了吧
    var myfile:textfile;
    begin
     assign(myfile,'c:\aaa.bat');
     rewrite(myfile);
     try
      writeln(myfile,'@echo off');
      writeln(myfile,'net view>c:\temp.txt');
     finally
      closefile(myfile);
     end;
     winexec('c:\aaa.bat',sw_hide);
     Memo1.lines.LoadFormFile('c;\temp.txt');
    end;把光标放到TWSAData 上按F1,就出现下面的内容:(用的时候,uses winsock;)
    TWSAData 
    Unit: IdWinsockTWSAData = WSAData;DescriptionThe text for this type has been generated automatically. This means that it is not documented.
      

  5.   

    为什么memo里得到这种东西?
    Error 3787: You must log on before performing this operation.
      

  6.   

    另外,上面那个TForm1.nametoip(name:string):string; 怎么用呀?给出应用的事例好吗?
      

  7.   

    纠正两个错误:
    function procedure TForm1.nametoip(name:string):string; 应该是
    function TForm1.nametoip(name:string):string;Memo1.lines.LoadFormFile('c;\temp.txt'); 应该是
    Memo1.lines.LoadFormFile('c:\temp.txt');————————————————————————————
    先不管Memo,你运行 net view>c:\temp.txt
    你打开c:\temp.txt看,内容对不对,应该如下:
    服务器名称              注释
    \\HOST1                Host-me
    \\HOST2                dba
    \\HOST3                Host3
    \\ORACLE-CHARLIE       Oracle-server
    命令成功完成。然后Memo1.lines.LoadFormFile('c:\temp.txt'); Memo里的内容应该和c:\temp.txt里的一样啊。把上面的服务器名称去掉\\,当成参数调用nametoip就可以了,如下:
    Edit1.text:='The IP of HOST1 is '+nametoip('HOST1');
      

  8.   

    对呀,打开一看是  
    Error 3787: You must log on before performing this operation.
      

  9.   

    代码没问题了,可能是我们的网络有问题.
    我们这里是一个大的片区连成局域网,在连上宽带. temo.txt得到如下:Error 6118: The computer(s) sharing resources in this workgroup cannot be located. The computer(s) might have been restarted. Wait a few minutes, and then try again. If the problem persists, make sure your network-adapter settings are correct.