Einzelnen Beitrag anzeigen

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, 09: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