Autor Beitrag
matze
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Sa 22.05.04 09:18 
hallo !!!

wie kann ich denn prüfen, ob der PC über seine Netzwerkkarte in einem LAN hängt oder nicht ?? (unter 2000/XP)
es gibt ja von Windows diesen Ballo Hint, wenn der PC ins Lan kommt. "Netzwerkkabel angschlossen" oder so.
wie kann ich denn dieen Status in Delphi anfragen ??

danke
matze

_________________
In the beginning was the word.
And the word was content-type: text/plain.
ultra2k
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starontopic star
Beiträge: 82

XP x64
D7 Enterprise
BeitragVerfasst: Di 08.06.04 21:36 
kleiner lösungsvorschlag:

es gibt routinen mit denen du u.a. die mac adresse abfragen kannst, wenn kein netzwerkkabel steckt, oder die verbindung deaktiviert ist, geben diese meiß "0" zurück...

wär vielleicht ein anfang :?:

_________________
...wenn NULL besonders groß ist, isses schon fast wie ein bisschen EINS!
Karlson
ontopic starontopic starontopic starontopic starontopic starofftopic starofftopic starofftopic star
Beiträge: 2088



BeitragVerfasst: Di 08.06.04 22:01 
Hallo,

Ich habe hier einen Beitrag in der Delphi Praxis gefunden, in welchem genau das gleiche steht. Dort stehen schon einige sehr gute lösungsansätze drin.
matze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Di 08.06.04 23:00 
hmmm... danke ich werd mir das mal anschauen !

_________________
In the beginning was the word.
And the word was content-type: text/plain.
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Mi 09.06.04 06:58 
Hmm, ich habe mir den DP-Thread noch nicht durchgelesen - vielleicht steht's ja schon drin...
Ich habe mir mal geholfen, indem ich ein UDP-Broadcast rausgeschickt habe. Dieses Broadcast kommt natürlich auch lokal zurück, aber nur an den Karten, die auch aktiv sind. Über den Broadcastempfang kriegst du ne IP und weist sofort, dass die gewählte Karte aktiv ist.

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
djmasi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 54

Win XP Prof. Tablet PC Edition 2005
D7 Enterprise
BeitragVerfasst: Mi 07.07.04 21:42 
Hatte mich auch schon mit dem Thema rumgeärgert. Die Sachen mit Registry und MAC Adresse fallen raus, da das mit der Reg auf jedem Windows unterschiedlich ist und die MAC bei Entfernen des Kabels erhalten bleibt.

Habe aber gerade etwas gefunden bei delphi3000.com.

bin mal so frei und liste den verwendeten Code:

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    Button2: TButton;
    Timer1: TTimer;
    CheckBox1: TCheckBox;
    procedure Button2Click(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
    function DetectHostIP: boolean;
  end;

var
  Form1: TForm1;
  HostIP:String='Unknown';


implementation

{$R *.dfm}

function TForm1.DetectHostIP: Boolean;
var
  wsdata: TWSAData;
  hostname: array[0..255] of char;
  hostEnt: PHostEnt;
  addr: PChar;
begin
  WSAStartup($0101,wsdata);
  try
    gethostname(hostname,sizeof(hostname));
    hostEnt := gethostbyname(hostname);
    if Assigned(hostEnt) then
      if Assigned(hostEnt^.h_addr_list) then
        begin
          addr := hostEnt^.h_addr_list^;
          if Assigned(addr) then
            begin
              HostIP := Format('%d.%d.%d.%d',[byte(addr[0]),byte(addr[1]),byte(addr[2]),byte(addr[3])]);
              Result := True;
            end
          else
            Result := False;
        end
      else
        Result := False
    else
      begin
        MessageDlg(Format('WinSock Error %d', [WSAGetLastError]),mtError,[mbOK],0);
        Result := False;
      end;
  finally
    WSACleanup;
  end;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  if DetectHostIP=True then ShowMessage(HostIP);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  if DetectHostIP=True then
    if HostIP <> '127.0.0.1' then
      CheckBox1.Checked := True //CheckBox sagt: Netzwerkkabel steckt
    else
      CheckBox1.Checked := False;  //oder halt net
end;

end.


Bei mir funktioniert das wunderbar, nach dem Rausziehen dauert es circa 5 sek, beim Reinstecken 1/2 sek bis es umschlägt.

Das Problem ist allerdings: Bei meinem Centrino 1.5 braucht das Prog 20% CPU. Das darf natürlich nicht sein. Habe aber grad keine Lust da ich morgen ne Algebra Klausur schreibe.

Aber werde mich damit weiter befassen.

Ciao Thomas
djmasi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 54

Win XP Prof. Tablet PC Edition 2005
D7 Enterprise
BeitragVerfasst: Do 08.07.04 08:38 
Hab's gerade nochmal mit WLAN ausprobiert, geht auch. Allerdings müsste man hier vielleicht noch ne Unterscheidung für die einzelnen Netzwerkadapter machen. Dürfte aber kein Problem sein.

Vielleicht war das gestern bei mir ein anderer Fehler, aber jetzt braucht das Programm nur noch 1 bis 2% CPU. :P
matze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Fr 09.07.04 13:22 
unter welchem Betriebssystem hast dus denn gemacht ?

_________________
In the beginning was the word.
And the word was content-type: text/plain.
djmasi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 54

Win XP Prof. Tablet PC Edition 2005
D7 Enterprise
BeitragVerfasst: Fr 09.07.04 15:55 
Windows XP Tablet PC Edition mit SP 1 8)

