2

I write because I could not find any answers on the internet. I am writing a project in C# on WLANs (using managed C++). Considering a given interface (network card) connected to a network, there is a way to know how long the interface is connected to the network?

In other words, I would like to know the time that you see in the connection information in Windows 7 (trayicon -> Open Network and Sharing Center).

I hope I was clear and I apologize for my poor English.

http://imageshack.us/photo/my-images/39/immaginenrz.png/

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Bruno Bruzzano
  • 477
  • 7
  • 21

1 Answers1

0

May be you are looking for getiftable() function of windows. It is a winapi function that provides all the information related to a particular network interface

Here is a good example of it

Here is the explanation of the entire structure

According to msdn

dwLastChange

Type: DWORD

The length of time, in hundredths of seconds (10^-2 sec), starting from the last computer restart, when the interface entered its current operational state.

When connected, it will give you the time since it is so - i.e. in connected state.

Here is the declaration of this function in c#

public static extern uint GetIfTable(IntPtr pIfTable, ref uint pdwSize, bool bOrder);

Declare Function GetIfTable Lib "iphlpapi.dll" (TODO) As TODO

        IPHelperTraslation.GetIfTable(IntPtr.Zero, ref size, false);

        IPHelperTraslation.GetIfTable(buf, ref size, false);

You can use pinvoke.net for further help on declaration.

It seems like there is an easy way in .Net framework but not as expected

http://msdn.microsoft.com/en-us/library/system.net.networkinformation.operationalstatus.aspx

Check out the way they have implemented network status detection. My assumption is, you will have to record the time in your application since the status last changed.

I also practically tested the getifrow function and to my disappointment, it always returns zero on Win7, don't know about other platforms.

Murtuza Kabul
  • 6,438
  • 6
  • 27
  • 34
  • ok, thanks for your quick response. But how can I use this "lastChange" in a C # project? – Bruno Bruzzano Sep 29 '12 at 15:16
  • It is a windows api call, you will have to make an api call, pinvoke, first find proper declaration of this api and then use it. – Murtuza Kabul Sep 29 '12 at 15:17
  • Sorry, but I later found that you are using c++ cli, so you will not need any declaration – Murtuza Kabul Sep 29 '12 at 15:18
  • You will have to make following includes Iphlpapi.h and the function getiftable() will be available in your code. – Murtuza Kabul Sep 29 '12 at 15:18
  • I'm sorry, I'll explain. I have created a project in c #, but since I use the wlanapi (written in c + +) I used "Managed Wifi" [link](http://managedwifi.codeplex.com/). So I am always in a C # project. How I can use what you said? sorry for my english. – Bruno Bruzzano Sep 29 '12 at 15:32
  • I tried as you suggested me but finally I could not get it to work. I also followed your suggestion and I saw this [article](http://social.msdn.microsoft.com/Forums/en/csharpgeneral/thread/3347a33a-1562-4643-ad80-0823e19a37f0), but it does not work. Can you help me? – Bruno Bruzzano Sep 29 '12 at 16:56
  • 1
    "The dwLastChange member is not currently supported by NDIS. On Windows Vista and later, NDIS returns zero for this member. On earlier versions of Windows, an arbitrary value is returned in this member for the interfaces supported by NDIS. For interfaces supported by other interface providers, they might return an appropriate value." [MSDN] – Bruno Bruzzano Sep 29 '12 at 18:54
  • pls give me a little time, let's see if I could give you a working code – Murtuza Kabul Sep 30 '12 at 04:43