Detect power change event / UPS state - c++

I want to detect when the system is on UPS or just power state changes.
Something like this:
I tried to detect it using GetSystemPowerStatus function:
Code:
SYSTEM_POWER_STATUS powerStatus;
bool powerStatusRes = GetSystemPowerStatus(&powerStatus);
switch (powerStatus.ACLineStatus) {
case 0:
std::cout << "Offline" << std::endl;
break;
case 1:
std::cout << "Online" << std::endl;
break;
case 255:
std::cout << "Unknown status" << std::endl;
default:
std::cout << "Failed to detect the status" << std::endl;
}
switch (powerStatus.BatteryFlag) {
case 1:
std::cout << "High — the battery capacity is at more than 66 percent" << std::endl;
break;
case 2:
std::cout << "Low — the battery capacity is at less than 33 percent" << std::endl;
break;
case 4:
std::cout << "Critical — the battery capacity is at less than five percent" << std::endl;
break;
case 8:
std::cout << "Charging" << std::endl;
break;
case 128:
std::cout << "No system battery" << std::endl;
break;
case 255:
std::cout << "Unknown status—unable to read the battery flag information" << std::endl;
break;
default:
std::cout << "Failed to detect the battery flag" << std::endl;
}
But it returns powerStatus.ACLineStatus for UPS or AC as Online and for powerStatus.BatteryFlag - No system battery.
Also I have tried to detect the UPS using GetPwrCapabilities function:
Code:
SYSTEM_POWER_CAPABILITIES SysPowerCapabilities = {0};
if (!GetPwrCapabilities(&SysPowerCapabilities)){
std::cout << "Failed to get System Power information!" << std::endl;
}
if (SysPowerCapabilities.UpsPresent) {
std::cout << "UPS found" << std::endl;
} else {
std::cout << "UPS not found" << std::endl;
}
It returns - UPS not found (driver is not available for this UPS model).
Now I'm trying to detect power changes by using the Qt nativeEvent function:
bool Test::nativeEvent(const QByteArray &eventType, void *message, long *result)
{
Q_UNUSED(result);
Q_UNUSED(eventType);
MSG *msg = static_cast<MSG*>(message);
if (msg->message == WM_POWERBROADCAST && msg->wParam == PBT_APMPOWERSTATUSCHANGE) {
qDebug() << "Power changed!!!";
}
return false;
}
But nothing is printed to the console. Any ideas how to detect it? Thanks.
Updated: 03.02.2019
I have found that I need to use the HID API to get some values for the UPS. Some of the values for UPS from the net.
0x00840010 = UPS
0x00840012 = Battery
0x00840030 = Voltage
0x00840040 = ConfigVoltage
0x0084001a = Input
0x0084005a = AudibleAlarmControl
0x00840002 = PresentStatus
0x00850044 = Charging
0x00850045 = Discharging
0x008500d0 = ACPresent
Updated code:
QString devicePath = "\\\\?\\HID#VID_....";
HANDLE fileHandle = CreateFileA(devicePath.toStdString().c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_FLAG_OVERLAPPED, NULL);
if (fileHandle != INVALID_HANDLE_VALUE) {
std::cout << "Success. Device opened for reading..." << std::endl;
PHIDP_PREPARSED_DATA preparsedData;
HIDP_CAPS capabilities;
HIDD_ATTRIBUTES attributes;
BOOL hidPreparsedRes = HidD_GetPreparsedData(fileHandle, &preparsedData);
if (hidPreparsedRes) {
if (HidD_GetAttributes(fileHandle, &attributes)) {
std::cout << "Product ID: " << attributes.ProductID << std::endl;
std::cout << "Size: " << attributes.Size << std::endl;
std::cout << "Vendor ID: " << attributes.VendorID << std::endl;
std::cout << "Version number: " << attributes.VersionNumber << std::endl;
if (HidP_GetCaps(preparsedData, &capabilities) == HIDP_STATUS_SUCCESS) {
std::cout << "Caps: " << capabilities.NumberOutputValueCaps << std::endl;
} else {
std::cout << "Failed to return HID capabilities!" << std::endl;
}
} else {
std::cout << "Failed to get HID attributes" << std::endl;
}
std::cout << getLastErrorAsString() << std::endl;
} else {
std::cout << "Failed to get preparsed data!" << std::endl;
}
HidD_FreePreparsedData(preparsedData);
} else {
std::cout << "Failed to open device for reading!" << std::endl;
}
CloseHandle(fileHandle);
So now, it returns:
Success. Device opened for reading...
Product ID: 20833
Size: 12
Vendor ID: 1637
Version number: 2
Caps: 1
HIDP_CAPS structure has a lot of different values, the question is how to convert them to UPS values to check for power changes/UPS status? Thanks.

