Network Connection Event Listener

J

Joe Hanna

Hello everyone,

I have tried search MSDN, Google and this newsgroup for a working solution but have not been successful.

I am trying to capture the System event raised when a Network Connection is established in VB.NET. I have seen many posts suggesting to create a timer and check the network status but I find this too resource hungry for my requirements.

I am not sure if WMI is the right area or not - I have no working knowledge of WMI!

Perhaps I can listen to Windows messages or something like that (if I knew what to listen for!).

Any help/code would be greatly appreciated.

Many Thanks,
Joe
 
W

Willy Denoyette [MVP]

What exactly do you call a "network connection"?
Do mean a connection from a client to a network resource, or a connection at the physical level?

Willy.

Hello everyone,

I have tried search MSDN, Google and this newsgroup for a working solution but have not been successful.

I am trying to capture the System event raised when a Network Connection is established in VB.NET. I have seen many posts suggesting to create a timer and check the network status but I find this too resource hungry for my requirements.

I am not sure if WMI is the right area or not - I have no working knowledge of WMI!

Perhaps I can listen to Windows messages or something like that (if I knew what to listen for!).

Any help/code would be greatly appreciated.

Many Thanks,
Joe
 
J

Joe Hanna

Thanks for your reply Willy.

At physical level I suppose.

I am writing a utility that needs to do different things when the laptop detects and connects to different wireless networks (office 1, office 2, home, etc...).

I simply need to know when the network connection is established and so can check the IP etc.

I didn't want to write something that "poles" the network connection as this would be too resource intensive.

Joe


What exactly do you call a "network connection"?
Do mean a connection from a client to a network resource, or a connection at the physical level?

Willy.

Hello everyone,

I have tried search MSDN, Google and this newsgroup for a working solution but have not been successful.

I am trying to capture the System event raised when a Network Connection is established in VB.NET. I have seen many posts suggesting to create a timer and check the network status but I find this too resource hungry for my requirements.

I am not sure if WMI is the right area or not - I have no working knowledge of WMI!

Perhaps I can listen to Windows messages or something like that (if I knew what to listen for!).

Any help/code would be greatly appreciated.

Many Thanks,
Joe
 
T

Tian Min Huang

Hello Joe,

Thanks for your post. As I understand, you want to get notified when a
Network Connection is established. Please correct me if there is any
misunderstand.

I believe WMI is a right area. Based on my experience and research, you can
use the Win32_NetworkConnection class in WMI as in this case. Please refer
to the following MSDN document:

Win32_NetworkConnection
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wmisdk/wmi/
win32_networkconnection.asp

BTW, in the future, it would be best to post WMI questions in the following
newsgroup:
microsoft.public.win32.programmer.wmi

Hope this helps.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Joe Hanna

Thanks for your reply Tian. The reason I posted to the dotnet groups is
because I am a VB.NET developer - not a win32 developer. I have no
knowledge of c/c++.

With respect to my problem, I am not after Network Connections in terms of
shared network resources (mapped drives).

I am interested in knowing whether the Network cable has just been plugged
in or my Wireless Network card has justed connected to an Access point.
Focus should rather be on the Network Adapter(s).

I figured listening to windows messages would help. So I tried creating a
class that Implements IMessageFilter and I was able to listen to broadcast
messages that sent when a Network Adaptor is Enabled/Disabled.

The messages object (m) appears as follows:

m.msg = 537 (WM_DEVICECHANGE from windows.h)
m.Wparam = 7 (DBT_DEVNODES from dbt.h)

These messages also are sent if a COM port is enabled/disabled. They are
not specific enough for my purposes. The other interesting note, is that
when the Network Adaptor is disabled, 4 of the above messages were sent
(with different WHnd). I guess there was one for each Mapped Network Drive
that I have. When I enabled the adapter, I got 8 messages, with each of the
previous WHnd appearing twice.

