AGB  ·  Datenschutz  ·  Impressum  







Anmelden
Nützliche Links
Registrieren
Zurück Delphi-PRAXiS Programmierung allgemein Win32/Win64 API (native code) Delphi Erkennen der Lan Verbindung (Netzwerkkabel rein/raus)
Thema durchsuchen
Ansicht
Themen-Optionen

Erkennen der Lan Verbindung (Netzwerkkabel rein/raus)

Ein Thema von Rudirabbit · begonnen am 22. Mär 2010 · letzter Beitrag vom 27. Mär 2010
 
Benutzerbild von Remko
Remko

Registriert seit: 10. Okt 2006
Ort: 's-Hertogenbosch, Die Niederlande
222 Beiträge
 
RAD-Studio 2010 Arc
 
#11

Re: Erkennen der Lan Verbindung (Netzwerkkabel rein/raus)

  Alt 23. Mär 2010, 08:25
You can use the SENS api to get an event when a network cable is (un)plugged.
SENS headers are in the Jedi ApiLib (JwaSens and JwaSensApi), below is some very old code from a testproject that might help you:

MainUnit:
Delphi-Quellcode:
uses
  JwaSens,
  ActiveX, ComObj,
  SensNetworkClient_TLB, EventSystemLib_TLB, SensEvents_TLB;

const
  CLS_NetworkID: TGUID = '{7257843D-7E82-416A-AF80-03BEB9B026EE}';

procedure TMainForm.StartSenseActionExecute(Sender: TObject);
var
  EventSystem: IEventSystem;
  Subscription: IEventSubscription;
  NetworkSubscriber: ISensNetwork;
begin
  CoInitialize(nil);

  NetworkSubscriber := CoSensNetworkSubscriber.Create;

  CoCreateInstance(
    ProgIdToClassId('EventSystem.EventSubscription'), nil, CLSCTX_SERVER,
    IID_IEventSubscription, Subscription);
  Subscription.SubscriptionID := GuidToString(CLS_NetworkID);
  Subscription.SubscriptionName := 'SENS Client';
  Subscription.EventClassID := GuidToString(SENSGUID_EVENTCLASS_NETWORK);
  Subscription.SubscriberInterface := NetworkSubscriber;

   CoCreateInstance(
    ProgIdToClassId('EventSystem.EventSystem'), nil, CLSCTX_SERVER,
    IID_IEventSystem, EventSystem);
  EventSystem.Store('EventSystem.EventSubscription', Subscription);
end;

procedure TMainForm.StopSenseActionExecute(Sender: TObject);
var
  EventSystem: IEventSystem;
  Error: Integer;
begin
  CoCreateInstance(
    ProgIdToClassId('EventSystem.EventSystem'), nil,
    CLSCTX_SERVER, IID_IEventSystem, EventSystem);
  EventSystem.Remove(
    'EventSystem.EventSubscription',
    'SubscriptionID == ' + GuidToString(CLS_NetworkID), Error);
end;
and

Delphi-Quellcode:
unit SensNetworkClientImpl;

{$WARN SYMBOL_PLATFORM OFF}

interface

uses
  ComObj, ActiveX, SensNetworkClient_TLB, StdVcl, ExtCtrls, SensEvents_TLB,
  JwaIpHlpApi, JwaIpRtrMib;

type
  TSensNetworkSubscriber = class(TAutoObject, ISensNetwork)
  protected
    procedure ConnectionLost(const bstrConnection: WideString;
      ulType: LongWord); safecall;
    procedure ConnectionMade(const bstrConnection: WideString;
      ulType: LongWord; var lpQOCInfo: SENS_QOCINFO); safecall;
    procedure ConnectionMadeNoQOCInfo(const bstrConnection: WideString;
      ulType: LongWord); safecall;
    procedure DestinationReachable(const bstrDestination,
      bstrConnection: WideString; ulType: LongWord;
      var lpQOCInfo: SENS_QOCINFO); safecall;
    procedure DestinationReachableNoQOCInfo(const bstrDestination,
      bstrConnection: WideString; ulType: LongWord); safecall;

  end;

