Eth.
Eth.

Reputation: 3

Getting battery information for Laptop and put it in label in C++

I am doing an App for laptops Called Batterlizer, It will get all the information about the battery, So everything was going well until I wanted to get the battery percent and other things , so here is my code:

SYSTEM_POWER_STATUS BatteryPower;

if(GetSystemPowerStatus( &BatteryPower ))

{

long unsigned int BatteryFull = BatteryPower.BatteryLifeTime;

const char BatteryFullTime[900] = {BatteryFull};

BatteryLeftText =  CreateWindow("static", "Battery Life:",WS_CHILD | 

WS_VISIBLE | WS_TABSTOP, 0, 0, 100, 20,hwnd, (HMENU)(501),(HINSTANCE) 

GetWindowLong (hwnd, GWL_HINSTANCE), NULL);

UpdatedBatteryText = CreateWindow("static",BatteryFullTime, WS_VISIBLE | 

WS_CHILD | WS_TABSTOP, 90, 0, 50, 20, hwnd, (HMENU)(501),(HINSTANCE) 

GetWindowLong (hwnd, GWL_HINSTANCE),NULL);

}

the problem is , it says strange letters when it comes to BatteryFullTime, Any Ideas guys?

Upvotes: 0

Views: 271

Answers (2)

MSalters
MSalters

Reputation: 179907

You're using C instead of C++, that makes it harder. In C++ :

std::string BatteryFullTime = std::to_string(BatteryFull);

No need to care about length, or remember details of the conversion function. However:

SetWindowText(UpdatedBatteryText, BatteryFullTime.c_str());

The Windows function does need a C string, but it's easy to get a C string from the C++ string.

Upvotes: 1

JCM
JCM

Reputation: 197

Try the following:

   char szBatteryLifeBuffer[900] = {0}; 
   sprintf(szBatteryLifeBuffer, "%lu", BatteryFull); 
   SetWindowText(UpdatedBatteryText, szBatteryLifeBuffer);

   // This also should work, too. 
   // SendMessage(UpdatedBatteryText, WM_SETTEXT, 0, (LPARAM)(LPTSTR)szBatteryLifeBuffer); 

Get rid of

// This does not work.
const char BatteryFullTime[900] = {BatteryFull};

Upvotes: 2

Related Questions