Aber warum willst du das wissen? Hast du den Code mal ausprobiert?
matze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Sa 10.07.04 09:03 
ich hatte bisher noch keine zeit, aber ich werde das mal unter verschiedenen OS testen und dann die ergebnisse hier posten !

_________________
In the beginning was the word.
And the word was content-type: text/plain.
djmasi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 54

Win XP Prof. Tablet PC Edition 2005
D7 Enterprise
BeitragVerfasst: Sa 10.07.04 22:50 
Hab's geschafft! Nach langem hin- und her hab ich die WMI zum laufen bekommen. Es werden jetzt alle Netzwerkadapter ausgegeben, die auch in "Netzwerkverbindungen" stehen, deren MAC-Adresse und Status (Connected/NOT Connected).

Probiert es mal bitte unter anderen OS als XP/SP1 aus und postet mal die Resultate.

ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
unit UNetWMI;

interface

uses
   // Delphi VCL Units
   Classes, Variants;

   // external forward declarations
   function getSystemMACAddresses: TStringList;

implementation

uses
   // Delphi VCL Units
   Windows,
   Dialogs,
   ComObj,
   ActiveX,
   // Imported type libraries
   WbemScripting_TLB;   { Typelib name: "Microsoft Windows WMI Scripting V1.2 Library" }

const
   // define used constants
   WMI_LOCAL_COMPUTER   = '.';
   WMI_SYSTEM_NAMESPACE = 'root\CIMV2';
   WMI_CLASS_NIC        = 'Win32_NetworkAdapter';
   WMI_ATTRIB_ADAPTER   = 'AdapterType';
   WMI_ATTRIB_AVAILAB   = 'NetConnectionID';
   WMI_ATTRIB_CONNSTAT  = 'NetConnectionStatus';
   WMI_ATTRIB_MAC       = 'MACAddress';
   WMI_ATTRIB_NAME      = 'Name';

// retrieve System MAC Addresses via WMI
//
function getSystemMACAddresses: TStringList;
var
   l_WMILocator:          ISWbemLocator;     // Locator, gets Services
   l_WMIServices:         ISWbemServices;    // Services, gets Object Definitions
   l_WMIObjectDefinition: ISWbemObject;      // Definition, gets Set of Objetcs
   l_WMIObjectSet:        SWbemObjectSet;    // ObjectSet, gets Enum over Instances
   l_WMIObjectInstances:  IEnumVariant;      // Enum of Instances, gets Object
   l_WMIObject:           ISWbemObject;      // Object, gets Sets of his properties
   l_WMIPropertySet:      ISWbemPropertySet; // PropertySet, gets single property
   l_WMIProperty:         ISWbemProperty;    // Property, gets Value

   l_TempObj:             OleVariant;        // temporary used values
   l_ObjValue:            Cardinal;
