Einzelnen Beitrag anzeigen

djmasi

Registriert seit: 9. Jun 2004
Ort: Leipzig
105 Beiträge
 
Delphi 7 Enterprise
 
#38

Re: Feststellen, ob Netzwerkkabel gezogen wurde?

  Alt 10. Jul 2004, 22:58
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.

Delphi-Quellcode:
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:

Delphi-Quellcode:
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.


Anbei mal mein Versuchsprojekt
Angehängte Dateien
Dateityp: rar netzwerkkabel.rar (196,6 KB, 103x aufgerufen)
Thomas Maßmann
  Mit Zitat antworten Zitat