Related

Not able to receive the OnBarCodeEvent for zebra scanner in linux c++

I am using the zebra linux sdk for scanner to capture the barcode that is scanned.
I want to listen to the OnBarcodeEvent and capture the code.
I have tried the sample console-app code and gui app code
I have called the
::ExecCommand( CMD_REGISTER_FOR_EVENTS , inXml, outXml, &eStatus);
The OnBarcodeEvent doesn't get invoked when I scan a bar code.
Code provided below.
void SampleEventListener::connect_corescanner()
{
cout <<"connect_corescanner startp"<< endl;
int iScannerType = SCANNER_TYPE_ALL;
StatusID eStatus ;
::Open(this, SCANNER_TYPE_ALL , &eStatus );
std::cout << "Open eStatus line 634 ConsoleSampleEventListener.cpp" << ": " << eStatus << std::endl;
if ( eStatus != STATUS_OK )
{
cout <<eStatus<< endl;
cout <<"can't connect to the corescanner. "<< endl;
exit(0);
}
// register for all events //
std::string inXml;
std::ostringstream oss;
oss << "<inArgs>" << std::endl;
oss << " <cmdArgs>" << std::endl;
oss << " <arg-int>6</arg-int>" << std::endl;
oss << " <arg-int>1,2,4,8,16,32</arg-int>" << std::endl;
oss << " </cmdArgs>" << std::endl;
oss << "</inArgs>" ;
inXml = oss.str();
std::string outXml;
::ExecCommand( CMD_REGISTER_FOR_EVENTS , inXml, outXml, &eStatus);
std::cout << "ExecCommand eStatus line 658 ConsoleSampleEventListener.cpp" << ": " << eStatus << std::endl;
if( eStatus != STATUS_OK ){
cout <<"unable to register for events"<< endl;
exit(-1);
}
cout <<"subscribe to events successful."<< outXml;
cout <<"\nsubscribe to events successful."<< endl;
cout <<"connect_corescanner endp\n"<< endl;
}
void SampleEventListener::OnBarcodeEvent(short int eventType, std::string & pscanData)
{
cout << "Barcode Detected" << endl;
cout << "Out XML" << endl;
cout << pscanData << endl;
}

C++ - WinAPI get list of all connected USB devices

