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 "".
Related
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 am working on a power engineering project on 4th semester, and programming isn't my strong side. I've been working on using libusb for communication between a PSoC 5 and a Linux terminal program written in C++. The terminal code is:
The problem is that libusb_open_device_with_vid_pid(NULL, 0x1111, 0x2222) returns 0 every time, even though the device is recognized by the Linux OS. OS is Ubuntu if that is relevant.
#include <iostream>
#include "libusb-1.0/libusb.h"
#include "usb.h"
#include <time.h>
using namespace std;
union USB_DATA
{
unsigned char USB_ARRAY[1200];
int DirectionOfPower;
int menu;
float Voltage;
float Current;
float Temperature;
float PowerFactor;
float DistortionPowerFactor;
float Amplitude_Of_Harmonics[1001];
float Regulate_To;
};
union USB_DATA USB_;
/*
void error(string s, int err)
{
cout << s " ERROR: " << libusb_error_name(err) << endl;
exit(err);
}
*/
int main()
{
int transfer_size;
int err;
float Reg_To;
// Device Handle
libusb_device_handle* dev;
// Initialize libusb with default context
libusb_init(NULL);
// Open Device VID = 0x1111, PID = 0x2222 with the default libusb context
dev = libusb_open_device_with_vid_pid( NULL, 0x1111, 0x2222 );
// If device is null, we didn't find it
/*
if (dev == NULL)
{
cout << "Device not found, exiting." << endl;
return -1;
}
int k = 0;
while (dev == NULL)
{
cout << "Device not found, trying again." << " " << k << endl;
//sleep(1);
k = k+1;
}
*/
// Claim interface 0 on the device. Here we te the operation system that wewan this device
libusb_claim_interface(dev, 0);
libusb_detach_kernel_driver(dev, 0);
// Set alternate setting 0 on interface 0
libusb_set_interface_alt_setting(dev, 0, 0);
while(true)
{
cout << "Welcome to Spaendingsregulering!" << endl;
cout << endl;
cout << "MENU" << endl;
cout << "Indtast nummer for navigation" << endl;
cout << "1. Indsaet driftsparametre " << endl;
cout << "2. Analyser harmoniske " << endl;
cout << "3. Fremvis data " << endl;
while(true)
{
cin >> USB_.menu;
if(cin.good())
break;
cin.clear();
}
/*
err = libusb_bulk_transfer(dev, 0x02, USB_.USB_ARRAY, sizeof(union USB_), &transfer_size, 1000);
if( err )
error( "Bulk OUT Transfer Failed!", err);
err = libusb_bulk_transfer(dev, 0x81, USB_.USB_ARRAY, sizeof(union USB_), &transfer_size, 1000);
if( err )
error( "Bulk IN Transfer Failed!", err);
*/
if(USB_.menu == 1)
while(true)
{
cout << "Indsaet oensket spaending" << endl;
cout << "Indtast 999 for at vende tilbage til hovedmenuen" << endl;
cin >> Reg_To;
cout << endl;
if(Reg_To == 999)
{
break;
}
USB_.Regulate_To = Reg_To;
cout << "=======================" << endl;
cout << "Saetter oensket spaending til:" << " " << USB_.Regulate_To << "V" << endl;
cout << "=======================" << endl;
cout << endl;
cout << "Vender tilbage til hovedmenu" << endl;
cout << "..." << endl;
cout << endl;
if(cin.good())
break;
cin.clear();
}
}
}
libusb_open_device_with_vid_pid combines finding and opening and doesn't return an error code. If you are sure the device is there, have you checked you have the rights to read/write to it ? You can also increase the verbosity of error messages.
– Leiaz
Thanks! that did it!. I forgot to use sudo.. rookie mistake – Çağrı Esen
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.
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...
cI want to select some text with my cursor using the Mark Function from Console, but my code doesn't work ...
CONSOLE_SELECTION_INFO c;
if(GetConsoleSelectionInfo(&c))
{
while((c.dwFlags & CONSOLE_MOUSE_DOWN) == 0) { if(c.dwFlags) cout << c.dwFlags; }
cout << "SelectionAnchor: " << c.dwSelectionAnchor.X << " " << c.dwSelectionAnchor.Y;
cout << "RectangleSelection: " << c.srSelection.Top << " " << c.srSelection.Left << c.srSelection.Bottom << c.srSelection.Right;
}
else cout << "\n\nError: " << GetLastError();
Whatever I'm selecting or I'm doing, always c.dwFlags will be 0 ...