获取局域网内机器名

时间:2021-06-16 00:31:14

unit main;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ImgList,ExtCtrls, ComCtrls;

type
  TNetResourceArray = ^TnetResource;
  TForm1 = class(TForm)
    TreeView1: TTreeView;
    ImageList: TImageList;
    StatusBar: TStatusBar;
    procedure FormShow(Sender: TObject);
    procedure TreeView1Click(Sender: TObject);
  private
    { Private declarations }
    procedure GetComputerName; //获取计算机名
  public
    { Public declarations }
  end;
//function NetExecute(aFormClass: TFormClass): string;
var
  Form1: TForm1;
  node: Ttreenode;
  aComputerName : string;//全局变量,存储选中的主机名
implementation

{$R *.dfm}

procedure TForm1.FormShow(Sender: TObject);
var
  NetResource: TNetResource;
  Buf: Pointer;
  Count, BufSize, Res: DWORD;
  lphEnum: THandle;
  p: TNetResourceArray;
  i, j: SmallInt;
  NetworkTypeList: TList;
  my_node_1: Ttreenode;
begin
  node := treeview1.Items.add(Treeview1.topitem, '整个网络');
  node.imageindex := 0;
  treeview1.SetFocus;
  statusbar.panels[0].text := '正在列举域名...,请稍侯';
  statusbar.refresh;
  screen.Cursor := crHourGlass;
  NetworkTypeList := TList.Create;
  //获取整个网络中的文件资源的句柄,lphEnum为返回名柄
  Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
    RESOURCEUSAGE_CONTAINER, nil, lphEnum);
  if Res <> NO_ERROR then exit; //执行失败
  //获取整个网络中的网络类型信息
  Count := $FFFFFFFF; //不限资源数目
  BufSize := 8192; //缓冲区大小设置为8K
  GetMem(Buf, BufSize); //申请内存,用于获取工作组信息
  Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
  if (Res = ERROR_NO_MORE_ITEMS) //资源列举完毕
    or (Res <> NO_ERROR) //执行失败
    then Exit;
  P := TNetResourceArray(Buf);
  for I := 0 to Count - 1 do //记录各个网络类型的信息
  begin
    NetworkTypeList.Add(p);
    Inc(P);
  end;
  //WNetCloseEnum关闭一个列举句柄
  Res := WNetCloseEnum(lphEnum); //关闭一次列举
  if Res <> NO_ERROR then exit;

  for J := 0 to NetworkTypeList.Count - 1 do //列出各个网络类型中的所有工作组名称
  begin //列出一个网络类型中的所有工作组名称
    NetResource := TNetResource(NetworkTypeList.Items[J]^); //网络类型信息
  //获取某个网络类型的文件资源的句柄,NetResource为网络类型信息,lphEnum为返回名柄
    Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
      RESOURCEUSAGE_CONTAINER, @NetResource, lphEnum);
    if Res <> NO_ERROR then break; //执行失败

    while true do //列举一个网络类型的所有工作组的信息
    begin
      Count := $FFFFFFFF; //不限资源数目
      BufSize := 8192; //缓冲区大小设置为8K
      GetMem(Buf, BufSize); //申请内存,用于获取工作组信息
              //获取一个网络类型的文件资源信息,
      Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
      if (Res = ERROR_NO_MORE_ITEMS) //资源列举完毕
        or (Res <> NO_ERROR) //执行失败
        then break;
      P := TNetResourceArray(Buf);
      for I := 0 to Count - 1 do //列举各个工作组的信息
      begin
        my_node_1 := treeview1.Items.addchild(node, StrPAS(P^.lpRemoteName)); //取得一个工作组的名称
        my_node_1.imageindex := 1;
        Inc(P);
      end;
    end;
    Res := WNetCloseEnum(lphEnum); //关闭一次列举
    if Res <> NO_ERROR then break; //执行失败
  end;
  FreeMem(Buf);
  NetworkTypeList.Destroy;
  screen.Cursor := crDefault;
  statusbar.panels[0].text := '';
  statusbar.refresh;
end;

procedure TForm1.GetComputerName;
var
  NetResource: TNetResource;
  Buf: Pointer;
  Count, BufSize, Res: DWORD;
  ind: integer;
  lphEnum: THandle;
  Temp: TNetResourceArray;
  groupname : string;
  my_node: Ttreenode;
  my_node_2: ttreenode;
begin
  screen.Cursor := crHourGlass;
  statusbar.panels[0].text := '正在列举组名...,请稍侯';
  statusbar.refresh;
  my_node := treeview1.Selected;
  groupname := treeview1.Selected.Text;
  FillChar(NetResource, SizeOf(NetResource), 0); //初始化网络层次信息
  NetResource.lpRemoteName := @GroupName[1]; //指定工作组名称
  NetResource.dwDisplayType := RESOURCEDISPLAYTYPE_SERVER; //类型为服务器(工作组)
  NetResource.dwUsage := RESOURCEUSAGE_CONTAINER;
  NetResource.dwScope := RESOURCETYPE_DISK; //列举文件资源信息
  //获取指定工作组的网络资源句柄
  Res := WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_DISK,
    RESOURCEUSAGE_CONTAINER, @NetResource, lphEnum);
  if Res <> NO_ERROR then Exit; //执行失败
  while True do //列举指定工作组的网络资源
  begin
    Count := $FFFFFFFF; //不限资源数目
    BufSize := 8192; //缓冲区大小设置为8K
    GetMem(Buf, BufSize); //申请内存,用于获取工作组信息
   //获取计算机名称
    Res := WNetEnumResource(lphEnum, Count, Pointer(Buf), BufSize);
    if Res = ERROR_NO_MORE_ITEMS then break; //资源列举完毕
    if (Res <> NO_ERROR) then Exit; //执行失败
    Temp := TNetResourceArray(Buf);
    for Ind := 0 to Count - 1 do //列举工作组的计算机名称
    begin
      my_node_2 := treeview1.Items.AddChild(my_node, Temp^.lpRemoteName + 2);
      my_node_2.imageindex := 2;
      Inc(Temp);
    end;
  end;
  Res := WNetCloseEnum(lphEnum); //关闭一次列举
  if Res <> NO_ERROR then exit; //执行失败
  FreeMem(Buf);
  screen.Cursor := crDefault;
  statusbar.panels[0].text := '';
  statusbar.refresh;
end;

procedure TForm1.TreeView1Click(Sender: TObject);
begin
  if treeview1.selected.level = 1 then
  begin
    if treeview1.Selected.count = 0 then
    //添加计算机名
      GetComputerName;
end;
end;

end.