Help with an unicode String returned by DeviceIoControl
-
Hello, i have the following problem (i think it is a beginner question but i hope to get an informative answer!) Actually i try to get the BatteryDeviceName of my notebook. I use the code snipped from the MSDN. But at these step iam hanging because the DeviceIoControl returns a BATTERY_INFORMATION structure, because the InformationLevel (bqi) is set to BatteryInformation. BUT I want the BatteryDeviceName, so I changed the Information Level. Then the IOCTL_BATTERY_QUERY_INFORMATION should return a null terminated unicode string. My question is how can i store this ?string? Here is my code:
BATTERY_INFORMATION bi = {0};
bqi.InformationLevel = BatteryDeviceName;if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&bi,sizeof(bi),&dwOut,NULL))
{
MessageBox(NULL,L"#8 - IOCTL_BATTERY_QUERY_INFORMATION ... OK",L"",MB_ICONINFORMATION);
MessageBox(NULL,L"[BatteryDeviceName should be here !!!]",L"",MB_ICONINFORMATION);
}As you can see i need to replace the bold line or more to store the String. I hope someone can help me. Thanks, gabbana
-
Hello, i have the following problem (i think it is a beginner question but i hope to get an informative answer!) Actually i try to get the BatteryDeviceName of my notebook. I use the code snipped from the MSDN. But at these step iam hanging because the DeviceIoControl returns a BATTERY_INFORMATION structure, because the InformationLevel (bqi) is set to BatteryInformation. BUT I want the BatteryDeviceName, so I changed the Information Level. Then the IOCTL_BATTERY_QUERY_INFORMATION should return a null terminated unicode string. My question is how can i store this ?string? Here is my code:
BATTERY_INFORMATION bi = {0};
bqi.InformationLevel = BatteryDeviceName;if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&bi,sizeof(bi),&dwOut,NULL))
{
MessageBox(NULL,L"#8 - IOCTL_BATTERY_QUERY_INFORMATION ... OK",L"",MB_ICONINFORMATION);
MessageBox(NULL,L"[BatteryDeviceName should be here !!!]",L"",MB_ICONINFORMATION);
}As you can see i need to replace the bold line or more to store the String. I hope someone can help me. Thanks, gabbana
Maybe something like this:
wchar_t szBatteryDeviceName[260];
bqi.InformationLevel = BatteryDeviceName;if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),szBatteryDeviceName,sizeof(szBatteryDeviceName),&dwOut,NULL))
{
...
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Maybe something like this:
wchar_t szBatteryDeviceName[260];
bqi.InformationLevel = BatteryDeviceName;if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),szBatteryDeviceName,sizeof(szBatteryDeviceName),&dwOut,NULL))
{
...
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Great thank you, but i have one problem: If I use the szBatteryDeviceName in the MessageBox, he prints the correct batterydevicename but adds a part of the application path, so i think it ignores the null terminated string ?
Interesting - I wouldn't expect that. I guess you could add the null terminator yourself:
wchar_t szBatteryDeviceName[260];
bqi.InformationLevel = BatteryDeviceName;if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),szBatteryDeviceName,sizeof(szBatteryDeviceName),&dwOut,NULL))
{
szBatteryDeviceName[dwOut/sizeof(wchar_t)] = 0;
...
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Interesting - I wouldn't expect that. I guess you could add the null terminator yourself:
wchar_t szBatteryDeviceName[260];
bqi.InformationLevel = BatteryDeviceName;if(DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),szBatteryDeviceName,sizeof(szBatteryDeviceName),&dwOut,NULL))
{
szBatteryDeviceName[dwOut/sizeof(wchar_t)] = 0;
...
}Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Okay, now i tried this with another type. I only get -1 as seconds, i am not sure if this is for a constant that the estimatedtime value is not available, but is the way correct for getting them: estimated time should return as an ULONG.
ULONG estimatedtime;
bqi.InformationLevel = BatteryEstimatedTime;
DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&estimatedtime,sizeof(ULONG),&dwOut,NULL);
wchar_t szestimatedtime[260];
wsprintf(szestimatedtime,L"%d seconds",estimatedtime);
MessageBox(NULL,szestimatedtime,L"",0);Thanks for help! bye, gabbana
-
Okay, now i tried this with another type. I only get -1 as seconds, i am not sure if this is for a constant that the estimatedtime value is not available, but is the way correct for getting them: estimated time should return as an ULONG.
ULONG estimatedtime;
bqi.InformationLevel = BatteryEstimatedTime;
DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&estimatedtime,sizeof(ULONG),&dwOut,NULL);
wchar_t szestimatedtime[260];
wsprintf(szestimatedtime,L"%d seconds",estimatedtime);
MessageBox(NULL,szestimatedtime,L"",0);Thanks for help! bye, gabbana
gabbana wrote:
I only get -1 as seconds
-1 == 0xFFFFFFFF == BATTERY_UNKNOWN_TIME (BatClass.h)
Mark Salsbery Microsoft MVP - Visual C++ :java:
-
Okay, now i tried this with another type. I only get -1 as seconds, i am not sure if this is for a constant that the estimatedtime value is not available, but is the way correct for getting them: estimated time should return as an ULONG.
ULONG estimatedtime;
bqi.InformationLevel = BatteryEstimatedTime;
DeviceIoControl(hBattery,IOCTL_BATTERY_QUERY_INFORMATION,&bqi,sizeof(bqi),&estimatedtime,sizeof(ULONG),&dwOut,NULL);
wchar_t szestimatedtime[260];
wsprintf(szestimatedtime,L"%d seconds",estimatedtime);
MessageBox(NULL,szestimatedtime,L"",0);Thanks for help! bye, gabbana
From MSDN, if the estimated time is unknown (for example, the battery is not discharging and the AtRate specified was 0) this will return
BATTERY_UNKNOWN_TIME
."Love people and use things, not love things and use people." - Unknown
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne