상세 컨텐츠

본문 제목

[네트웍/인터넷] 네트워크 컴퓨터가 존재하는지 검사하기

카테고리 없음

by [롯벨] 2023. 9. 9. 08:13

본문

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation
{$R *.DFM}

function RemoteComputerExists(const ComputerName : String) : Boolean;
{
  Returns True if the remote computer ComputerName exists on the network.
  ComputerName must be of the form "\Name".  The function is not case-sensitive.
  Be warned that this may take a *long* time to return.

  Based on code supplied by:
  Michael P. Bobowski
  Atled Engineering Group
  Milwaukee, WI
  <mbobo@execpc.com>
}
  function Enumerate(lpnr : PNetResource; const ComputerName : String): Boolean;
  type
    TNetResourceArray = array[0..16383] of TNetResource;
    PNetResourceArray = ^TNetResourceArray;
  var
    hEnum: THandle;
    BufferSize, NumEntries, Entry: Integer;
    lpnrLocalc: PNetResourceArray;
  begin
    Result := False;
    if NO_ERROR <> WNetOpenEnum(RESOURCE_GLOBALNET, RESOURCETYPE_ANY, 0 {Usage: All resources}, lpnr, hEnum) then
    begin
      Exit;
    end;
    BufferSize := 16384; {16 kB}
    {Get as many entries as possible; NumEntries will be set to the number actually read (if successfull)}
    NumEntries := $FFFFFFFF;
    lpnrLocalc := AllocMem(BufferSize);
    repeat
      case WNetEnumResource(hEnum, NumEntries, lpnrLocalc, BufferSize) of
      NO_ERROR : begin
        for Entry := 0 to (NumEntries - 1) do
        begin
          {lpnrLocalc^[Entry].dwScope will be RESOURCE_GLOBALNET since that is what we asked for}
          if 0 = ANSICompareText(lpnrLocalc^[Entry].lpRemoteName,ComputerName) then
          begin
            {lpnrLocalc^[Entry].dwDisplayType = RESOURCEDISPLAYTYPE_SERVER should also be True}
            {RESOURCEUSAGE_CONTAINER = (lpnrLocalc^[Entry].dwUsage and RESOURCEUSAGE_CONTAINER) should also be True}
            Result := True;
            break;
          end; {then}
          {ResourceType is irrelevant}

          if (lpnrLocalc^[Entry].dwDisplayType = RESOURCEDISPLAYTYPE_DOMAIN) or
             (lpnrLocalc^[Entry].dwDisplayType = RESOURCEDISPLAYTYPE_NETWORK) then
          begin
            {Must recurse}
            if RESOURCEUSAGE_CONTAINER = (lpnrLocalc^[Entry].dwUsage and RESOURCEUSAGE_CONTAINER) then
            begin
              {Recursion possible}
              Result := Enumerate(@(lpnrLocalc^[Entry]), ComputerName);
              if Result then
                break;
            end; {then}
          end; {then}
        end; {for}
      end; {NO_ERROR}

      ERROR_MORE_DATA : begin
        {The buffer is too small for even one entry: increase the buffer...}
        FreeMem(lpnrLocalc, BufferSize);
        BufferSize := 2*BufferSize;
        lpnrLocalc := AllocMem(BufferSize);
        {...and try again}
        end; {ERROR_MORE_DATA}
      else
      {
      ERROR_NO_MORE_ITEMS  (Enumeration is complete)
      ERROR_INVALID_HANDLE (The handle given by the hEnum parameter is not valid)
      ERROR_NO_NETWORK     (No network is present)
      ERROR_EXTENDED_ERROR (use WNetGetLastError for details)
      }
         break;
      end; {case}
    until Result;

    FreeMem(lpnrLocalc, BufferSize);
    WNetCloseEnum(hEnum);
  end; {Enumerate}

begin
  {Start enumeration at the root of the network}
  Result := Enumerate(Nil, ComputerName);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  // Edit.Text에 컴퓨터 이름을 사용자가 입력함
  if Pos('\', Edit1.Text) = 0 then
    Edit1.Text := '\'+Edit1.Text;

  if RemoteComputerExists(Edit1.Text) then
    ShowMessage('네트워크 컴퓨터가 존재합니다 ')
  else
    ShowMessage('네트워크 컴퓨터가 존재하지 않습니다 ');
end;

end.