My other idea was to listen to windows messages. I used Spy++ to watch the
messages that went to a "Network Connection" window when the Network Adaptor
was Enabled/Disabled (as this window automatically refreshes the Icons and
Statuses of each Connection). This what I could make out:-

WM_USER + 16
WM_USER + 12
A bunch of other messages...

I don't quite understand windows messaging - as I am not a win32 programmer.
What is WM_USER?

I have done some more WMI digging (it is overwhelming having so many WMI
classes - you don't know where to find what you're looking for). I think
Win32_NetworkAdapter is probably more appropriate - but then I found
CIM_NetworkAdapter. What is the difference?

I downloaded the WMI extensions for VS.NET 2003 from MSDN then did the
following:-
* Added "Win32_NetworkAdapter" to the Management Events group in the Server
Explorer.
* Ensured it had started listening to Events using the Right-Click Menu
(Start was greyed out, Stop was available).
* Disabled/Enabled the Network Adapter but NOTHING appeared in the Output
Window.

Please help. I am new and quite lost when it comes to this lower level
system programming.

Thanks again,
Joe
 
W

Willy Denoyette [MVP]

Joe Hanna said:
Thanks for your reply Tian. The reason I posted to the dotnet groups is
because I am a VB.NET developer - not a win32 developer. I have no
knowledge of c/c++.

With respect to my problem, I am not after Network Connections in terms of
shared network resources (mapped drives).

I am interested in knowing whether the Network cable has just been plugged
in or my Wireless Network card has justed connected to an Access point.
Focus should rather be on the Network Adapter(s).

I figured listening to windows messages would help. So I tried creating a
class that Implements IMessageFilter and I was able to listen to broadcast
messages that sent when a Network Adaptor is Enabled/Disabled.

The messages object (m) appears as follows:

m.msg = 537 (WM_DEVICECHANGE from windows.h)
m.Wparam = 7 (DBT_DEVNODES from dbt.h)

These messages also are sent if a COM port is enabled/disabled. They are
not specific enough for my purposes. The other interesting note, is that
when the Network Adaptor is disabled, 4 of the above messages were sent
(with different WHnd). I guess there was one for each Mapped Network
Drive
that I have. When I enabled the adapter, I got 8 messages, with each of
the
previous WHnd appearing twice.

My other idea was to listen to windows messages. I used Spy++ to watch
the
messages that went to a "Network Connection" window when the Network
Adaptor
was Enabled/Disabled (as this window automatically refreshes the Icons and
Statuses of each Connection). This what I could make out:-

WM_USER + 16
WM_USER + 12
A bunch of other messages...

I don't quite understand windows messaging - as I am not a win32
programmer.
What is WM_USER?

I have done some more WMI digging (it is overwhelming having so many WMI
classes - you don't know where to find what you're looking for). I think
Win32_NetworkAdapter is probably more appropriate - but then I found
CIM_NetworkAdapter. What is the difference?

I downloaded the WMI extensions for VS.NET 2003 from MSDN then did the
following:-
* Added "Win32_NetworkAdapter" to the Management Events group in the
Server
Explorer.
* Ensured it had started listening to Events using the Right-Click Menu
(Start was greyed out, Stop was available).
* Disabled/Enabled the Network Adapter but NOTHING appeared in the Output
Window.

Please help. I am new and quite lost when it comes to this lower level
system programming.

Thanks again,
Joe
Take a look at the root\wmi namespace's class "MSNdis_MediaConnectStatus",
the property "NdisMediaConnectStatus" can have two values;
0 not connected and 1 connected.

Willy.
 
T

Tian Min Huang

Hi Joe,

Thanks for your reply. The reason I recommend you
microsoft.public.win32.programmer.wmi is that this group is also a managed
newsgroup which focuses on WMI. I believe WMI is the best method for this
issue.

Please take a look at MSNdis_MediaConnectStatus as suggested by Willy.

