Get time through WIFI or GPRS to solve the problem of changing the battery time of Windows Mobile

Source: Internet
Author: User

From: http://blog.csdn.net/ppCuda/archive/2010/01/03/5123011.aspx

After more than one year, all of my WM5 batteries will be lost. Although some calibration tools appeared online, it seems to be useless to my Atom Exec.

So on the basis of the previous version, automatic WIFI connection Internet time calibration and automatic GPRS time calibration were added. C #

First, enable and disable the WIFI function.

/// <Summary>
/// Set the WifI status, enable/disable
/// </Summary>
/// <Param name = "open"> true indicates that WIFI is enabled, and false indicates that WIFI is disabled. </param>
/// <Returns> return true if the operation is successful </returns>
Private bool setwifistate (bool open)
{
// Find the registry to obtain the name of the WIFI device, and then turn on the WIFI power
Bool ret = false;
String [] sNames = null;
RegistryKey keyWlan = null;
Try
{
KeyWlan = Registry. LocalMachine. OpenSubKey (@ "System \ CurrentControlSet \ Control \ Power \ State ");
SNames = keyWlan. GetValueNames ();
}
Catch {}
Finally
{
If (keyWlan! = Null) keyWlan. Close ();
}

Foreach (string wl in sNames)
{
If (wl. StartsWith ("{98c5250d-c29a-4985-ae5f-afe5425e5006 }\\"))
{
// POWER_NAME = 0x00000001
SetDevicePower (wl, 0x00000001, open? DevicePowerState. D0: DevicePowerState. D4 );
Ret = true;
Break;
}
}
Return ret;
}

Then, check whether WIFI has been connected to a hot spot. This requires constant check.

/// <Summary>
/// View the WifI connection hotspot status
/// </Summary>
/// <Returns> true indicates that the previous hotspot is connected. </returns>
Private bool checkwificonstate ()
{
Bool ret = false;
RegistryKey keyWlan = null;
Try
{
KeyWlan = Registry. LocalMachine. OpenSubKey (@ "System \ State \ Hardware ");
Int value = (int) keyWlan. GetValue ("wifi", 0 );
If (value> = 0x00000010)
{
Ret = true;
}
}
Catch {}
Finally
{
If (keyWlan! = Null) keyWlan. Close ();
}

Return ret;
}

The above is the wifi connection to open the hotspot, the next is to open the GPRS Query

The function ConnMgrEstablishConnectionSync is used to enable GPRS. I found a ConnectionManager class on the Internet and encapsulated this operation. I will share it with you!

