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...
Related
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.
I'm completely new to Libusb. I have written a c++ program in Ubuntu, I can connect to the device, I transfer some data to the device using libusb_bulk_transfer(...). It works the first time, but the second time it returns -1 code. But when I reject the device and then when I reinstall the device to my computer, it works again.
Here is my code:
#include <iostream>
#include <stdio.h>
#include <libusb.h>
using namespace std;
#define BULK_OUTPUT LIBUSB_ENDPOINT_OUT | 0x01
#define BULK_INPUT LIBUSB_ENDPOINT_IN | 0x01
union usb_relator {
unsigned char usb_data[4];
int32_t number;
};
int main()
{
libusb_device_handle *handle = NULL;
if (libusb_init(NULL) < 0)
{
cout << "Failure" << endl;
return 0;
}
cout << "libusb inited successfully!!" << endl;
libusb_device **devices;
libusb_get_device_list(NULL, &devices);
if (libusb_open(devices[0], &handle) < 0)
{
cout << "The device can not be open!!" << endl;
return 0;
}
cout << "device opened successfully!!" << endl;
libusb_device_descriptor descriptor;
libusb_get_device_descriptor(devices[0], &descriptor);
cout << "Vendor id: " << (int)descriptor.idVendor << endl;
cout << "Product Id: " << (int)descriptor.idProduct << endl;
libusb_detach_kernel_driver(handle, 0);
if (libusb_claim_interface(handle, 0) < 0)
{
cout << "Can not claim interface!!" << endl;
return 0;
}
cout << "Handle has been claimed!!" << endl;
if (libusb_set_interface_alt_setting(handle, 0, 0) < 0)
{
cout << "Could not set alternate setting of the handle!!" << endl;
return 0;
}
cout << "alt_setting set successfully!!" << endl;
usb_relator number;
cout << "Please insert your number: ";
cin >> number.number;
int transfer_size;
int returnCode = libusb_bulk_transfer(handle, BULK_OUTPUT, number.usb_data, sizeof(usb_relator), &transfer_size, 5000);
if (returnCode == 0)
{
cout << "Data sent!!" << endl;
}
else
{
cout << "Data could not be send!! Code: " << returnCode << endl;
}
returnCode = libusb_bulk_transfer(handle, BULK_INPUT, number.usb_data, sizeof(usb_relator), &transfer_size, 5000);
if (returnCode == 0)
{
cout << "Data recieved!!" << endl;
cout << number.usb_data << endl;
}
else
{
cout << "Data could not recieve!! Code: " << returnCode << endl;
}
libusb_release_interface(handle, 0);
libusb_close(handle);
libusb_free_device_list(devices, 1);
libusb_exit(NULL);
}
I'm using Ubuntu 14.04, and I'm using VMWare. Thanks for your help.
I think it has something to do with the libusb_detach_kernal_driver(...) methode I call in my code.(Right before the line I claim the interface).
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.
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 "".
I want to convert the strFileNotifyInfo[1].FileName(Wchar_t) to a string so that i can see the filepath. but i can't make it work.
Here is my code:
while(TRUE)
{
if( ReadDirectoryChangesW( hDir, (LPVOID)&strFileNotifyInfo, sizeof(strFileNotifyInfo), FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE || FILE_NOTIFY_CHANGE_CREATION, &dwBytesReturned, NULL, NULL) == 0)
{
cout << "Reading Directory Change" << endl;
}
else
{
cout << ("File Modified: ") << strFileNotifyInfo[1].FileName << endl;
cout << ("Loop: ") << nCounter++ << endl;
}
}
Use wcout when working with wide character data.
std::wcout << L"File Modified: " << strFileNotifyInfo[1].FileName << std::endl;
You should also keep in mind that FileName is not null-terminated.
WCHAR* filename_w = strFileNotifyInfo[1].FileName;
DWORD filename_len = strFileNotifyInfo[1].FileNameLength;
std::string filename(filename_w, filename_w + filename_len);
std::cout << "File Modified: " << filename << std::endl;