I'm trying to create a program that will categorize all the connected USB devices and their port GUID.
I found an example of how to get all the information from connected input devices:
#include <windows.h>
#include <iostream>
// Namespace
using namespace std;
// Main
int main()
{
// Program
cout << "USB Device Lister." << endl;
// Get Number Of Devices
UINT nDevices = 0;
GetRawInputDeviceList(NULL, &nDevices, sizeof(RAWINPUTDEVICELIST));
// Got Any?
if (nDevices < 1)
{
// Exit
cout << "ERR: 0 Devices?";
cin.get();
return 0;
}
// Allocate Memory For Device List
PRAWINPUTDEVICELIST pRawInputDeviceList;
pRawInputDeviceList = new RAWINPUTDEVICELIST[sizeof(RAWINPUTDEVICELIST) * nDevices];
// Got Memory?
if (pRawInputDeviceList == NULL)
{
// Error
cout << "ERR: Could not allocate memory for Device List.";
cin.get();
return 0;
}
// Fill Device List Buffer
int nResult;
nResult = GetRawInputDeviceList(pRawInputDeviceList, &nDevices, sizeof(RAWINPUTDEVICELIST));
// Got Device List?
if (nResult < 0)
{
// Clean Up
delete[] pRawInputDeviceList;
// Error
cout << "ERR: Could not get device list.";
cin.get();
return 0;
}
// Loop Through Device List
for (UINT i = 0; i < nDevices; i++)
{
// Get Character Count For Device Name
UINT nBufferSize = 0;
nResult = GetRawInputDeviceInfo(pRawInputDeviceList[i].hDevice, // Device
RIDI_DEVICENAME, // Get Device Name
NULL, // NO Buff, Want Count!
&nBufferSize); // Char Count Here!
// Got Device Name?
if (nResult < 0)
{
// Error
cout << "ERR: Unable to get Device Name character count.. Moving to next device." << endl << endl;
// Next
continue;
}
// Allocate Memory For Device Name
WCHAR* wcDeviceName = new WCHAR[nBufferSize + 1];
// Got Memory
if (wcDeviceName == NULL)
{
// Error
cout << "ERR: Unable to allocate memory for Device Name.. Moving to next device." << endl << endl;
// Next
continue;
}
// Get Name
nResult = GetRawInputDeviceInfo(pRawInputDeviceList[i].hDevice, // Device
RIDI_DEVICENAME, // Get Device Name
wcDeviceName, // Get Name!
&nBufferSize); // Char Count
// Got Device Name?
if (nResult < 0)
{
// Error
cout << "ERR: Unable to get Device Name.. Moving to next device." << endl << endl;
// Clean Up
delete[] wcDeviceName;
// Next
continue;
}
// Set Device Info & Buffer Size
RID_DEVICE_INFO rdiDeviceInfo;
rdiDeviceInfo.cbSize = sizeof(RID_DEVICE_INFO);
nBufferSize = rdiDeviceInfo.cbSize;
// Get Device Info
nResult = GetRawInputDeviceInfo(pRawInputDeviceList[i].hDevice,
RIDI_DEVICEINFO,
&rdiDeviceInfo,
&nBufferSize);
// Got All Buffer?
if (nResult < 0)
{
// Error
cout << "ERR: Unable to read Device Info.. Moving to next device." << endl << endl;
// Next
continue;
}
// Mouse
if (rdiDeviceInfo.dwType == RIM_TYPEMOUSE)
{
// Current Device
cout << endl << "Displaying device " << i + 1 << " information. (MOUSE)" << endl;
wcout << L"Device Name: " << wcDeviceName << endl;
cout << "Mouse ID: " << rdiDeviceInfo.mouse.dwId << endl;
cout << "Mouse buttons: " << rdiDeviceInfo.mouse.dwNumberOfButtons << endl;
cout << "Mouse sample rate (Data Points): " << rdiDeviceInfo.mouse.dwSampleRate << endl;
if (rdiDeviceInfo.mouse.fHasHorizontalWheel)
{
cout << "Mouse has horizontal wheel" << endl;
}
else
{
cout << "Mouse does not have horizontal wheel" << endl;
}
}
// Keyboard
else if (rdiDeviceInfo.dwType == RIM_TYPEKEYBOARD)
{
// Current Device
cout << endl << "Displaying device " << i + 1 << " information. (KEYBOARD)" << endl;
wcout << L"Device Name: " << wcDeviceName << endl;
cout << "Keyboard mode: " << rdiDeviceInfo.keyboard.dwKeyboardMode << endl;
cout << "Number of function keys: " << rdiDeviceInfo.keyboard.dwNumberOfFunctionKeys << endl;
cout << "Number of indicators: " << rdiDeviceInfo.keyboard.dwNumberOfIndicators << endl;
cout << "Number of keys total: " << rdiDeviceInfo.keyboard.dwNumberOfKeysTotal << endl;
cout << "Type of the keyboard: " << rdiDeviceInfo.keyboard.dwType << endl;
cout << "Subtype of the keyboard: " << rdiDeviceInfo.keyboard.dwSubType << endl;
}
// Some HID
else // (rdi.dwType == RIM_TYPEHID)
{
// Current Device
cout << endl << "Displaying device " << i + 1 << " information. (HID)" << endl;
wcout << L"Device Name: " << wcDeviceName << endl;
cout << "Vendor Id:" << rdiDeviceInfo.hid.dwVendorId << endl;
cout << "Product Id:" << rdiDeviceInfo.hid.dwProductId << endl;
cout << "Version No:" << rdiDeviceInfo.hid.dwVersionNumber << endl;
cout << "Usage for the device: " << rdiDeviceInfo.hid.usUsage << endl;
cout << "Usage Page for the device: " << rdiDeviceInfo.hid.usUsagePage << endl;
}
// Delete Name Memory!
delete[] wcDeviceName;
}
// Clean Up - Free Memory
delete[] pRawInputDeviceList;
// Exit
cout << endl << "Finnished.";
cin.get();
return 0;
}
I tried to convert this code to get all the connected USB devices but failed.
So my question is what is the best way to collect the data I'm looking for?
If you want all USB devices, not just "input" devices, then you need to use the same APIs that Device Manager does.
For example, all devices shown by Device Manager can be listed with the help of the SetupDiGetClassDevs function.
For listing USB devices, you'll want to use the enumerator parameter set to "USB" (the enumerator is the bus where the device is attached, for example it can be "PCI", "PCMCIA", "USB" for the main computer busses, and it can also be a secondary bus provided by an expansion device, e.g. "SCSI", "FTDIBUS", and so on). You may sometimes find that you're more interested in child devices than the USB-attached parent device itself.
Keep in mind that it is not enough just to comment // (rdi.dwType == RIM_TYPEHID) as you did in following sequence, since it will print only rdiDeviceInfo.hid.SOMETHING info. If device is not HID, I would expect some junk, or similar to be printed.
else // (rdi.dwType == RIM_TYPEHID)
{
// Current Device
cout << endl << "Displaying device " << i + 1 << " information. (HID)" << endl;
wcout << L"Device Name: " << wcDeviceName << endl;
cout << "Vendor Id:" << rdiDeviceInfo.hid.dwVendorId << endl;
cout << "Product Id:" << rdiDeviceInfo.hid.dwProductId << endl;
cout << "Version No:" << rdiDeviceInfo.hid.dwVersionNumber << endl;
cout << "Usage for the device: " << rdiDeviceInfo.hid.usUsage << endl;
cout << "Usage Page for the device: " << rdiDeviceInfo.hid.usUsagePage << endl;
}
I would recommend you to add some logging and breakpoints (to print number of devices, to print all device names etc.). If you don't solve it, please paste the code that you use for testing with precise issue explanation.
UPDATE:
GetRawInputDeviceList function
Remarks
The devices returned from this function are the mouse, the keyboard, and other Human Interface Device (HID) devices.
If your device is not HID don't expect to see it in a list.