begin
   // create the return object
   result := TStringList.Create;

   // retrieve object enum through WMI classes
   l_WMILocator := CoSWbemLocator.Create;
   l_WMIServices := L_WMILocator.ConnectServer(WMI_LOCAL_COMPUTER, WMI_SYSTEM_NAMESPACE, '', '', '', '', 0, nil);
   l_WMIObjectDefinition := l_WMIServices.Get(WMI_CLASS_NIC, wbemFlagUseAmendedQualifiers, nil);
   l_WMIObjectSet := l_WMIObjectDefinition.Instances_(0, nil);
   l_WMIObjectInstances := (l_WMIObjectSet._NewEnum) as IEnumVariant;

   // iterate through enum values (WbemObjects) and get the property values
   while (l_WMIObjectInstances.Next(1, l_TempObj, l_ObjValue) = S_OK) do
   begin
      l_WMIObject:= IUnknown(l_TempObj) as SWBemObject;

      l_WMIPropertySet := l_WMIObject.Properties_;

      l_WMIProperty := l_WMIPropertySet.Item(WMI_ATTRIB_AVAILAB, 0);
      if not VarIsNull(l_WMIProperty.Get_Value) then
        begin
          result.Add(l_WMIProperty.Get_Value);
          l_WMIProperty := l_WMIPropertySet.Item(WMI_ATTRIB_NAME, 0);
          if not VarIsNull(l_WMIProperty.Get_Value) then
            result.Append(' - '+l_WMIProperty.Get_Value);
          l_WMIProperty := l_WMIPropertySet.Item(WMI_ATTRIB_MAC, 0);
          if not VarIsNull(l_WMIProperty.Get_Value) then
            result.Append(' - '+l_WMIProperty.Get_Value)
          else
            result.Append(' - MAC-Address not available');
          l_WMIProperty := l_WMIPropertySet.Item(WMI_ATTRIB_ADAPTER, 0);
          if not VarIsNull(l_WMIProperty.Get_Value) then
            result.Append(' - '+l_WMIProperty.Get_Value)
          else
            result.Append(' - Network Type not available');
          l_WMIProperty := l_WMIPropertySet.Item(WMI_ATTRIB_CONNSTAT, 0);
          if l_WMIProperty.Get_Value=2 then
            result.Append(' - Status: Connected')
          else
            result.Append(' - Status: NOT Connected');
          result.Add('');
        end;
   end;
end;
end.

Eingebunden wird das dann folgendermaßen:

ausblenden Delphi-Quelltext
1:
2:
3:
4:
procedure TForm1.Button1Click(Sender: TObject);
begin
  ListBox1.Items := getSystemMACAddresses;
end;



Wer die WMI Library braucht, sollte hier vorbeischauen. Aber Achtung, waren circa 323MB zum downloaden.
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Mo 12.07.04 12:00 
djmasi hat folgendes geschrieben:
Wer die WMI Library braucht, sollte hier vorbeischauen. Aber Achtung, waren circa 323MB zum downloaden.
Vielleicht wäre es einfacher, wenn du dein Prog als EXE hochladen konntest. Dann müsste ich mit meinem Modem nicht mal 1 MB runterladen :(

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
djmasi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 54

Win XP Prof. Tablet PC Edition 2005
D7 Enterprise
BeitragVerfasst: Mo 12.07.04 15:10 
Leider kann man ja hier nix hochladen. Deshalb stehts hier :D
matze Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 4613
Erhaltene Danke: 24

XP home, prof
Delphi 2009 Prof,
BeitragVerfasst: Mo 12.07.04 18:20 
also bevor das hier noch ausartet, würde ich vorschlagen, dass wir dieses crossposting lassen.
der thread in der DP ist ein weinig ausführlicher: www.delphipraxis.net...ic4043,0,asc,30.html

_________________
In the beginning was the word.
And the word was content-type: text/plain.
maxk
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 1696
Erhaltene Danke: 1

Win XP, Debian Lenny
Delphi 6 Personal
BeitragVerfasst: Mo 12.07.04 18:55 
:gruebel: Wer in der DP einen Account hat, kennt den Beitrag von dort - und wer keinen hat (ja ich geb's ja zu...) der kann von dort auch nur schwer herunterladen

_________________
Ein Computer wird das tun, was Du programmierst - nicht das, was Du willst.
djmasi
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 54

Win XP Prof. Tablet PC Edition 2005
D7 Enterprise
BeitragVerfasst: Mo 12.07.04 19:44 
Für alle die keinen DP-Zugang haben:

wwws.htwk-leipzig.de...ad/netzwerkkabel.rar
wwws.htwk-leipzig.de...d/IsNetworkAlive.rar

Für das erste braucht man WMI und mindestens 2000, besser XP
Das zweite sollte ab 95 laufen. Feedbacks sind natürlich erwünscht :D