Public class ConnectManager
{
Const int S_ OK = 0;
Const uint CONNMGR_PARAM_GUIDDESTNET = 0x1;
Const uint CONNMGR_PRIORITY_USERINTERACTIVE = 0x08000;
Const uint INFINITE = 0 xffffffff;
Const uint CONNMGR_STATUS_CONNECTED = 0x10;
Const int CONNMGR_MAX_DESC = 128; // @ constdefine Max size of a network description

Const int CONNMGR_FLAG_PROXY_HTTP = 0x1; // @ constdefine HTTP Proxy supported
Const int CONNMGR_FLAG_PROXY_WAP = 0x2; // @ constdefine WAP Proxy (gateway) supported
Const int CONNMGR_FLAG_PROXY_SOCKS4 = 0x4; // @ constdefine SOCKS4 Proxy supported
Const int CONNMGR_FLAG_PROXY_SOCKS5 = 0x8; // @ constdefine SOCKS5 Proxy supported

Const UInt16 IDC_WAIT = 32514;
Const UInt16 IDC_ARROW = 32512;

Private IntPtr m_hConnection = IntPtr. Zero;

Public ConnectManager ()
{
}

~ ConnectManager ()
{
ReleaseConnection ();
}

/// <Summary>
/// Check whether the connection is available
/// </Summary>
/// <Returns> </returns>
Public bool GetConnMgrAvailable ()
{
IntPtr hConnMgr = ConnMgrApiReadyEvent ();
Bool bAvailbale = false;
Uint dwResult = WaitForSingleObject (hConnMgr, 2000 );

If (dwResult = 0)
{
BAvailbale = true;
}

// Close
If (hConnMgr. ToInt32 ()! = 0) CloseHandle (hConnMgr );

Return bAvailbale;
}
/// <Summary>
/// Ing URL
/// </Summary>
/// <Param name = "lpszURL"> </param>
/// <Param name = "guidNetworkObject"> </param>
/// <Param name = "pcsDesc"> </param>
/// <Returns> </returns>
Public int MapURLAndGUID (string lpszURL, ref GUID guidNetworkObject, ref string pcsDesc)
{
If (lpszURL = null | lpszURL. Length <1)
Return 0;

Uint nIndex = 0;
Int hResult = ConnMgrMapURL (lpszURL, ref guidNetworkObject, ref nIndex );
If (hResult <0)
{
Throw new Exception ("cocould not map a request to a network identifier ");
}
Else
{
If (pcsDesc! = Null)
{
CONNMGR_DESTINATION_INFO DestInfo = new CONNMGR_DESTINATION_INFO ();
If (ConnMgrEnumDestinations (int) nIndex, ref DestInfo)> = 0)
{
PcsDesc = DestInfo. szDescription;
}
}
}

Return (int) nIndex;
}
/// <Summary>
/// Enumerate network identifier information
/// </Summary>
/// <Param name = "lstNetIdentifiers"> </param>
Public List <CONNMGR_DESTINATION_INFO> EnumNetIdentifier ()
{
List <CONNMGR_DESTINATION_INFO> lstNetIdentifiers = new List <CONNMGR_DESTINATION_INFO> ();
// Obtain the Network List
For (uint dwEnumIndex = 0; dwEnumIndex ++)
{
CONNMGR_DESTINATION_INFO networkDestInfo = new CONNMGR_DESTINATION_INFO ();

If (ConnMgrEnumDestinations (int) dwEnumIndex, ref networkDestInfo )! = 0)
{
Break;
}
LstNetIdentifiers. Add (networkDestInfo );
}

Return lstNetIdentifiers;
}

/// <Summary>
/// Establish a connection
/// </Summary>
/// <Param name = "nIndex"> </param>
/// <Returns> </returns>
Public bool EstablishConnection (uint nIndex)
{
ReleaseConnection ();
// Obtain the correct connection information
CONNMGR_DESTINATION_INFO DestInfo = new CONNMGR_DESTINATION_INFO ();
Int hResult = ConnMgrEnumDestinations (int) nIndex, ref DestInfo );

If (hResult> = 0)
{
// Initialize the Connection Structure
CONNMGR_CONNECTIONINFO ConnInfo = new CONNMGR_CONNECTIONINFO ();

ConnInfo. cbSize = (uint) Marshal. SizeOf (ConnInfo );
ConnInfo. dwParams = CONNMGR_PARAM_GUIDDESTNET;
ConnInfo. dwFlags = CONNMGR_FLAG_PROXY_HTTP | CONNMGR_FLAG_PROXY_WAP | CONNMGR_FLAG_PROXY_SOCKS4 | CONNMGR_FLAG_PROXY_SOCKS5;
ConnInfo. dwPriority = CONNMGR_PRIORITY_USERINTERACTIVE;
ConnInfo. guidDestNet = DestInfo. guid;
ConnInfo. bExclusive = 1; // 0 indicates the shared connection. GPRS is not disabled after the program exits. 1 indicates that the connection is not shared.
ConnInfo. bDisabled = 0;

Uint dwStatus = 0;
HResult = ConnMgrEstablishConnectionSync (ref ConnInfo, ref m_hConnection, 18*1000, ref dwStatus );
If (hResult <0)
{
// Bool bRet = WaitForConnected (10, ref dwStatus );
Return false;
}
Else
{
Return true;
}
}

