// Batterytooldlg. cpp: Lang
//
# Include "stdafx. H"
# Include "batterytool. H"
# Include "batterytooldlg. H"
# Include <winuser. h>
# Ifdef _ debug
# Define new debug_new
# Endif
# Define ioctl_nbdevctl_port_io_read/
Ctrochelle code (file_device_unknown, 0x703, method_buffered, file_any_access)
# Define ioctl_nbdevctl_port_io_write/
Ctrochelle code (file_device_unknown, 0x704, method_buffered, file_any_access)
// The app about was blocked by caboutdlg.
Class caboutdlg: Public cdialog
{
Public:
Caboutdlg ();
// Decompress
Enum {IDD = idd_aboutbox };
Protected:
Virtual void dodataexchange (cdataexchange * PDX); // DDX/DDV pair
//
Protected:
DECLARE_MESSAGE_MAP ()
};
CAboutDlg: CAboutDlg (): CDialog (CAboutDlg: IDD)
{
}
Void CAboutDlg: DoDataExchange (CDataExchange * pDX)
{
CDialog: DoDataExchange (pDX );
}
BEGIN_MESSAGE_MAP (CAboutDlg, CDialog)
END_MESSAGE_MAP ()
// CBatteryToolDlg Blocking
CBatteryToolDlg: CBatteryToolDlg (CWnd * pParent/* = NULL */)
: CDialog (CBatteryToolDlg: IDD, pParent)
, LowerLimit (0)
, UpperLimit (100)
, IBTPowPercent (0)
, Start (false)
, BCharging (true)
, BMesageboxOpen (true)
{
// M_hIcon = AfxGetApp ()-> LoadIcon (IDR_MAINFRAME );
}
Void CBatteryToolDlg: DoDataExchange (CDataExchange * pDX)
{
CDialog: DoDataExchange (pDX );
DDX_Text (pDX, IDC_LowerLimit, LowerLimit );
DDV_MinMaxInt (pDX, LowerLimit, 0,100 );
DDX_Text (pDX, IDC_UpperLimit, UpperLimit );
DDV_MinMaxInt (pDX, UpperLimit, 0,100 );
DDX_Text (pDX, IDC_BatteryPowPercent, iBTPowPercent );
DDV_MinMaxInt (pDX, iBTPowPercent, 0,100 );
}
BEGIN_MESSAGE_MAP (CBatteryToolDlg, CDialog)
ON_WM_SYSCOMMAND ()
ON_WM_PAINT ()
ON_WM_QUERYDRAGICON ()
ON_WM_TIMER ()
//} AFX_MSG_MAP
ON_BN_CLICKED (IDOK, & CBatteryToolDlg: OnBnClickedOk)
ON_BN_CLICKED (IDC_BUTTON_Stop, & CBatteryToolDlg: OnBnClickedButtonStop)
End_message_map ()
// Cbatterytooldlg contains invalid parameter values
Bool cbatterytooldlg: oninitdialog ()
{
Cdialog: oninitdialog ();
// Callback [Min...] callback Parameter
// Idm_aboutbox skip Parameter
Assert (idm_aboutbox & 0xfff0) = idm_aboutbox );
Assert (idm_aboutbox <0xf000 );
Cmenu * psysmenu = getsystemmenu (false );
If (psysmenu! = NULL)
{
Cstring straboutmenu;
Straboutmenu. loadstring (ids_aboutbox );
If (! Straboutmenu. isempty ())
{
Psysmenu-> appendmenu (mf_separator );
Psysmenu-> appendmenu (mf_string, idm_aboutbox, straboutmenu );
}
}
// When the bank crashes, the Bank throws and cuts off the bars.
// Please wait until then
SetIcon (m_hIcon, TRUE); // you can specify
SetIcon (m_hIcon, FALSE); // you can specify
// TODO: too many other ﹚
// ***** Command argument *****
CString CommandLine = theApp. m_lpCmdLine;
CommandLine. MakeLower ();
Int index = CommandLine. Find ("/l ");
If (index! =-1)
{
CString cNum;
CNum = CommandLine. Mid (index + 2, 4 );
LowerLimit = atoi (cNum );
}
Index = CommandLine. Find ("/u ");
If (index! =-1)
{
CString cNum;
CNum = CommandLine. Mid (index + 2, 4 );
UpperLimit = atoi (cNum );
}
If (UpperLimit> 100 | LowerLimit <0 | UpperLimit <LowerLimit)
{
MessageBox ("Command argument error! Please input correct type:/r/n Batterytool.exe/u N/l L/r/nwhere N and L are numer between 0 and 100 .");
ExitProcess (0 );
}
Else if (CommandLine = "")
{
UpperLimit = 90;
LowerLimit = 70;
}
UpdateData (false );
// ***** Command argument *****
IBTPowPercent = EcIO. GetBatteryStatus ();
SetDlgItemInt (IDC_BatteryPowPercent, iBTPowPercent );
SetTimer (1,5000, (TIMERPROC) NULL );
If (EcIO. GetACStatus () & EcIO. GetChargeStatus () & (iBTPowPercent <100 ))
SetDlgItemText (IDC_BatteryStatus, "AC mode and Charging ");
Else if (iBTPowPercent = 100)
SetDlgItemText (IDC_BatteryStatus, "AC mode ");
Else
{
SetDlgItemText (IDC_BatteryStatus, "Discharging ");
BCharging = false;
}
For (int I = 0; I <2; I ++)
HThread [I] = CreateThread (NULL, 0, WhileThread, 0, create_suincluded, 0 );
// Test report
BTStatus. Open ("BatteryStatus.txt", CFile: modeCreate | CFile: modeReadWrite | CFile: typeText | CFile: shareDenyNone );
BTStatus. SeekToBegin ();
Result = 1;
Btstatus. Write ("1", 1 );
Btstatus. Close ();
Report. Open ("batteryreport.txt", cfile: modecreate | cfile: modereadwrite );
Report. seektoend ();
Report. Write ("battery Report:/R/N", 20 );
Settimer (2,60000, (timerproc) null );
Onbnclickedok ();
Return true;
}
Void cbatterytooldlg: ontimer (uint_ptr nidevent)
{
Ibtpowpercent = ecio. getbatterystatus ();
Setdlgitemint (idc_batterypowpercent, ibtpowpercent );
/* Setdlgitemint (idc_3e, ecio. getbatteryerror1 ());
Setdlgitemint (idc_3f, ecio. getbatteryerror2 ());
Setdlgitemint (idc_e8, ecio. getbatteryerror3 ());*/
BCharging = EcIO. GetACStatus ();
/* If (EcIO. GetACStatus () & EcIO. GetChargeStatus () & (iBTPowPercent <100 ))
SetDlgItemText (IDC_BatteryStatus, "AC mode and Charging ");
Else if (EcIO. GetACStatus () & EcIO. GetChargeStatus ())
SetDlgItemText (IDC_BatteryStatus, "AC mode ");
Else
SetDlgItemText (IDC_BatteryStatus, "Discharging ");*/
If (EcIO. GetACStatus () & (iBTPowPercent <100 ))
SetDlgItemText (IDC_BatteryStatus, "AC mode and Charging ");
Else if (EcIO. GetACStatus ())
SetDlgItemText (IDC_BatteryStatus, "AC mode ");
Else
SetDlgItemText (IDC_BatteryStatus, "Discharging ");
Btstatus. Open ("batterystatus.txt", cfile: modereadwrite | cfile: typetext | cfile: sharedenynone );
If (upperlimit> = ibtpowpercent & lowerlimit <= ibtpowpercent)
{
Setdlgitemtext (idc_result, "pass! ");
If (bmesageboxopen)
Closeerrormessagebox ();
If (result = 1)
{
Btstatus. seektobegin ();
Result = 0;
Btstatus. Write ("0", 1 );
}
}
Else
{
Setdlgitemtext (idc_result, "not ready ");
If (result = 0)
{
Btstatus. seektobegin ();
Result = 1;
Btstatus. Write ("1", 1 );
}
}
Btstatus. Close ();
If (start)
{
If (upperlimit <ibtpowpercent & bcharging)
{
Ecio. setchargestatus (false );
For (INT I = 0; I <2; I ++)
{
While (resumethread (hthread [I])> 1)
;
}
Bcharging = ecio. getacstatus ();
}
Else if (lowerlimit> ibtpowpercent &&! Bcharging)
{
EcIO. SetChargeStatus (true );
For (int I = 0; I <2; I ++)
SuspendThread (hThread [I]);
If (EcIO. GetACStatus ())
{
BCharging = true;
}
Else
{
Start = false;
/* GetDlgItem (IDC_BUTTON_Stop)-> EnableWindow (false );
GetDlgItem (IDOK)-> EnableWindow ();
GetDlgItem (IDC_LowerLimit)-> EnableWindow ();
GetDlgItem (IDC_UpperLimit)-> EnableWindow ();*/
BMesageboxOpen = true;
MessageBox ("Please connect AC power and try again.", "Battery Capcity Error", MB_ICONERROR );
Start = true;
}
}
If (UpperLimit <iBTPowPercent & bCharging)
{
Start = false;
/* GetDlgItem (IDC_BUTTON_Stop)-> EnableWindow (false );
GetDlgItem (IDOK)-> EnableWindow ();
GetDlgItem (IDC_LowerLimit)-> EnableWindow ();
GetDlgItem (IDC_UpperLimit)-> EnableWindow ();*/
BMesageboxOpen = true;
MessageBox ("Please remove AC power and try again.", "Battery Capcity Error", MB_ICONERROR );
Start = true;
}
}
If (iBTPowPercent> = LowerLimit & iBTPowPercent <= UpperLimit)
{
If (! BCharging)
{
For (int I = 0; I <2; I ++)
SuspendThread (hThread [I]);
}
}
If (nIDEvent = 2)
{
CString batteryreport;
CTime time = CTime: GetCurrentTime ();
Batteryreport. format ("% d/% d: % d 3E = % X, 3F = % X, E8 = % X. /r/n ", time. getMonth (), time. getDay (), time. getHour (), time. getMinute (), EcIO. getBatteryError1 (), EcIO. getBatteryError2 (), EcIO. getBatteryError3 ());
Report. Write (batteryreport, batteryreport. GetAllocLength ());
}
}
Void CBatteryToolDlg: OnSysCommand (UINT nID, LPARAM lParam)
{
If (nID & 0xFFF0) = IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
DlgAbout. DoModal ();
}
Else
{
CDialog: OnSysCommand (nID, lParam );
}
}
// When the peak traffic is reached, the parallel bars are blocked.
// The enzyme cucumber cucumulatum and zookeeper/Lang Jia Yi MFC laiyun
// Please wait until then
Void CBatteryToolDlg: OnPaint ()
{
If (IsIconic ())
{
CPaintDC dc (this); // enzyme barrier ()
SendMessage (WM_ICONERASEBKGND, reinterpret_cast <WPARAM> (dc. GetSafeHdc (), 0 );
// You can see the following information:
Int cxIcon = GetSystemMetrics (SM_CXICON );
Int cyIcon = GetSystemMetrics (SM_CYICON );
CRect rect;
GetClientRect (& rect );
Int x = (rect. Width ()-cxIcon + 1)/2;
Int y = (rect. Height ()-cyIcon + 1)/2;
// Melon seeds
Dc. DrawIcon (x, y, m_hIcon );
}
Else
{
CDialog: OnPaint ();
}
}
// Sneer, just say, Σ Cheng Yi, slack
// The Zookeeper is a companion speaker in the village.
HCURSOR CBatteryToolDlg: OnQueryDragIcon ()
{
Return static_cast <HCURSOR> (m_hIcon );
}
Void CBatteryToolDlg: OnBnClickedOk ()
{
// TODO
UpdateData ();
If (LowerLimit> = 0 & LowerLimit <= UpperLimit & UpperLimit <= 100)
{
GetDlgItem (IDC_LowerLimit)-> EnableWindow (false );
GetDlgItem (IDC_UpperLimit)-> EnableWindow (false );
Start = true;
IBTPowPercent = EcIO. GetBatteryStatus ();
SetDlgItemInt (IDC_BatteryPowPercent, iBTPowPercent );
GetDlgItem (IDC_BUTTON_Stop)-> EnableWindow ();
GetDlgItem (IDOK)-> EnableWindow (false );
If (UpperLimit <iBTPowPercent)
{
EcIO. SetChargeStatus (false );
For (int I = 0; I <2; I ++)
{
While (ResumeThread (hThread [I])> 1)
;
}
BCharging = false;
SetDlgItemText (IDC_Result, "Not ready ");
}
Else if (LowerLimit> iBTPowPercent)
{
EcIO. SetChargeStatus (true );
If (EcIO. GetACStatus ())
{
BCharging = true;
SetDlgItemText (IDC_Result, "Not ready ");
}
Else
{
Start = false;
/* GetDlgItem (IDC_BUTTON_Stop)-> EnableWindow (false );
GetDlgItem (IDOK)-> EnableWindow ();
GetDlgItem (IDC_LowerLimit)-> EnableWindow ();
GetDlgItem (IDC_UpperLimit)-> EnableWindow ();*/
BMesageboxOpen = true;
MessageBox ("Please connect AC power and try again.", "Battery Capcity Error", MB_ICONERROR );
Start = true;
}
}
Else
{
SetDlgItemText (IDC_Result, "PASS! ");
}
}
If (LowerLimit> = UpperLimit)
AfxMessageBox ("LowerLimit must less than UpperLimit ");
}
Void CBatteryToolDlg: GetShutdownPrivilege ()
{
HANDLE htoken;
TOKEN_PRIVILEGES tp;
LUID luid;
OpenProcessToken (GetCurrentProcess (), TOKEN_ADJUST_PRIVILEGES, & htoken );
If (! LookupPrivilegeValue (
NULL, // lookup privilege on local system
SE_SHUTDOWN_NAME, // privilege to lookup
& Luid) // es LUID of privilege
{
EcIO. ShowError (GetLastError ());
}
Tp. PrivilegeCount = 1;
Tp. Privileges [0]. Luid = luid;
Tp. Privileges [0]. Attributes = SE_PRIVILEGE_ENABLED;
// Enable the privilege or disable all privileges.
If (! AdjustTokenPrivileges (
Htoken,
FALSE,
& Tp,
Sizeof (TOKEN_PRIVILEGES ),
(PTOKEN_PRIVILEGES) NULL,
(PDWORD) NULL ))
{
EcIO. ShowError (GetLastError ());
}
}
Void CBatteryToolDlg: OnBnClickedButtonStop ()
{
Start = false;
EcIO. SetChargeStatus (true );
For (int I = 0; I <2; I ++)
SuspendThread (hThread [I]);
GetDlgItem (IDC_BUTTON_Stop)-> EnableWindow (false );
GetDlgItem (IDOK)-> EnableWindow ();
GetDlgItem (IDC_LowerLimit)-> EnableWindow ();
Getdlgitem (idc_upperlimit)-> enablewindow ();
}
DWORD winapi whilethread (lpvoid lpparam)
{
While (true)
;
Return 0;
}
Void cbatterytooldlg: oncancel ()
{
Ecio. setchargestatus (true );
Btstatus. Close ();
Cdialog: oncancel ();
}
Void cbatterytooldlg: closeerrormessagebox ()
{
Cwnd * messageh = findwindow (null, "battery capcity error ");
If (messageh! = NULL)
{
: Postmessage (messageh-> m_hwnd, wm_close, 0, 0 );
Bmesageboxopen = false;
}
}