-2

I've written a program to get keyboard hook. What I want is, when a user presses Enter, the typed text, for example: "hello world", which I stored in exportMsg, should be returned from the function.

I want to make a dll and export exportMsg.

Here is my code. Thanks in advance.

#include <Windows.h>
#include <stdio.h>
#include <iostream> 
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam); 
HHOOK keyboardHook;
HWND prevWindow;
std::string exportMsg=""; 
int main()
{
    keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, 0, 0);
    MSG msg{ 0 };
    while (GetMessage(&msg, NULL, 0, 0) != 0);
    UnhookWindowsHookEx(keyboardHook);
    return 0;
} 
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    wchar_t title[256];
    HWND fwindow = GetForegroundWindow();
    PKBDLLHOOKSTRUCT key = (PKBDLLHOOKSTRUCT)lParam;
    //a key was pressed
    if (wParam == WM_KEYDOWN && nCode == HC_ACTION )
    { 
        //return if enter pressed
        if (key->vkCode == '\r')
        {  
            std::cout << exportMsg << std::endl;
        }
        else
        {
            exportMsg.push_back(key->vkCode);
        }
    }
    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}
Zues
  • 11
  • 5
  • The most important part of `SetWindowsHookEx` is the 3rd parameter, and you left it null. – Andy Aug 17 '20 at 03:08
  • @Andy how can I associate `exportMsg` to `SetWindowsHookEx ` to get the message? – Zues Aug 17 '20 at 03:29
  • You need the instance of the module the hook resides. So in your case it would be `GetModuleHandle(NULL)`. So you'd call it as so: `SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, GetModuleHandle(NULL), 0);` – Andy Aug 17 '20 at 03:39
  • Sorry, didn't understand the question. I thought i read it wasn't working at all. You *should* fill in the 3rd parameter, for sure. Could you re-phrase it to explain what you want to happen when they hit the enter key? You want it "exported to the DLL" doesn't make much sense. – Andy Aug 17 '20 at 03:51
  • The issue is I want to eport `exportMsg `, whenever the Enter key is pressed. – Zues Aug 17 '20 at 06:04
  • @Zues Do you want to export to a file or return the string every time you press ENTER? – Zeus Aug 17 '20 at 07:09
  • @ZhuSong I want to return the string. – Zues Aug 17 '20 at 07:51
  • @Zues If you press the ENTER key randomly, does the function calling the DLL use multithreading? – Zeus Aug 17 '20 at 08:09
  • @ZhuSong no, it's just a simple function, doesn't use multithreading. – Zues Aug 17 '20 at 08:12

1 Answers1

0

As std::cin doesn't work in Unity, I needed to use Keyboard Hook to scan qrCode through a QR Code Scanner Machine and send the scanned string to the Unity.

This algorithm capture the key strokes and returns the string when Enter is pressed.

My DLL code looks like this:

Header file:

LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam);
HHOOK keyboardHook;
HWND prevWindow;
std::string exportMsg = "";

QRCODEREADER_EXPORTS_API void StartHook(char* str, int strlen);

Source file:

QRCODEREADER_EXPORTS_API void StartHook(char* str, int strlen)
{ 
    string result;
    cout << "please scan the QR code!!!\n";
    //std::cin >> result; 
    exportMsg.clear();

    HHOOK keyboardHook = SetWindowsHookEx(WH_KEYBOARD_LL, KeyboardProc, GetModuleHandle(NULL), 0);
    if (!keyboardHook) {
        std::cout << "Failed to hook keyboard\n";
    }
    else {
        MSG msg{ 0 };
        while (GetMessage(&msg, NULL, 0, 0) != 0);
         
        result = exportMsg;
        cout << "your data: " << result << "\n";

        result = result.substr(0, strlen);
        //str = new char[result.length()];
        std::copy(result.begin(), result.end(), str);
        str[min(strlen - 1, (int)result.size())] = 0;
    } 
     
    std::cout << "Quitting...\n"; 
    UnhookWindowsHookEx(keyboardHook); 
}
LRESULT CALLBACK KeyboardProc(int nCode, WPARAM wParam, LPARAM lParam)
{
    HWND fwindow = GetForegroundWindow();
    PKBDLLHOOKSTRUCT key = (PKBDLLHOOKSTRUCT)lParam;
    //a key was pressed
    if (wParam == WM_KEYDOWN && nCode == HC_ACTION)
    {
        //return if enter pressed
        if (key->vkCode == '\r')
        {
            std::cout << exportMsg << std::endl;
            PostQuitMessage(0); 
            UnhookWindowsHookEx(keyboardHook);
        }
        else
        {
            exportMsg.push_back(key->vkCode);
        }
    }
    return CallNextHookEx(keyboardHook, nCode, wParam, lParam);
}

dllmain:

int main() {
    char* a;

    for (int i = 0; i < 3; i++)
    {
        a = new char[50];
        StartHook(a, 50);
        cout<< i << "th iter: " << a << endl;
    } 
    return 0;
}
Zues
  • 11
  • 5