In PPC, how can I determine whether the network is connected or disconnected?

Source: Internet
Author: User

In the development of PPC, you sometimes need to determine when the network is connected, when it is disconnected, and handle it accordingly. For example, in the timesyncppc program, you need to get the message after the network connection and then download the time synchronization information. Therefore, you must have a method for the timesyncppc program to get the system message after the PPC connection network. Of course, some people say they can keep polling to see if the system is connected to the network, but this is definitely not a good method. In fact, Windows
Mobile
After 5.0, Microsoft has provided a special function to get the network state change notification. What we need to do is wait for the message. The function is as follows:

Hresult
Winapi connmgrregisterforstatuschangenotification (
Bool
Fenable,
Hwnd
Hwnd
);

This function notifies the system to send a notification of network status changes to a specified window. The returned values include:

S_ OK
Function call was successful.

E_handle the supplied window handle is
Invalid.

E_accessdenied The Calling process does not have sufficient
Privileges to use this
Function.

When the network is connected or disconnected, the connmgrregisterforstatuschangenotification function sends a message of connmgr_status_change_icationication_msg to the window. However, the definition of connmgr_status_change_icationication_msg is not a message, but a string:

# Define
Connmgr_status_change_icationication_msg
Text ("connmgr_status_change_notification_msg ")

Therefore, you need to register the message before using the message:

M_connectmsg
=
Registerwindowmessage (connmgr_status_change_notification_msg );

The returned value m_connectmsg is the message passed to the window.

Connmgr_status_change_icationication_msg
You can determine whether to disconnect or connect to the network based on wparam. The test shows that the message is triggered when you connect to ActiveSync, GPRS (cmwap or cmnet), WiFi, or even make a phone call.

In addition, there is an error in Microsoft documents. The connmgrregisterforstatuschangenotification function is often written as connmgrregisterforstatusnotification. Therefore, if you see the connmgrregisterforstatusnotification function, it actually refers to the callback function.

The following is an actual example. If you have any questions, please leave a message at www.17feixiang.com:

# Include
"Connmgr_status.h"
# Include "connmgr. H"
# Pragma
Comment (Lib, "wininet. lib ")
# Pragma comment (Lib, "cellcore. lib ")

Uint
M_connectmsg;

Bool
Cxxxxdlg: oninitdialog ()
{
......

Hresult ret =
Connmgrregisterforstatuschangenotification (true,
This-> m_hwnd );
M_connectmsg =
Registerwindowmessage (connmgr_status_change_notification_msg );

Return
True; // return true unless you set the focus to a control
}

Lresult
Cxxxxdlg: windowproc (uint message, wparam, lparam)
{
//
Todo: add your specialized code here and/or call the base
Class
If (Message =
M_connectmsg)
{
Switch (wparam)
{
Case
Connmgr_status_connected
:

MessageBox (_ T ("connected "));
Break;

Case
Connmgr_status_disconnected
:

MessageBox (_ T ("disconnected "));
Break;

Default
:

Break;
}
}

Return
Cdialog: windowproc (message, wparam, lparam );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.