Playing a sound in a MIDI Callback function

I got this little piece of code which is working fine for now. However I want to play a sound in the callback function but I read in the MSDN pages:
"Applications should not call any multimedia functions from inside the callback function, as doing so can cause a deadlock. Other system functions can safely be called from the callback".
I'm quite new to programming and my question is: How can I work around this and still be able to play sounds when a MIDI-key is hit.
#include<iostream>
#include<cstdlib>
#include<windows.h>
#include<Mmsystem.h>
#include<stdio.h>
using namespace std;
void CALLBACK midiCallback(HMIDIIN handle, UINT uMsg, DWORD dwInstance, DWORD dwParam1, DWORD dwParam2)
{
switch (uMsg)
{
case MIM_OPEN:
cout << "-----OPENED.-----" << endl;
break;
case MIM_CLOSE:
cout << "-----EVERYTHING IS CLOSING.-----" << endl;
break;
case MIM_DATA:
cout << "-----APPARENTLY THERE I5 DATA.-----" << endl;
break;
case MIM_LONGDATA:
cout << "-----LONGDATA'D.-----" << endl;
break;
case MIM_ERROR:
cout << "-----ERROR.-----" << endl;
break;
case MIM_LONGERROR:
cout << "-----LONGERROR. EVEN WORSE.-----" << endl;
break;
}
cout << "dwInstance is " << dwInstance << endl;
cout << "Handle is " << handle << endl;
cout << "dwParam1 is " << dwParam1 << endl; //dwParam1 is the bytes of the MIDI Message packed into an unsigned long
cout << "dwParam1_hiword is " << HIWORD(dwParam1) << endl; //velocity
cout << "dwParam1_loword is " << LOWORD(dwParam1) << endl; //keyID
cout << "dwParam2 is " << dwParam2 << endl; //dwParam2 is the timestamp of key press
cout << "uMsg is " << uMsg << endl;
cout << "-----" << endl;
}
void MidiThing() {
MIDIINCAPS mic;
unsigned long result;
HMIDIIN inHandle;
unsigned long iNumDevs, i;
iNumDevs = midiInGetNumDevs(); /* Get the number of MIDI In devices in this computer */
/* Go through all of those devices, displaying their names */
for (i = 0; i < iNumDevs; i++)
{
/* Get info about the next device */
if (!midiInGetDevCaps(i, &mic, sizeof(MIDIINCAPS)))
{
/* Display its Device ID and name */
cout << "Device ID [" << i << "]: " << mic.szPname << endl;
}
}
cout << endl;
// Open the default MIDI In device. (DevID 0)
result = midiInOpen(&inHandle, 0, (DWORD)midiCallback, 0, CALLBACK_FUNCTION);
if (result != MMSYSERR_NOERROR) {
cout << "midiInOpen() failed...rv= " << result << endl;
}
else
{
midiInStart(inHandle);
}
cout << endl;
cout << "Press \"ESC\" to quit." << endl;
while (1) {
if (GetAsyncKeyState(VK_ESCAPE))
{
break;
cout << "exit=true." << endl;
}
Sleep(100);
}
midiInStop(inHandle);
midiInReset(inHandle);
midiInClose(inHandle);
cout << endl;
cout << inHandle << " was the MIDIIN handle." << endl;
cout << endl;
cout << "MIDI is closed now." << endl;
}
int main(int argc, char *argv[])
{
MidiThing();
cout << "Exit is success." << endl;
return EXIT_SUCCESS;
}
Wake up another thread from the callback.

