0

In my .h file function mouseProc is declared as static(it has to be)

.h file

static LRESULT CALLBACK mouseProc(int Code, WPARAM wParam, LPARAM lParam);

initially i thought i would add &ui as a parameter to this function, but i am not able to do so. it gives me error "incompatible parameter type with HOOKPROC"

so, now i am not able to access the textboxes and labels in my UI by using
ui->textbox->apped(); how to solve this ?

---UPDATE as per suggestion of making ui static--------------

mainwindow.h

#pragma once
#ifndef MAINWINDOW_H
#define MAINWINDOW_H
#include "windows.h"
#include "windowsx.h"

QT_BEGIN_NAMESPACE
namespace Ui { class MainWindow; }
QT_END_NAMESPACE

class MainWindow : public QMainWindow
{
    Q_OBJECT                      //Used to handle events
    Q_DISABLE_COPY(MainWindow) //added

public:
    MainWindow(QWidget* parent = 0);
    ~MainWindow();                    //Destructor used to free resources

    // Static method that will act as a callback-function
   static LRESULT CALLBACK mouseProc(int Code, WPARAM wParam, LPARAM lParam);

 //   BOOL InitializeUIAutomation(IUIAutomation** automation);

  static Ui::MainWindow* ui; // declared static,pointing to UI class

private:

    // hook handler
    HHOOK mouseHook;
};
#endif // MAINWINDOW_H

mainwindow.cpp

#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <Windows.h>
#include <UIAutomation.h>

using namespace std;

Ui::MainWindow* MainWindow::ui = 0;

MainWindow::MainWindow(QWidget* parent)
    : QMainWindow(parent)
    //, ui(new Ui::MainWindow)
{
    ui = new Ui::MainWindow();
    ui->setupUi(this);

    
     HINSTANCE hInstance = GetModuleHandle(NULL);

    // Set hook
    mouseHook = SetWindowsHookEx(WH_MOUSE_LL, &mouseProc, hInstance, 0);
    // Check hook is correctly
    if (mouseHook == NULL)
    {
        qWarning() << "Mouse Hook failed";
    }
}
BOOL InitializeUIAutomation(IUIAutomation** automation)
{
    CoInitialize(NULL);
    HRESULT hr = CoCreateInstance(__uuidof(CUIAutomation), NULL,
        CLSCTX_INPROC_SERVER, __uuidof(IUIAutomation),
        (void**)automation);
    return (SUCCEEDED(hr));
}

MainWindow::~MainWindow()
{
    delete ui;
}
LRESULT CALLBACK MainWindow::mouseProc(int Code, WPARAM wParam, LPARAM lParam)
{
    ui->textbox->append(string);
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Danzow
  • 25
  • 7

1 Answers1

0

You can't add extra parameters to mouseProc. The compiler won't accept it, it needs to match the signature that SetWindowsHookEx() is expecting. If you resort to using a type-cast to force the compiler to accept it, the OS still won't know how to populate the new parameters with values at runtime when the hook is called, and you will just end up corrupting the call stack, and/or crashing your code.

To do what you want, you will simply have to store your ui pointer in global memory, or even make it be static as well. Somewhere that mouseProc can reach it.

As for the compiler error, what it says is correct - the MainWindow class you have shown does not have a member named textbox, which is why accessing ui->textbox (regardless of where ui will come from) fails to compile. So, you need to figure out where the textbox is really being stored, and then access it from THERE instead of from the MainWindow.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • Thanks for the suggestion, i have made `ui` static , still i am getting errors , am i doing something wrong here ? – Danzow Sep 11 '20 at 01:04
  • @Danzow You did not say what errors you are actually getting. If you are still seeing errors related to `textbox` being unknown, I already told you why that is. Does `ui->setupUi()` create the `textbox`? If so, where? – Remy Lebeau Sep 11 '20 at 01:15
  • The error i am getting is `a non-static member reference must be relative to a specific object' in the line ` ui->textbox->append(string); ` , the error is on `ui` , i am pretty sure , there is no problem with `textbox` , as i am able to access it easily in other non-static functions. i guess now my question is " How to correctly make ui pointer static ?". – Danzow Sep 11 '20 at 01:20
  • @Danzow that error does not match the code shown. `ui` is static, not non-static. And if you are able to access `textbox` in other code, then that code is accessing some other UI class, because there is no `textbox` in this code. – Remy Lebeau Sep 11 '20 at 01:48
  • Yes , you are right , i apologize for the confusion, i have now corrected the code and the error i am getting is `1 unresolved externals` and `unresolved external symbol "public: static class Ui::MainWindow::ui" (? ui @MainWindow@@2PEAV1Ui@@EA)` , This is in mainwindow.obj file. – Danzow Sep 11 '20 at 02:03
  • @Danzow you declared the `ui` variable as `static`, so now you need to define it in your `.cpp` file, eg: `Ui::MainWindow* MainWindow::ui = 0;` – Remy Lebeau Sep 11 '20 at 02:10
  • i tried defining it using `Ui::MainWindow* MainWindow::ui = 0;`inside my constructor, it says `member defination or redifination illegal in current scope` ,can u please help me where to define it in my cpp file , you can also edit my question to show the correct place, Thanks for looking into this. – Danzow Sep 11 '20 at 02:51
  • @Danzow in the `.cpp` file, it needs to be defined in global scope, outside of any of the class methods. I edited your question to show that. Now, I suggest you stop stumbling around and go get yourself a [good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). – Remy Lebeau Sep 11 '20 at 02:55
  • i sure will, when i declare it in global scope , it gives me Exception Unhandled `write access violation ,this was a nullptr` in ui_mainwindow.h file. – Danzow Sep 11 '20 at 03:02
  • @Danzow then you are doing something else wrong that we can't see. This is why it is important to provide a [mcve] when you ask questions. – Remy Lebeau Sep 11 '20 at 03:08