Answered by:
Visual C++ basic program error

Question
-
Hi guys.I'm new in programing and i'm getting this error...
Run-Time Check Failure #3 - The variable 'x' is being used without being initialized.
...in this program
#include<windows.h>
int _stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
SYSTEM_POWER_STATUS x;
if (x.BatteryFlag == 8)
MessageBox(NULL,L"Battery is Charging.",L"Caution!",MB_ICONINFORMATION);
}tell me please,what i'm doing wrong and if this program is able to get know if my laptop battery is charging.thnx.
Monday, January 26, 2015 1:49 PM
Answers
-
On 1/26/2015 8:49 AM, DkMandar wrote:
SYSTEM_POWER_STATUS x;
if (x.BatteryFlag == 8)How do you expect x.BatteryFlag to acquire the value of 8 (or any other value)? Do you believe the structure will magically fill itself?
The documentation for SYSTEM_POWER_STATUS mentions the function you should use to initialize it.
Igor Tandetnik- Proposed as answer by John Boncek Monday, January 26, 2015 2:54 PM
- Unproposed as answer by DkMandar Monday, January 26, 2015 4:31 PM
- Marked as answer by DkMandar Monday, January 26, 2015 6:07 PM
Monday, January 26, 2015 2:15 PM -
its not working @Wyck.giving an error that illegal use of data type BOOL in an expression.
thnx for reply anyway!
My code worked for me:
#include <windows.h> #include <iostream> int main() { SYSTEM_POWER_STATUS status; ZeroMemory( &status, sizeof( status ) ); BOOL result = GetSystemPowerStatus( &status ); if( !result ) { // FAILED to get power status return -1; } bool isCharging = (status.BatteryFlag & 8) != 0; std::cout << "Charging: " << std::boolalpha << isCharging << std::endl; return 0; }
And your code compiles too:
#include<windows.h> int _stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){ SYSTEM_POWER_STATUS status; GetSystemPowerStatus(&status); if(status.BatteryFlag == 8) MessageBox(NULL,L"Battery is charging",L"caution",MB_ICONINFORMATION); }
So there's something you're not telling me.
What code are you compiling? What was the error message? What version of VC are you using?
- Marked as answer by DkMandar Monday, January 26, 2015 6:08 PM
Monday, January 26, 2015 4:46 PM -
Does status.BatteryFlag == 8? What value does it have?
You don't check the error code from GetSystemPowerStatus so I have no idea whether that call failed or succeeded.
You also don't handle the case that status.BatteryFlag == 9. This would be the case that the battery is charging and contains more than 66%. You also don't handle 10 or 12, or potential future cases that haven't been defined yet. You really want
if(status.BatteryFlag & 8) MessageBox(NULL,L"Battery is charging",L"caution",MB_ICONINFORMATION);
But put a breakpoint on the if statement and look for yourself at what status is.
- Marked as answer by DkMandar Monday, January 26, 2015 6:06 PM
Monday, January 26, 2015 5:24 PM
All replies
-
What do you need as a more detailed explanation? I think "The variable 'x' is being used without being initialized."is a very helpful error message.
Maybe change the line
SYSTEM_POWER_STATUS x;
to this
SYSTEM_POWER_STATUS x; x=ZeroMemory(&x, sizeof(SYSTEM_POWER_STATUS));
ZeroMemory initialize the structure SYSTEM_POWER_STATUS with zeros.
https://msdn.microsoft.com/en-us/library/windows/desktop/aa366920(v=vs.85).aspx
Best regards
Bordon
Note: Posted code pieces may not have a good programming style and may not perfect. It is also possible that they do not work in all situations. Code pieces are only indended to explain something particualar.- Edited by Bordon Monday, January 26, 2015 1:56 PM
Monday, January 26, 2015 1:56 PM -
i don't still understand that why it was necessary but it worked.
yet it is giving a new error message
'=' : cannot convert from 'void *' to 'SYSTEM_POWER_STATUS'
thnx for reply!
Monday, January 26, 2015 2:03 PM -
On 1/26/2015 8:49 AM, DkMandar wrote:
SYSTEM_POWER_STATUS x;
if (x.BatteryFlag == 8)How do you expect x.BatteryFlag to acquire the value of 8 (or any other value)? Do you believe the structure will magically fill itself?
The documentation for SYSTEM_POWER_STATUS mentions the function you should use to initialize it.
Igor Tandetnik- Proposed as answer by John Boncek Monday, January 26, 2015 2:54 PM
- Unproposed as answer by DkMandar Monday, January 26, 2015 4:31 PM
- Marked as answer by DkMandar Monday, January 26, 2015 6:07 PM
Monday, January 26, 2015 2:15 PM -
You first have to call GetSystemPowerStatus( &x ); To set the values for the structure.
Also the BatteryFlag is a bit field that may contain other values so an appropriate check for charging is something like this:
SYSTEM_POWER_STATUS status; ZeroMemory(&status, sizeof(status)); BOOL result = GetSystemPowerStatus(&status); if (!result) { // FAILED to get power status return -1; } bool isCharging = (status.BatteryFlag & 8) != 0;
Monday, January 26, 2015 3:19 PM -
alright.as u said @Igor Tandetnik,i tried hard.but i'm beginner and still doing something wrong.please identify my mistake.here is code>
#include<windows.h>
int _stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
SYSTEM_POWER_STATUS status;
GetSystemPowerStatus(&status);
if(status.BatteryFlag == 8)
MessageBox(NULL,L"Battery is charging",L"caution",MB_ICONINFORMATION);
}- Edited by DkMandar Monday, January 26, 2015 4:35 PM
Monday, January 26, 2015 4:15 PM -
its not working @Wyck.giving an error that illegal use of data type BOOL in an expression.
thnx for reply anyway!
Monday, January 26, 2015 4:21 PM -
SomeOne help me please...Monday, January 26, 2015 4:32 PM
-
What isn't working? Is the compiler giving error messages (what is the message, what line?)
Does the code not do what you expect? If you use the debugger and put a breakpoint on your if statement, what value does status.BatteryFlag have?
Monday, January 26, 2015 4:35 PM -
@ Wyck
program is supposed to show an massage of "battery is Charging",when i connect it to adapter.but instead it is showing these lines in output window...
'Learning_first.exe': Loaded 'C:\Users\DkMandar\Documents\Visual Studio 2010\Projects\Learning_first\Debug\Learning_first.exe', Symbols loaded.
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\ntdll.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\kernel32.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\KernelBase.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\user32.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\msvcr100d.dll', Symbols loaded.
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\gdi32.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\imm32.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\msctf.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\msvcrt.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\Jaksta\AC\x86\jaudcap.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\advapi32.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\ole32.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\sechost.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\rpcrt4.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\combase.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\sspicli.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\cryptbase.dll', Cannot find or open the PDB file
'Learning_first.exe': Loaded 'C:\Windows\SysWOW64\bcryptprimitives.dll', Cannot find or open the PDB file
1144) CheckProcessInRegistry: Got include list but no match was found.
1144) IsProcessAllowed(C:\Users\DkMandar\documents\visual studio 2010\Projects\Learning_first\Debug\Learning_first.exe) (80004005)
1144) DllMain(ProcDetach) [OK]
1144) DSound_Unhook
1144) MCIWave_Unhook
1144) AudioClient_Unhook
1144) CAudioStreamMgr::Shutdown
'Learning_first.exe': Unloaded 'C:\Windows\Jaksta\AC\x86\jaudcap.dll'
'Learning_first.exe': Unloaded 'C:\Windows\SysWOW64\combase.dll'
'Learning_first.exe': Unloaded 'C:\Windows\SysWOW64\sechost.dll'
'Learning_first.exe': Unloaded 'C:\Windows\SysWOW64\rpcrt4.dll'
'Learning_first.exe': Unloaded 'C:\Windows\SysWOW64\sspicli.dll'
'Learning_first.exe': Unloaded 'C:\Windows\SysWOW64\cryptbase.dll'
'Learning_first.exe': Unloaded 'C:\Windows\SysWOW64\ole32.dll'
'Learning_first.exe': Unloaded 'C:\Windows\SysWOW64\advapi32.dll'
The program '[4420] Learning_first.exe: Native' has exited with code 1 (0x1).
and doing nothing else!
thnx for ur reply.
- Edited by DkMandar Monday, January 26, 2015 4:54 PM
Monday, January 26, 2015 4:41 PM -
its not working @Wyck.giving an error that illegal use of data type BOOL in an expression.
thnx for reply anyway!
My code worked for me:
#include <windows.h> #include <iostream> int main() { SYSTEM_POWER_STATUS status; ZeroMemory( &status, sizeof( status ) ); BOOL result = GetSystemPowerStatus( &status ); if( !result ) { // FAILED to get power status return -1; } bool isCharging = (status.BatteryFlag & 8) != 0; std::cout << "Charging: " << std::boolalpha << isCharging << std::endl; return 0; }
And your code compiles too:
#include<windows.h> int _stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){ SYSTEM_POWER_STATUS status; GetSystemPowerStatus(&status); if(status.BatteryFlag == 8) MessageBox(NULL,L"Battery is charging",L"caution",MB_ICONINFORMATION); }
So there's something you're not telling me.
What code are you compiling? What was the error message? What version of VC are you using?
- Marked as answer by DkMandar Monday, January 26, 2015 6:08 PM
Monday, January 26, 2015 4:46 PM -
output shows some lines in output window when i compile MY code.and i told already which lines.
i'm using VC++ 2010.
Monday, January 26, 2015 4:49 PM -
Does status.BatteryFlag == 8? What value does it have?
You don't check the error code from GetSystemPowerStatus so I have no idea whether that call failed or succeeded.
You also don't handle the case that status.BatteryFlag == 9. This would be the case that the battery is charging and contains more than 66%. You also don't handle 10 or 12, or potential future cases that haven't been defined yet. You really want
if(status.BatteryFlag & 8) MessageBox(NULL,L"Battery is charging",L"caution",MB_ICONINFORMATION);
But put a breakpoint on the if statement and look for yourself at what status is.
- Marked as answer by DkMandar Monday, January 26, 2015 6:06 PM
Monday, January 26, 2015 5:24 PM -
yep u r right @SimonRev. it was not battery flag status 8.thats why it was not showing msg.so i used ACLineStatus instead, and it worked. here is code>
#include<windows.h>
int _stdcall WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int nCmdShow){
SYSTEM_POWER_STATUS status;
GetSystemPowerStatus(&status);
if(status.ACLineStatus == 1)
MessageBox(NULL,L"Battery is charging",L"caution",MB_ICONINFORMATION);
}
it shows a msg if adapter is connected.now i need to work to upgrade this program to show a msg when battery get fully charged. u guys were right too @Igor Tandetnik, and @Wyck.thank u all guys for helping me.
Monday, January 26, 2015 6:17 PM