Getting process ID's

My program below will concatenate the names of the processes into the names string. How can I change it to include the process ID's instead of names? What type should names be, how to initialise it and how to concatenate every proc32.th32ProcessID in it ?
PROCESSENTRY32 proc32;
TCHAR names[MAX_PATH]=L"";
if(hSnap == INVALID_HANDLE_VALUE)
{
cout<<"invalid handle value error!\n";
return;
}
proc32.dwSize = sizeof(PROCESSENTRY32);
if(!Process32First(hSnap, &proc32))
{
cout<<"Tread32First() error!\n";
CloseHandle(hSnap);
return ;
}
do
{
//cout<<"Current process id: "<<GetCurrentProcessId()<<"\n";
wcout<<L"Process Name: "<<proc32.szExeFile<<"\n";
cout<<"Process ID: " <<proc32.th32ProcessID<<"\n";
cout<<"Thread Count: "<<proc32.cntThreads<<"\n"<<endl;
lstrcat(names, proc32.szExeFile);
lstrcat(names, L"\n");
}while(Process32Next(hSnap, &proc32));
Since you are using C++ anyway, you should make use of it. Use std::vector, std::wstring, etc:
PROCESSENTRY32W proc32;
vector<wstring> names;
vector<DWORD> ids;
if (hSnap == INVALID_HANDLE_VALUE)
{
cout << "invalid handle value error!" << endl;
return;
}
proc32.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(hSnap, &proc32))
{
cout << "Tread32First() error!" << endl;
CloseHandle(hSnap);
return ;
}
do
{
//cout << "Current process id: " << GetCurrentProcessId() << endl;
wcout << L"Process Name: " << proc32.szExeFile << endl;
cout << "Process ID: " << proc32.th32ProcessID << endl;
cout << "Thread Count: " << proc32.cntThreads << endl << endl;
names.push_back(proc32.szExeFile);
ids.push_back(proc32.th32ProcessID);
}
while (Process32Next(hSnap, &proc32));
// use names and ids as needed...
Or:
PROCESSENTRY32W proc32;
vector<PROCESSENTRY32W> procs;
if (hSnap == INVALID_HANDLE_VALUE)
{
cout << "invalid handle value error!" << endl;
return;
}
proc32.dwSize = sizeof(PROCESSENTRY32W);
if (!Process32FirstW(hSnap, &proc32))
{
cout << "Tread32First() error!" << endl;
CloseHandle(hSnap);
return ;
}
do
{
//cout << "Current process id: " << GetCurrentProcessId() << endl;
wcout << L"Process Name: " << proc32.szExeFile << endl;
cout << "Process ID: " << proc32.th32ProcessID << endl;
cout << "Thread Count: " << proc32.cntThreads << endl << endl;
procs.push_back(proc32);
}
while (Process32Next(hSnap, &proc32));
// use procs as needed...

SendMessage not getting text

Not sure why SendMessage isn't getting the text from the class I need. I have done this before but it was is VisualBasic and I wanted to port it over to c++. I have not tried this code on any other program. I was reading something about it possibly being unicode but I wasn't sure on how to implement that.
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
using namespace std;
void FindmIRC()
{
cout << "[mIRC]" << endl;
cout << "- find mIRC window" << endl;
HWND hwndmIRC = FindWindow(L"mIRC", NULL);
if (NULL != hwndmIRC)
{
cout << " + found mIRC window" << endl;
cout << "- find MDIClient window" << endl;
HWND hwndMDIClient = FindWindowEx(hwndmIRC, NULL, L"MDIClient", NULL);
if (NULL != hwndMDIClient)
{
cout << " + found MDIClient window" << endl;
cout << "- find mIRC_Channel window" << endl;
HWND hwndmIRC_Channel = FindWindowEx(hwndMDIClient, NULL, L"mIRC_Channel", NULL);
if (NULL != hwndmIRC_Channel)
{
cout << " + found mIRC_Channel window" << endl;
cout << "- find Static window" << endl;
HWND hwndStatic = FindWindowEx(hwndmIRC_Channel, NULL, L"Static", NULL);
if (NULL != hwndStatic)
{
cout << " + found Static window" << endl;
cout << "- get text length" << endl;
int textLen = (int)SendMessage(hwndStatic, WM_GETTEXTLENGTH, 0, 0);
if (0 < textLen)
{
cout << "- getting text" << endl;
const int bufferSize = 1024;
char textBuffer[bufferSize] = "";
SendMessage(hwndStatic, WM_GETTEXT, (WPARAM)bufferSize, (LPARAM)textBuffer);
cout << "[begin text]" << endl;
cout << textBuffer << endl;
cout << "[end text]" << endl;
}
else
{
cerr << "No text." << endl;
}
}
else
{
cerr << "Static not found." << endl;
}
}
else
{
cerr << "mIRC_Channel not found." << endl;
}
}
else
{
cerr << "MDIClient not found." << endl;
}
}
else
{
cerr << "mIRC not open." << endl;
}
}
int main()
{
FindmIRC();
return 0;
}
The highlighted class is what has the text:
The program find the class with no problem and does not report not finding it so I don't see a reason why it should not find it. Any help is great!
As you can see on your spy++ output, highlighted control does not contain any text. It should appear on the left of Static in "".