Return false;
}
/// <Summary>
/// Connection status
/// </Summary>
/// <Param name = "nTimeoutSec"> </param>
/// <Param name = "pdwStatus"> </param>
/// <Returns> </returns>
Public bool WaitForConnected (int nTimeoutSec, ref uint pdwStatus)
{
Uint dwStartTime = GetTickCount ();
Bool bRet = false;

While (GetTickCount ()-dwStartTime <(uint) nTimeoutSec * 1000)
{
If (m_hConnection.ToInt32 ()! = 0)
{
Uint dwStatus = 0;
Int hr = ConnMgrConnectionStatus (m_hConnection, ref dwStatus );
If (dwStatus! = 0) pdwStatus = dwStatus;
If (hr> = 0)
{
If (dwStatus = CONNMGR_STATUS_CONNECTED)
{
BRet = true;
Break;
}
}
}
Thread. Sleep (100 );
}

Return bRet;
}

/// <Summary>
/// Release all connections
/// </Summary>
Public void ReleaseConnection ()
{

If (m_hConnection.ToInt32 ()! = 0)
{
ConnMgrReleaseConnection (m_hConnection, 0 );
M_hConnection = IntPtr. Zero;
}
}

[StructLayout (LayoutKind. Sequential)]
Public struct CONNMGR_CONNECTIONINFO
{
Public uint cbSize;
Public uint dwParams;
Public uint dwFlags;
Public uint dwPriority;
Public int bExclusive;
Public int bDisabled;
Public GUID guidDestNet;
Public IntPtr hWnd;
Public uint uMsg;
Public uint lParam;
Public uint ulMaxCost;
Public uint ulMinRcvBw;
Public uint ulMaxConnLatency;
}

[StructLayout (LayoutKind. Sequential, CharSet = CharSet. Unicode)]
Public struct CONNMGR_DESTINATION_INFO
{
Public GUID guid; // @ field GUID associated with network
[Financialas (UnmanagedType. ByValTStr, SizeConst = CONNMGR_MAX_DESC)]
Public string szDescription; // @ field Description of network
Public int fSecure; // @ field Is it OK to allow multi-homing on the network
}

Public struct GUID
{// Size is 16
Public uint Data1;
Public UInt16 Data2;
Public UInt16 Data3;
[Financialas (UnmanagedType. ByValArray, SizeConst = 8)]
Public byte [] Data4;
}

[DllImport ("coredll. dll")]
Public static extern uint GetTickCount ();

[DllImport ("coredll. dll")]
Public static extern uint WaitForSingleObject (IntPtr hHandle, uint dwMilliseconds );

[DllImport ("cellcore. dll")]
Public static extern int ConnMgrMapURL (string pwszURL, ref GUID pguid, ref uint pdwIndex );

[DllImport ("cellcore. dll")]
Public static extern int ConnMgrEstablishConnectionSync (ref CONNMGR_CONNECTIONINFO ci, ref IntPtr phConnection, uint dwTimeout, ref uint pdwStatus );

[DllImport ("cellcore. dll")]
Private static extern IntPtr ConnMgrApiReadyEvent ();

[DllImport ("cellcore. dll")]
Public static extern int ConnMgrReleaseConnection (IntPtr hConnection, int bCache );

[DllImport ("cellcore. dll")]
Public static extern int ConnMgrEnumDestinations (int nIndex, ref CONNMGR_DESTINATION_INFO pDestInfo );

[DllImport ("cellcore. dll")]
Public static extern int ConnMgrConnectionStatus (IntPtr hConnection, // @ parm Handle to connection, returned from ConnMgrEstablishConnection
Ref uint pdwStatus // @ parm Returns current connection status, one of CONNMGR_STATUS _*
);

[DllImport ("coredll. dll")]
Private static extern int CloseHandle (IntPtr hObject );
};

 

Download the software as follows: (the right key of the Cross-stick is matchtime.exe, and the image can be dropped. jpg)

 

In addition, the download of the specific project and code has been passed to csdn and is being verified. I will post it to share with you!

The code used to connect the query time server to get the time and set the system time is skipped. For details, see my previous blog

This article from the CSDN blog, reproduced please indicate the source: http://blog.csdn.net/ppCuda/archive/2010/01/03/5123011.aspx

Related Article

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.