PDA

View Full Version : Windows hook using windows api in QT



davinciomare
4th October 2016, 22:59
Hi i am searching for windows hook and i checked this in the webpage of microsoft. But i dont know how i can't return the value since function using LRESULT to show for example since qDebug the key pressed for me in terminal.
Error:
C:\Users\moh\Documents\Hook\main.cpp:63: error: C2440: 'return' : no se puede realizar la conversi¢n de 'QString' a 'LRESULT' No hay disponible ning£n operador de conversi¢n definido por el usuario que pueda realizar esta conversi¢n, o bien no se puede llamar al operador

The question is how i can change the returned value to Qstring. It's only for doing one example with qDebug thx

d_stranz
5th October 2016, 01:24
LRESULT is just a long. Read the QString documentation for how to convert integers into printable strings.

davinciomare
5th October 2016, 01:51
I know how to convert i need to use this:
QString tmp = QString::number();

But i have one more question related with this, i want to return the value for learning and then show with qdebug but i couldnt. Code:

#include <QCoreApplication>
#include <QDebug>
#include <QTime>
#include <QChar>
#include <iostream>
#include <Windows.h>
#pragma comment(lib,"user32.lib")

HHOOK hHook = NULL;

using namespace std;

void UpdateKeyState(BYTE *keystate, int keycode)
{
keystate[keycode] = GetKeyState(keycode);
}

LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{

//WPARAM is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN or WM_SYSKEYUP
//LPARAM is the key information
// if( wParam == WM_KEYDOWN )
//qDebug() << "Key Pressed!";

//Get the key information

KBDLLHOOKSTRUCT cKey = *((KBDLLHOOKSTRUCT*)lParam);

wchar_t buffer[5];

//get the keyboard state
BYTE keyboard_state[256];
GetKeyboardState(keyboard_state);
UpdateKeyState(keyboard_state, VK_SHIFT);
UpdateKeyState(keyboard_state, VK_CAPITAL);
UpdateKeyState(keyboard_state, VK_CONTROL);
UpdateKeyState(keyboard_state, VK_MENU);



//Get Keyboard Layout
HKL keyboard_layout = GetKeyboardLayout(0);

//Get the name
char lpszName[0x100] = {0};

DWORD dwMsg = 1;
dwMsg += cKey.scanCode << 16;
dwMsg += cKey.flags << 24;

int i = GetKeyNameText(dwMsg, (LPTSTR)lpszName,255);
//try to convert the key info
int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer,4,0, keyboard_layout);
buffer[4] = L'\0';
//qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
int num = cKey.vkCode;
// Print the output

if( wParam == WM_KEYDOWN )
//qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
return num;


return CallNextHookEx(hHook, nCode, wParam, lParam);
}

int main(int argc, char *argv[])
{

QCoreApplication a(argc, argv);

hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL,0);
if(hHook == NULL){
qDebug() << "error";
}
QString tmp = QString::number(num);

return a.exec();
}


It's only for doing one example with qdebug of the webpage of microsoft. sorry again..

d_stranz
5th October 2016, 05:49
I have no idea what you are trying to do. YOUR CODE IS UNREADABLE. FOR THE LAST TIME, USE [CODE] TAGS.

davinciomare
5th October 2016, 09:19
what is code tags? where are?

Lesiok
5th October 2016, 09:41
what is code tags? where are?Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

davinciomare
5th October 2016, 09:50
Sorry i will not repeat this mistake again.

#include <QCoreApplication>
#include <QDebug>
#include <QTime>
#include <QChar>
#include <iostream>
#include <Windows.h>
#pragma comment(lib,"user32.lib")

HHOOK hHook = NULL;

using namespace std;

void UpdateKeyState(BYTE *keystate, int keycode)
{
keystate[keycode] = GetKeyState(keycode);
}

LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{

//WPARAM is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN or WM_SYSKEYUP
//LPARAM is the key information
// if( wParam == WM_KEYDOWN )
//qDebug() << "Key Pressed!";

//Get the key information

KBDLLHOOKSTRUCT cKey = *((KBDLLHOOKSTRUCT*)lParam);

wchar_t buffer[5];

//get the keyboard state
BYTE keyboard_state[256];
GetKeyboardState(keyboard_state);
UpdateKeyState(keyboard_state, VK_SHIFT);
UpdateKeyState(keyboard_state, VK_CAPITAL);
UpdateKeyState(keyboard_state, VK_CONTROL);
UpdateKeyState(keyboard_state, VK_MENU);



//Get Keyboard Layout
HKL keyboard_layout = GetKeyboardLayout(0);

//Get the name
char lpszName[0x100] = {0};

DWORD dwMsg = 1;
dwMsg += cKey.scanCode << 16;
dwMsg += cKey.flags << 24;

int i = GetKeyNameText(dwMsg, (LPTSTR)lpszName,255);
//try to convert the key info
int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer,4,0, keyboard_layout);
buffer[4] = L'\0';
//qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
int num = cKey.vkCode;
// Print the output

if( wParam == WM_KEYDOWN )
//qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
return num;


return CallNextHookEx(hHook, nCode, wParam, lParam);
}

int main(int argc, char *argv[])
{

QCoreApplication a(argc, argv);

hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL,0);
if(hHook == NULL){
qDebug() << "error";
}
QString tmp = QString::number(num);