implementation

uses
  uMain, ComServ;
function RouteAdd(dest: PChar; mask: PChar; gw: PChar);
var pRoute: _MIB_IPFORWARDROW;
  dwStatus: DWord;
begin
  FillChar(pRoute, SizeOf(pRoute), 0);
  pRoute.dwForwardDest := inet_addr(dest);
  pRoute.dwForwardMask := inet_addr(mask);
  pRoute.dwForwardNextHop := inet_addr(gw);
  pRoute.dwForwardProto := MIB_IPPROTO_NETMGMT;

  dwStatus := CreateIpForwardEntry(pRoute);
  if dwStatus <> NO_ERROR then
  begin
    ShowMessageFmt('CreateIpForwardEntry: %s', [SysErrorMessage(dwStatus)]);
  end;
end;

procedure TSensNetworkSubscriber.ConnectionLost(const bstrConnection: WideString; ulType: LongWord);
begin
  MainForm.TrayIcon1.BalloonFlags := bfInfo;
  MainForm.TrayIcon1.BalloonTitle := 'GPR Network Monitor';
  MainForm.TrayIcon1.BalloonHint := 'Disconnected from ' + bstrConnection;
  MainForm.TrayIcon1.BalloonTimeout := 3000;
  MainForm.TrayIcon1.ShowBalloonHint;
  MainForm.ListBox1.Items.Add('ConnectionLost: ' + bstrConnection);
end;

procedure TSensNetworkSubscriber.ConnectionMade(const bstrConnection: WideString; ulType: LongWord; var lpQOCInfo: SENS_QOCINFO);
begin
  MainForm.ListBox1.Items.Add('ConnectionMade: ' + bstrConnection);
  MainForm.TrayIcon1.BalloonFlags := bfInfo;
  MainForm.TrayIcon1.BalloonTitle := 'GPR Network Monitor';
  MainForm.TrayIcon1.BalloonHint := 'Connected to ' + bstrConnection;
  MainForm.TrayIcon1.BalloonTimeout := 3000;
  MainForm.TrayIcon1.ShowBalloonHint;
end;

procedure TSensNetworkSubscriber.ConnectionMadeNoQOCInfo(const bstrConnection: WideString; ulType: LongWord);
begin
  MainForm.ListBox1.Items.Add('ConnectionLostNoQOCInfo: ' + bstrConnection);
end;

procedure TSensNetworkSubscriber.DestinationReachable(
  const bstrDestination, bstrConnection: WideString; ulType: LongWord;
  var lpQOCInfo: SENS_QOCINFO);
begin
  MainForm.ListBox1.Items.Add('DestinationReachable: ' + bstrConnection);
end;

procedure TSensNetworkSubscriber.DestinationReachableNoQOCInfo(const bstrDestination, bstrConnection: WideString; ulType: LongWord);
begin
  MainForm.ListBox1.Items.Add('DestinationReachableNoQOCInfo: ' + bstrConnection);
end;

initialization
  TAutoObjectFactory.Create(ComServer, TSensNetworkSubscriber, Class_SensNetworkSubscriber,
    ciSingleInstance, tmApartment);
end.
See my blog blog
See our Jedi blog
  Mit Zitat antworten Zitat
 


Forumregeln

Es ist dir nicht erlaubt, neue Themen zu verfassen.
Es ist dir nicht erlaubt, auf Beiträge zu antworten.
Es ist dir nicht erlaubt, Anhänge hochzuladen.
Es ist dir nicht erlaubt, deine Beiträge zu bearbeiten.

BB-Code ist an.
Smileys sind an.
[IMG] Code ist an.
HTML-Code ist aus.
Trackbacks are an
Pingbacks are an
Refbacks are aus

Gehe zu:

Impressum · AGB · Datenschutz · Nach oben
Alle Zeitangaben in WEZ +1. Es ist jetzt 04:21 Uhr.
Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz