思路:
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. 命令行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;
解决方案 »
- 我按照网上介绍对QuickReport进行了修改,做了自己的Preview界面,在SDI上可以通过,但在MDI却不行.错误...
- 如何更新这个表?
- 什么数据库支持.表字段个数>255?
- ADO明细表问题
- 哪儿有游戏的半成品,我想下载下来,学习学习,500
- 请教一个很菜的问题
- ActiveX Control 编译问题
- 如何将EDIT中的一段文字加入当前正在编译的DBGRID中的文字后?
- 菜鸟送分系列之一:关于StringGrid
- 下面的这段程序编译时,为何总出现"Unsatisfied forward or external declaration:'TForm1.DownloadFile'"的错误信息?
- 那位高手帮我把这个编译一下,谢谢
- SQL語句問題!!!
不信,你把光标放到它上面,按F1 ... 如果Delphi里有(它任何单元里的)就会有帮助,并告诉你在哪个单元里。
你不是要IP吗,上面就够了。
什么都不用说了吧
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.
Error 3787: You must log on before performing this operation.
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');
Error 3787: You must log on before performing this operation.
我们这里是一个大的片区连成局域网,在连上宽带. 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.