2vision2
2vision2

Reputation: 5023

Monitor RAS Dial Connection from My application using APIs

I created a DialUp connection using the RASDIAL or RASPHONE exe. I want my application to monitor the established connection and if the connection gets disconnected my application should exit. How to manage this programatically? Please suggest me..

Upvotes: 1

Views: 2213

Answers (3)

Jeff Winn
Jeff Winn

Reputation: 804

The correct Win32 API for receiving event based notifications regarding RAS connections and disconnections is RasConnectionNotification.

Here's a link: http://msdn.microsoft.com/en-us/library/aa376726(VS.85).aspx

If you're using the DotRas SDK on CodePlex the RasConnectionWatcher handles these notifications using the above mentioned API.

using DotRas;

RasConnectionWatcher watcher = new RasConnectionWatcher();
watcher.Connected += (sender, e) => 
    { 
        // Connected! 
    };
watcher.Disconnected += (sender, e) => 
    { 
        // Disconnected! 
    };

You can also specify a handle returned from RasDial to receive events specific to that handle by setting the Handle property.

watcher.Handle = handle;
watcher.EnableRaisingEvents = true;

Here's a link to DotRas if you're interested: http://dotras.codeplex.com

Edit: I just wanted to add, if you do not specify a handle to the either the DotRas or Win32 APIs, you'll receive events from all connects, disconnects, and bandwidth added/removed on the machine.

Upvotes: 1

Alex K.
Alex K.

Reputation: 175876

lpvNotifier of the RadDial function allows you to pass a RasDialFunc1 which will receive notification events regarding the lifetime of the connection. (RasGetConnectStatus is also available for polling)

Upvotes: 0

Andreas
Andreas

Reputation: 1791

I suggest you use a thread to periodically check the status of the connection.

This can be done using RasEnumConnections from rasapi32.dll. This call returns the list of active connections, which you can then iterate over to check whether the one you opened is among them.

Upvotes: 2

Related Questions