I am standing by for your result.

Regards,

HuangTM
Microsoft Online Partner Support
MCSE/MCSD

Get Secure! -- www.microsoft.com/security
This posting is provided "as is" with no warranties and confers no rights.
 
J

Joe Hanna

Thanks Willy and Tian.

This WMI class is undocumented - I cannot find it in my MSDN library nor can
I find any info on the MSDN site. I found this article
http://www.dotnet247.com/247reference/msgs/21/105555.aspx which states that
MSNdis_MediaConnectStatus is for Win2K and Win32_NetworkAdapter was for
WinXP. Transposing the C# code, I could not get MSNdis_MediaConnectStatus
to work, however, I was successful with Win32_NetworkAdapter (I am using XP
Pro).

I have done some fast WMI learning and researching (Thanks to Thomas
Tomiczek's code from this discussion:
http://www.dotnet247.com/247reference/msgs/22/112900.aspx). This is what I
have pieced together to obtain the (reliable) IP Address of each Adapter and
its Connection Status.
1. Win32_NetworkAdapterConfiguration

This will provide:
* MAC Address (Adapter unique ID)
* IPEnabled
* IPAddress


2. Win32_NetworkAdapter

This will provide:
* MAC Address (Adapter unique ID) to be matched to MAC
Address from Step 1
* NetConnectionStatus (0=Disconnected 1=Connecting
2=Connected)
* (Tried NetworkAddresses but it returned Nothing in VB.NET)

Is this the right way to do it or have I gone about it the long way?

I am really greatful for your help!

Thanks,
Joe
 
W

Willy Denoyette [MVP]

Joe,
Take care MSNdis_MediaConnectStatus resides in root\wmi not in root\cimv2!
The root\wmi namespace contains the classes for WMI enabled device drivers
based on the WDM.
MSNdis_MediaConnectStatus is available as from W2K and Win32_NetworkAdapter
is available on XP and higher.
For more info on MSNdis classes check this
URL:http://www.ndis.com/faq/QA01050301/default.htm

Willy.
 
J

Joe Hanna

I don't quite understand the difference between root\wmi and root\cimv2.

Do you think the way I am handling it is ok or is it on the right track?
 
W

Willy Denoyette [MVP]

Joe Hanna said:
I don't quite understand the difference between root\wmi and root\cimv2.

Do you think the way I am handling it is ok or is it on the right track?
They are separate namespaces.
I can't answer your question on how you handle it without some code, but if
you don't explicitely specify the namespace you will be connceted to
root\cimv2 (which is the default).

Willy.
 
J

Joe Hanna

I might need to pickup a book about WMI and get up to speed with it.

I will keep the current method of obtaining the info until I learn something
better. At least it works for now albiet not the most elegant solution.

Thanks for your help Willy and Tian.

Regards,
Joe
 
Y

Yan-Hong Huang[MSFT]

Hi Joe,

In WMI, classes are grouped into namespaces, which are logical groups of
classes representing a specific area of management. For example, the
root\cimv2 namespace includes most of the classes that represent resources
commonly associated with a computer and operating system. The classes used
in the previous scripts (Win32_LogicalMemoryConfiguration, Win32_Service,
and Win32_NTLogEvent) reside in the root\cimv2 namespace, and are just
three of literally hundreds of classes defined in the CIM. For WDM drivers,
the root\wmi namespace is the right place.

Using the WBEM-object browser that is provided with the WMISDK, you can
check each namespace.

Best regards,
Yanhong Huang
Microsoft Community Support

Get Secure! ¨C www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
J

Joe Hanna

Thanks Yan-Hong.

I have played around with the WBEM-object browser. It is a bit like the .NET
Framework - it is knowing where to look for the appropriate class and then
identifying the properties or fields that will solve your problem.

There is much to learn about the WMI namespaces. It is obvious I have some
learning to do!

I appreciate your help.

Joe
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top