Einzelnen Beitrag anzeigen

Klaus01
Online

Registriert seit: 30. Nov 2005
Ort: München
5.771 Beiträge
 
Delphi 10.4 Sydney
 
#2

AW: Subnetmask bzw. Broadcastadresse ermitteln

  Alt 27. Jun 2011, 17:09
Delphi-Quellcode:
procedure TForm1.Button1Click(Sender: TObject);
begin
USock.EnumInterfaces(s);
ShowMessage(s);
end;

Hope it helps,
Hokki

*************************************************

unit InterfaceInfo;

interface

uses Windows, Winsock;

function EnumInterfaces(var sInt: string): Boolean;

function WSAIoctl(s: TSocket; cmd: DWORD; lpInBuffer: PCHAR; dwInBufferLen:
  DWORD;
  lpOutBuffer: PCHAR; dwOutBufferLen: DWORD;
  lpdwOutBytesReturned: LPDWORD;
  lpOverLapped: POINTER;
  lpOverLappedRoutine: POINTER): Integer; stdcall; external 'WS2_32.DLL';



const
  SIO_GET_INTERFACE_LIST = $4004747F;
  IFF_UP = $00000001;
  IFF_BROADCAST = $00000002;
  IFF_LOOPBACK = $00000004;
  IFF_POINTTOPOINT = $00000008;
  IFF_MULTICAST = $00000010;

type
  sockaddr_gen = packed record
    AddressIn: sockaddr_in;
    filler: packed array[0..7] of char;
  end;

type
  INTERFACE_INFO = packed record
    iiFlags: u_long;
    iiAddress: sockaddr_gen;
    iiBroadcastAddress: sockaddr_gen;
    iiNetmask: sockaddr_gen;
  end;

implementation

function EnumInterfaces(var sInt: string): Boolean;
var
  s: TSocket;
  wsaD: WSADATA;
  NumInterfaces: Integer;
  BytesReturned, SetFlags: u_long;
  pAddrInet: SOCKADDR_IN;
  pAddrString: PCHAR;
  PtrA: pointer;
  Buffer: array[0..20] of INTERFACE_INFO;
  i: Integer;
begin
  result := true;
  sInt := '';

  WSAStartup($0101, wsaD);

  s := Socket(AF_INET, SOCK_STREAM, 0);
  if (s = INVALID_SOCKET) then
    exit;

  try
    PtrA := @bytesReturned;
    if (WSAIoCtl(s, SIO_GET_INTERFACE_LIST, nil, 0, @Buffer, 1024, PtrA, nil,
      nil)
      <> SOCKET_ERROR) then
    begin
      NumInterfaces := BytesReturned div SizeOf(INTERFACE_INFO);

      for i := 0 to NumInterfaces - 1 do {Loop trough all interfaces}
      begin
        pAddrInet := Buffer[i].iiAddress.addressIn;
        pAddrString := inet_ntoa(pAddrInet.sin_addr);
        sInt := sInt + ' IP Address: ' + pAddrString + ',';
        pAddrInet := Buffer[i].iiNetMask.addressIn;
        pAddrString := inet_ntoa(pAddrInet.sin_addr);
        sInt := sInt + ' Subnet Mask: ' + pAddrString + ',';
      end;
    end;
  except
  end;
  CloseSocket(s);
  WSACleanUp;
  result := false;
end;
geklaut von hier

Grüße
Klaus
Klaus
  Mit Zitat antworten Zitat