Home > OS >  Getting cable a connected/disconnect notification
Getting cable a connected/disconnect notification

Time:07-18

On Windows with Qt5.15, I implemented a cable connected/disconnected notifier based on QNetworkInterface.

It calls allinterfaces() every second and checks the flags. It works.

However now I'm wondering what are the alternatives. Is there a simpler way, perhaps a native Windows thing that I should just listen?

CodePudding user response:

I know 2 ways to listen for these events.

One is NotifyIpInterfaceChange C API. Pretty easy to use: call that function, pass a callback, and Windows will call you when things change.

Another one is INetworkListManagerEvents COM interface. More complicated to get notified, but here's a workflow.

  1. Implement that COM interface in some class.

  2. Create INetworkListManager object with CoCreateInstance, the SDK has a symbolic constant for class ID, CLSID_NetworkListManager

  3. Call QueryInterface to get IConnectionPointContainer interface of that object

  4. Call IConnectionPointContainer.FindConnectionPoint passing IID_INetworkListManagerEvents

  5. Create an instance of your class which implements INetworkListManagerEvents callback interface, call IConnectionPoint.Advise.

CodePudding user response:

There is not really a signal for this in QNetworkInterface. Polling is the only way to achieve this cross-platform using the isUp method.

I would not compromise the cross-platform behaviour for a Windows specific API as that would have an impact on the portability of the application.

Polling may not be ideal for you, but it is cross-platform. Which seems to be a strong value in a Qt application.

Moreover, cable disconnection is not the only reason why an interface may be unavailable. This is also why often there is a "ping timeout" for such things to detect all sorts of cases when a network interface goes down.

If you think that there ought to be a signal for this, you can open a ticket on the Qt Project tracker (Jira) explaining your use case. If they think that your request is reasonable, someone may eventually implement it for you. Even better if you submit a patch yourself.

  • Related