return a.exec();
}

I want to return for example num of LRESULT function and print with qdebug since main or something like that.

Lesiok
5th October 2016, 11:51
I think that this is not a version of the code with which you write in the first post. Line 63 is empty.

davinciomare
5th October 2016, 12:09
When i use const char* to print with qdebug the program crash.
Code:
#include <QCoreApplication>
#include <QDebug>
#include <QTime>
#include <QChar>
#include <iostream>
#include <QFile>
#include <QString>
#include <Windows.h>
#pragma comment(lib,"user32.lib")

HHOOK hHook = NULL;

using namespace std;

QByteArray contenido="Estoy escribiendo a un archivo";
QFile ap("C:/Users/moh/Desktop/documento.txt");
QString ok;

void UpdateKeyState(BYTE *keystate, int keycode)
{
keystate[keycode] = GetKeyState(keycode);
}

LRESULT CALLBACK MyLowLevelKeyBoardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
QFile ap("C:/Users/moh/Desktop/documento.txt");


//WPARAM is WM_KEYDOWN, WM_KEYUP, WM_SYSKEYDOWN or WM_SYSKEYUP
//LPARAM is the key information
// if( wParam == WM_KEYDOWN )
//qDebug() << "Key Pressed!";

//Get the key information

KBDLLHOOKSTRUCT cKey = *((KBDLLHOOKSTRUCT*)lParam);

wchar_t buffer[5];

//get the keyboard state
BYTE keyboard_state[256];
GetKeyboardState(keyboard_state);
UpdateKeyState(keyboard_state, VK_SHIFT);
UpdateKeyState(keyboard_state, VK_CAPITAL);
UpdateKeyState(keyboard_state, VK_CONTROL);
UpdateKeyState(keyboard_state, VK_MENU);



//Get Keyboard Layout
HKL keyboard_layout = GetKeyboardLayout(0);

//Get the name
char lpszName[0x100] = {0};

DWORD dwMsg = 1;
dwMsg += cKey.scanCode << 16;
dwMsg += cKey.flags << 24;

int i = GetKeyNameText(dwMsg, (LPTSTR)lpszName,255);
//try to convert the key info
int result = ToUnicodeEx(cKey.vkCode, cKey.scanCode, keyboard_state, buffer,4,0, keyboard_layout);
buffer[4] = L'\0';
//qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
QString num = QString::fromUtf16((ushort*)buffer);
// Print the output

int cod = cKey.vkCode;

if( wParam == WM_KEYDOWN ){
//qDebug() << "key:" << cKey.vkCode << " " << QString::fromUtf16((ushort*)buffer) << " " << QString::fromUtf16((ushort*)lpszName);
ap.open(QIODevice::WriteOnly | QIODevice::Text);
ap.write((const char*)result);
ap.close();



}

return CallNextHookEx(hHook, nCode, wParam, lParam);


}

int main(int argc, char *argv[])
{

QCoreApplication a(argc, argv);


hHook = SetWindowsHookEx(WH_KEYBOARD_LL, MyLowLevelKeyBoardProc, NULL,0);
if(hHook == NULL){
qDebug() << "error";
}

/*
if(!ap.exists()){
qDebug() << "El archivo seleccionado no existe";
return 1;
}
ap.open(QIODevice::WriteOnly | QIODevice::Text);
if (!ap.isOpen()){
qDebug() << "El archivo no se ha podido abrir";
return 2;
}
//contenido = ap.readAll();
ap.write(contenido);
ap.flush();
ap.close();
*/

return a.exec();
}


Basically the problem is here:
ap.write((const char*)cod);

When i write this the program crash. And i can't return other values only 4 arguments since MyLowLevelKeyBoardProc. Some way to return my key pressed is for learning?

Lesiok
5th October 2016, 13:13
You do not necessarily know what you're doing. "(const char*)cod" is the "cod variable contains the address." I do not think the variable cod was a pointer to memory.
I guess you must first learn the basics of C ++ and later use libraries as powerful as Qt

davinciomare
5th October 2016, 14:13
yes i am not very well in c++ but i was searching and you right a lot of documentation about this so it's right. thx solved

d_stranz
5th October 2016, 16:05
yes i am not very well in c++

I do not know if you have experience with Python, but many of your mistakes make me think so. In Python, you can "print" anything, and the Python interpreter will find a way to convert the object into a printable string. So in Python, you can write "print( cod )" and the interpreter will convert "cod" into a string and print it.

You can't do the same thing in C++. In C++, -you- must convert the object into a string before it can be printed. "cod" is an integer, so you have to convert it using QString::num() before trying to print it. A cast -is not- conversion. Using (const char *)cod does not convert cod to a string. It tells C++ "cod is actually not an int, it is a pointer to a const char, so please use it that way". Of course, that is not true. "cod" is still an int, and trying to use it like it was a pointer to a string will cause a crash.

davinciomare
5th October 2016, 19:19
this was the problem that i have with other things. this is the biggest problem that i have and you explain me very well stranz

davinciomare
5th October 2016, 22:55
so everytime if i want to print something i must to convert to string ok and write in file. And if for example i have one char the conversion? ok so i must do always the same conversion char take me problems but it's ok this was my problem. I didnt understand this.

Lesiok
6th October 2016, 06:53
Read about QTextStream.