1

I am going to put an network status icon in my GUI application. To get the network status notification, I am trying to use the Windows API. For this I am thinking to use NetworkAvailabilityChangedEventHandler in my application. I am very new to programming with the Windows API and framework. Can anybody help me in the following things:

  1. Can the API NetworkAvailabilityChangedEventHandle only be used in C#?
  2. Can I use it in C++ (Qt)?
  3. Which header file must I include? (I checked in MSDN for this. but they are using namespace for this. All the examples are in C#. I am not able to understand how to implement it in my C++ code.)

I will be grateful if somebody can give me a detailed code snippet for using this windows event handler, including the .h file or namespace to be included.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Surjya Narayana Padhi
  • 7,741
  • 25
  • 81
  • 130

1 Answers1

4

Where did you get the idea to use the NetworkAvailabilityChangedEventHandler delegate?

That is explicitly not part of the Windows API, but rather a delegate function used by the .NET Framework in conjunction with the NetworkChange.NetworkAvailabilityChanged event. That explains why all the examples on MSDN are in C#—because this is only intended to be used in applications targeting the .NET Framework. If you're writing unmanaged C++ using Qt, then you're not using the .NET Framework, and you can't take advantage of its functionality.


The Windows API equivalent is the InternetGetConnectedState function, which returns a value indicating whether or not the system is currently connected to the Internet. You'll find that its MSDN documentation is substantially friendlier towards unmanaged C++ developers, because that's the
primary intended audience. The information that you're seeking is given at the bottom:

Header          Wininet.h

Library           Wininet.lib

DLL                 Wininet.dll

You can find a list of all the WinINet functions here.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • This isn't _precisely_ the same thing, as `NetworkChange.NetworkAvailabilityChanged` is a push notification, while `InternetGetConnectedState` is a polling API... – bdonlan Feb 09 '11 at 04:36
  • 3
    @bdonlan: You're right. I did say it was the equivalent, not that it was exactly the same. `NetworkChange.NetworkAvailabilityChanged` actually polls `InternetGetConnectedState` under the hood and implements a push notification to the consumer. You get a lot of things for free with .NET that you don't get with the Windows API. That doesn't stop the asker from rolling his own polling function, and to do it, he'd use `InternetGetConnectedState`. – Cody Gray - on strike Feb 09 '11 at 04:38
  • @cody Gray: thanks a lot for quick response with solution. When i used the API 'GetSystemPowerStatus' i just included the windows.h file in my application and dint add any dll and lib file. But is it ok just to include the file wininet.h in my code? or i need to use the lib and dll? if yes could please let me know how i can use them in my qt app? Do i need to find the .lib and .dll and paste them in my qt app directory or provide the path? – Surjya Narayana Padhi Feb 09 '11 at 05:00
  • @Surjya: You'll need to include the header file and add `wininet.lib` as a dependency to your project. You do *not* need to copy the DLL file anywhere, as it's included with a standard Windows installation. Your app should not have its own copy. It's difficult to provide more specific instructions on how to add a dependency without knowing which IDE/compiler you're using. In Visual Studio, right-click on your project and open its Properties window. Then expand the "Linker" category and select "Input". Add `wininet.dll` to the "Additional Dependencies" line. – Cody Gray - on strike Feb 09 '11 at 05:06
  • @Surjya: See [this question](http://stackoverflow.com/questions/1114914/add-library-to-visual-studio-2008-c-project) for more details. – Cody Gray - on strike Feb 09 '11 at 05:07
  • @Cody Gray: I am using Qt-Creator IDE. I included the line "LIB += wininet.lib". But I am getting compilation error as "No such file or directory : wininet.lib". Where can I find the .lib file in my windows installation? – Surjya Narayana Padhi Feb 09 '11 at 05:32
  • @Surjya: I may not be the best person to ask about that. I downloaded Qt a few months ago just to see what all the hullabaloo was about, and immediately uninstalled it after a few minutes upon discovering that it doesn't use native GUI controls from the underlying OS. So I've never actually used Qt-Creator, and that `LIB += wininet.lib` looks mighty suspicious to me. It may very well be the way that Qt-Creator does things, though. – Cody Gray - on strike Feb 09 '11 at 05:37
  • @Surjya: The only suggestion I can offer is to make sure that you have the Windows SDK installed. I don't know if Qt-Creator comes with it or not, but you need it to develop programs targeting the Windows API. If you don't have it, that would explain why you're missing the `.lib` files. You can download the latest version here: http://www.microsoft.com/downloads/en/details.aspx?FamilyID=6b6c21d2-2006-4afa-9702-529fa782d63b&displaylang=en – Cody Gray - on strike Feb 09 '11 at 05:37
  • the `qmake` script `win32: LIBS += -lWininet` should work, and the file is at `"C:\Program Files\Microsoft SDKs\Windows\v7.0A\Lib\WinInet.Lib"` – Top-Master Feb 25 '19 at 10:40