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, 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, after Windows Mobile 5.0, Microsoft has provided a special function to get the network status change notification. What we need to do is wait for the message. The function is as follows:
Hresult winapi connmgrregisterforstatuschangenotification (bool fenable, hwnd );
This function notifies the system to send a notification of network status changes to a specified window. The returned values include:
S_ OK The 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_notification_msg text ("connmgr_status_change_icationication_msg" )
Therefore, you need to register the message before using the message:
M_connectmsg = registerwindowmessage (connmgr_status_change_icationication_msg );
The returned value m_connectmsg is the message passed to the window.
After receiving the connmgr_status_change_icationication_msg message, 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_icationication_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.