Delphi-PRAXiS

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Netzwerke (https://www.delphipraxis.net/14-netzwerke/)
-   -   Delphi WLAN ip ermitteln (https://www.delphipraxis.net/96225-wlan-ip-ermitteln.html)

k4ni 20. Jul 2007 11:45


WLAN ip ermitteln
 
hey leute
Dieser script bewirkt das ich die lokale IP adresse des pc's herbekomm
Delphi-Quellcode:
function GetLocalIPs(const Lines:TStrings):Boolean;
type
  PPInAddr= ^PInAddr;
var
  wsaData: TWSAData;
  HostInfo: PHostEnt;
  HostName: Array[0..255] of Char;
  Addr: PPInAddr;
begin
  Result:=False;
  Lines.Clear;
  if WSAStartup($0102, wsaData)=0 then
  try
    if gethostname(HostName, SizeOf(HostName)) = 0 then Begin
       HostInfo:= gethostbyname(HostName);
       if HostInfo<>nil then Begin
          Addr:=Pointer(HostInfo^.h_addr_list);
          if (Addr<>nil) AND (Addr^<>nil) then
             Repeat
                    Lines.Add(StrPas(inet_ntoa(Addr^^)));
                    inc(Addr);
             Until Addr^=nil;
       end;
    end;
    Result:=True;
  finally
    WSACleanup;
  end;
end;
Wie bekomm ich die lokale IP des WLAN adapers raus?

Torpedo 20. Jul 2007 12:32

Re: WLAN ip ermitteln
 
Gibts da überhaupt einen Unterschied?

markusj 20. Jul 2007 15:10

Re: WLAN ip ermitteln
 
Wenn ich den Code richtig verstanden habe, gibt er die IPs aller Karten zurück und damit also auch die der WLAN-Karte.

mfG
Markus

k4ni 20. Jul 2007 19:09

Re: WLAN ip ermitteln
 
also ich benütz das script damit is ya die lokale IP des PC'S bekomm. und diese verwend ich dann in diesem script

Delphi-Quellcode:
unit DropAllU;
{
Aufruf:
   InstallFW('192.168.0.2');

und zum schluss:
   RemoveFW;
}

interface

   procedure InstallFW(MyIP: String);
   procedure RemoveFW;

implementation

uses Windows;

const
   IPHLPAPI = 'IPHLPAPI.DLL';

type
  PFFORWARD_ACTION = Integer;
  PPFFORWARD_ACTION = ^PPFFORWARD_ACTION;
  //
  INTERFACE_HANDLE = Pointer;
  //
  PFADDRESSTYPE    = Integer;
  PPFADDRESSTYPE   = ^PFADDRESSTYPE;
  //
  TByteArray       = Array [0..Pred(MaxInt)] of Byte;
  PByteArray       = ^TByteArray;
  TIpBytes         = Array [0..3] of Byte;

const
  PF_ACTION_DROP   = 1;

const
  PF_IPV4           = 0;

function  PfCreateInterface(
           dwName:          DWORD;
           inAction:        PFFORWARD_ACTION;
           outAction:       PFFORWARD_ACTION;
           bUseLog:         BOOL;
           bMustBeUnique:   BOOL;
           var ppInterface: INTERFACE_HANDLE): DWORD;
           stdcall; external IPHLPAPI name '_PfCreateInterface@24';

function  PfDeleteInterface(
           pInterface:      INTERFACE_HANDLE): DWORD;
           stdcall; external IPHLPAPI name '_PfDeleteInterface@4';

function  PfBindInterfaceToIPAddress(
           pInterface:      INTERFACE_HANDLE;
           pfatLinkType:    PFADDRESSTYPE;
           IPAddress:       PByteArray): DWORD;
           stdcall; external IPHLPAPI name '_PfBindInterfaceToIPAddress@12';

function  PfUnBindInterface(
           pInterface:      INTERFACE_HANDLE): DWORD;
           stdcall; external IPHLPAPI name '_PfUnBindInterface@4';

var
   Handle_Interface : INTERFACE_HANDLE = nil;

function StrToInt(S: PChar): Integer;
begin
   Result := 0;
   if S = '' then Exit;
   while S^ in ['0'..'9'] do begin
      Result := Result * 10 + Integer(S^) - Integer('0');
      Inc( S );
   end;
end;

function StrToIpBytes( IpStr: String ): TIpBytes;
var N : Integer;
begin
   N := 0;
   while Pos('.', IpStr)>0 do begin
      Result[N] := StrToInt(@Copy(IpStr, 1, Pos('.', IpStr) - 1)[1]);
      Delete(IpStr, 1, Pos('.', IpStr));
      Inc(N);
   end;
   Result[N] := StrToInt(@IpStr[1]);
end;

procedure InstallFW(MyIP: String);
var
   IpLocal : TIpBytes;
begin
   if (MyIP <> '') and Not Assigned(Handle_Interface) then begin
      FillChar(IpLocal, 4, #0);
      IpLocal := StrToIpBytes(MyIP);

      PfCreateInterface(0, PF_ACTION_DROP, PF_ACTION_DROP, False, False, Handle_Interface);
      PfBindInterfaceToIPAddress(Handle_Interface, PF_IPV4, @ipLocal);
   end;
end;

procedure RemoveFW;
begin
   if Assigned(Handle_Interface) then begin
      PfUnBindInterface(Handle_Interface);
      PfDeleteInterface(Handle_Interface);
      Handle_Interface := nil;
   end;
end;

end.
//credits fuer den gehen an x000x

Doch wenn z.b mein cousin das versucht und dann mit dem script die local IP zu bekommen und mit dem hier ^^die verbindung zu trennen (er benützt WLAN) dann klappt das nicht. Doch bei mir mit normal LAN gehts.
Es ist nicht nur bei ihm so. Bis jetzt haben sich nur WLAN user beschwert dass es nicht geht, idee?

markusj 20. Jul 2007 19:23

Re: WLAN ip ermitteln
 
Das liegt dann vermutlich aber eher daran, dass dein Cousin ein spezielles Tool (beispielsweise von Intel) verwendet, welches sich dagegen sträubt.

mfG
Markus

Ach ja: wir "scripten" hier nicht, sondern wir Programmieren ;)

k4ni 20. Jul 2007 19:28

Re: WLAN ip ermitteln
 
okay danke. Ich werd mich jetzt mal nach mehreren WLAN usern umschaun und die testen lassen.
Wegen dem Scripen, alles klar :-D bins nur gewöhnt xD

k4ni 21. Jul 2007 21:41

Re: WLAN ip ermitteln
 
Also, ich habs jetzt mal ein paar leute testen lassen und be keinem hats geklappt, hat jemand ne ahnung woran dass liegt?

x000x 24. Jul 2007 19:19

Re: WLAN ip ermitteln
 
Moin moin,
ich habe auch "nur" WLAN, dann bin ich wohl einer der wenigen bei dem
es funktioniert.
Die Ausgabe von ipconfig sieht bei mir wie folgt aus:
Code:
Ethernetadapter Drahtlose Netzwerkverbindung:

        Verbindungsspezifisches DNS-Suffix:
        Beschreibung. . . . . . . . . . . : 22Mbps Wireless Network PCI Adapter
        Physikalische Adresse . . . . . . : 00-03-2F-0C-80-A1
        DHCP aktiviert. . . . . . . . . . : Nein
   --> IP-Adresse. . . . . . . . . . . . : 192.168.0.112
        Subnetzmaske. . . . . . . . . . . : 255.255.255.0
        IP-Adresse. . . . . . . . . . . . : 192.168.0.111
        Subnetzmaske. . . . . . . . . . . : 255.255.255.0
        Standardgateway . . . . . . . . . : 192.168.0.9
        DNS-Server. . . . . . . . . . . . : 192.168.0.9
Wie du sehen kannst, habe ich noch eine weitere Adresse hinzugefügt (Fett). Es ist nun egal,
auf welche IP ich den Portfilter binde, ich komm bei aktiviertem Portfilter nicht mehr
z.B. ins Internet oder ins lokale Netz.

Da du nun aber geschrieben hast, das es bei deinen Kollegen nicht funktioniert, hier meine Vermutungen:
  • Du bindest den Filter an die falsche IP ?
  • Deine Kollegen haben neben dem WLAN Adapter noch eine weitere Netzwerkkarte, über die sie auch eine
    Verbindung zum INET haben?
  • Du rufst die Funktion InstallFW (Der Name ist hier übrigens nicht korrekt, sollte eher InstallPF lauten) für mehrere IP-Adressen auf. Dies wird
    so nicht funktionieren, da musst du den Code umstellen.
Schreib doch mal, welche IP-Konfiguration deine Tester haben (also Ausgabe von ipconfig /all) und wie du die Funktion
aufgerufen hast. Denke mal, dann kann ich dir hier weiter helfen.

k4ni 24. Jul 2007 22:46

Re: WLAN ip ermitteln
 
okay danke x000x für die function und deine hilfe, es geht jetzt doch. Weis aber nicht woran es lag :)
ty


Alle Zeitangaben in WEZ +1. Es ist jetzt 04:40 Uhr.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024 by Thomas Breitkreuz