C++ clipboard queue pasting random results? - c++

I'm trying to make a clipboard queue, allowing me to copy multiple things and then paste them FIFO. To do so, I'm using with Windows API as well as a basic keyboard hook to detect ctrl+c and ctrl+v. My code seems to work however I seem to constantly get a random output from the queue.
#include <Windows.h>
#include <stdio.h>
#include <vector>
using namespace std;
vector<char*> clipboardQueue;
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (wParam == WM_KEYDOWN) {
if (p->vkCode == 0x43 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-c is pressed
WM_COPY;
Sleep(500);
OpenClipboard(NULL);
char* buffer;
buffer = (char*)GetClipboardData(CF_TEXT);
CloseClipboard();
clipboardQueue.push_back(buffer);
cout << buffer << " copied!\n";
cout << "clipboard size: " << clipboardQueue.size() << "\n";
}
else if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed
if (clipboardQueue.size() > 0) {
const char* output = clipboardQueue[0];
const size_t len = strlen(output) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), output, len);
GlobalUnlock(hMem);
OpenClipboard(0);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
CloseClipboard();
WM_PASTE;
clipboardQueue.erase(clipboardQueue.begin());
cout << output << " pasted!\n";
cout << "clipboard size: " << clipboardQueue.size() << "\n";
}
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
int main()
{
OpenClipboard(NULL);
EmptyClipboard();
CloseClipboard();
HHOOK keyBoard = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, NULL);
MSG msg;
while (GetMessage(&msg, NULL, NULL, NULL)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnhookWindowsHookEx(keyBoard);
}
Using this code when copying "one, two, three, four, five" my outputs seem to be random with occassional missing keys entirely:
three
three
three
?
five
one
?
three
?
five
?
four
four
four
five

Key edits:
When copy data to clipboard using ctrl + c there will sent a WM_CLIPBOARDUPDATE message which you can use to monitor ctrl + c operations. But there is no related message to monitor paste control + v operation so I keep hook part for control + v.
I can reproduce "outputs seem to be random" issue and solve it using array instead of std::vector.
Create a message-only window if you don't need a interact UI.
Add a new custom clipboard format MY_CLIPBOARD_FORMAT to indicate this is set data cased WM_CLIPBOARDUPDATE message not actual control + v operation.
The following is just an example implement for your use case you can refer to:
#include <windows.h>
#define MAX_LOADSTRING 100
#define MY_CLIPBOARD_FORMAT (CF_PRIVATEFIRST + 1)
// Global Variables:
WCHAR szTitle[MAX_LOADSTRING]; // The title bar text
WCHAR szWindowClass[MAX_LOADSTRING]; // the main window class name
static int index = 0;
static char dataArry[10][10] = {};
// Forward declarations of functions included in this code module:
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK LowLevelKeyboardProc(int, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
DWORD errorCode;
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_QUEUECLIPBOARDDATA, szWindowClass, MAX_LOADSTRING);
// Register window class
WNDCLASSEXW wcex = {};
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.lpfnWndProc = WndProc;
wcex.hInstance = hInstance;
wcex.lpszClassName = szWindowClass;
ATOM cls = RegisterClassExW(&wcex);
errorCode = GetLastError();
// Create a message-only window
HWND hWnd = CreateWindowEx(0, szWindowClass, szTitle, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, NULL, NULL);
if (!hWnd)
{
errorCode = GetLastError();
return FALSE;
}
// Set keyboard hook
HHOOK keyBoard = SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, NULL, NULL);
// Register a clipboard format listener
if (!AddClipboardFormatListener(hWnd))
errorCode = GetLastError();
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
LRESULT CALLBACK LowLevelKeyboardProc(int nCode, WPARAM wParam, LPARAM lParam) {
PKBDLLHOOKSTRUCT p = (PKBDLLHOOKSTRUCT)lParam;
if (wParam == WM_KEYDOWN) {
if (p->vkCode == 0x56 && GetKeyState(VK_CONTROL) & 0x8000) { // ctrl-v is pressed
if(index <= 0)
return CallNextHookEx(NULL, nCode, wParam, lParam);
static int i = 0;
if (i < index)
{
const char* output = dataArry[i];
const size_t len = strlen(output) + 1;
HGLOBAL hMem = GlobalAlloc(GMEM_MOVEABLE, len);
memcpy(GlobalLock(hMem), output, len);
GlobalUnlock(hMem);
OpenClipboard(0);
EmptyClipboard();
SetClipboardData(CF_TEXT, hMem);
// Set custom defined format to indicate paste operation
SetClipboardData(MY_CLIPBOARD_FORMAT, NULL);
CloseClipboard();
OutputDebugStringA("\n pasted!\n");
i++;
}
else
{
i = 0;
index = 0;
}
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
DWORD errorCode;
HANDLE clipObj = NULL;
char* lptstr = NULL;
switch (message)
{
case WM_CLIPBOARDUPDATE:
{
OutputDebugStringA("\nWM_CLIPBOARDUPDATE\n");
// Empty clipboard casue this WM_CLIPBOARDUPDATE, actually no new data copied
if (IsClipboardFormatAvailable(MY_CLIPBOARD_FORMAT))
break;
if (!IsClipboardFormatAvailable(CF_TEXT))
break;
if (!OpenClipboard(hWnd))
{
errorCode = GetLastError();
break;
}
clipObj = GetClipboardData(CF_TEXT);
if(NULL == clipObj)
{
errorCode = GetLastError();
break;
}
lptstr = (char*)GlobalLock(clipObj);
if (lptstr != NULL)
{
OutputDebugStringA(lptstr);
memcpy(dataArry[index++], lptstr, strlen(lptstr));
GlobalUnlock(lptstr);
}
CloseClipboard();
}
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}

Related

Process with dll injected, hangs after closing

I'm trying to learn how to create a message-only window inside of a dll, but the process being injected by this dll 'hangs'/never exits when I close it.
I get the message box DLL_PROCESS_DETACH, but it remains open in the task manager.
It only happens when I call the function Main(), I'm probably doing something wrong.
Source code of the dll:
// Global variables.
HWND processhWnd = nullptr;
bool disableHooks = false;
class std::future<void> wndproc_thread;
class std::future<void> wm_copydata_thread;
LRESULT(WINAPI* DefWindowProcW_Hook)(HWND, UINT, WPARAM, LPARAM) = DefWindowProcW;
LRESULT WINAPI HookedDefWindowProcW(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam)
{
if (disableHooks)
return DefWindowProcW_Hook(hWnd, Msg, wParam, lParam);
switch (Msg)
{
}
return DefWindowProcW_Hook(hWnd, Msg, wParam, lParam);
}
#pragma region getprocessHWND
struct handle_data {
unsigned long process_id;
HWND window_handle;
};
BOOL Is_Main_Window(HWND handle)
{
return GetWindow(handle, GW_OWNER) == (HWND)0 && IsWindowVisible(handle);
}
BOOL CALLBACK Enum_Windows_Callback(HWND handle, LPARAM lParam)
{
//static wchar_t title[200]; how to print the window title?
//GetWindowText(handle, title, 200);
//doPrint("title: ", title);
handle_data& data = *(handle_data*)lParam;
unsigned long process_id = 0;
GetWindowThreadProcessId(handle, &process_id);
if (data.process_id != process_id || !Is_Main_Window(handle))
return TRUE;
data.window_handle = handle;
return FALSE;
}
HWND Find_Main_Window(unsigned long process_id)
{
handle_data data;
data.process_id = process_id;
data.window_handle = 0;
EnumWindows(Enum_Windows_Callback, (LPARAM)&data);
return data.window_handle;
}
#pragma endregion
BOOL APIENTRY DllMain(HMODULE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch (ul_reason_for_call)
{
case DLL_PROCESS_ATTACH:
{
Sleep(1000);
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourAttach(&(PVOID&)DefWindowProcW_Hook, HookedDefWindowProcW);
LONG lError = DetourTransactionCommit();
if (lError != NO_ERROR) {
MessageBox(HWND_DESKTOP, L"Failed to detour", L"", MB_OK);
return FALSE;
}
wndproc_thread = std::async(std::launch::async, []
{ Main(); });
}
break;
case DLL_PROCESS_DETACH:
{
DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
DetourDetach(&(PVOID&)True_WriteFile, HookedWriteFile);
LONG lError = DetourTransactionCommit();
if (lError != NO_ERROR) {
MessageBox(HWND_DESKTOP, L"DLL_PROCESS_DETACH", L"", MB_OK);
return FALSE;
}
}
break;
}
return TRUE;
}
void WM_COPYDATA(std::wstring data)
{
}
// Step 4: the Window Procedure.
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_COPYDATA:
{
PCOPYDATASTRUCT pcds = reinterpret_cast<PCOPYDATASTRUCT>(lParam);
std::wstring received_data = (wchar_t*)pcds->lpData;
std::wstring data = received_data;
wm_copydata_thread = std::async(std::launch::async, [data]
{ WM_COPYDATA(data); });
doPrint("WM_COPYDATA: ", data);
return 1;
}
case WM_CLOSE:
DestroyWindow(hWnd);
break;
case WM_DESTROY:
case WM_QUIT:
//OutputDebugString(L"WM_QUIT");
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
// Step 1-3 Creating a window procedure.
int Main()
{
WNDCLASSEX wc{};
HWND hWnd = nullptr;
MSG Msg;
//Step 1: Registering the Window Class
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = 0;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
DWORD PID = GetCurrentProcessId();
HWND processhWnd = Find_Main_Window(PID);
// The class name of the message-only window will be the
// window title of the process being injected.
std::wostringstream ss;
ss << std::hex << processhWnd;
std::wstring ClassName = ss.str();
wc.lpszClassName = ClassName.c_str();
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc))
{
DWORD err = GetLastError();
std::wstring error = L"Window Registration Failed.\nError: " + std::to_wstring(int(err));
MessageBox(NULL, error.c_str(), L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Step 2: Creating the Window
// Message-only window (HWND_MESSAGE).
// https://learn.microsoft.com/en-us/windows/win32/winmsg/window-features#message-only-windows
hWnd = CreateWindowEx(
WS_EX_TOOLWINDOW,
ClassName.c_str(), // name of the class, as passed to the RegisterClass function
NULL,
NULL,
CW_USEDEFAULT, CW_USEDEFAULT, 0, 0, // X,Y,W,H
HWND_MESSAGE,
NULL, 0, NULL);
if (hWnd == NULL)
{
std::wstring error = L"Window creation failed.\nError: " + std::to_wstring(int(err));
MessageBox(NULL, error.c_str(), L"Error!",
MB_ICONEXCLAMATION | MB_OK);
return 0;
}
doPrint("\nprocesshWnd: ", processhWnd);
doPrint("HWND_MESSAGE: ", hWnd, "\n");
ShowWindow(hWnd, SW_SHOW);
UpdateWindow(hWnd);
// Step 3: The Message Loop
while (GetMessage(&Msg, NULL, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return 0; // Msg.wParam;
}
The way I'm creating/calling the WndProcedure using threads, etc, is it 'correctly'?
My goal is to send data to the dll using WM_COPYDATA.

Raw Input - recive WM_INPUT while window/program is in background

I am trying to monitor and printout the RAWINPUT from a mouse sent to the foreground window, or just all RAWINPUT from the mouse in general.
the global hook LowLevelMouseProc does not work for me because it returns a MOUSEHOOKSTRUCT that does not give me the dx and dy.
The Raw Input API mentions that for WM_INPUT received when the current window is in the back ground, wParam will be set to RIM_INPUTSINK. But I have no idea how to receive WM_INPUT while the program is in the background.
here is some code explaining what I am trying to do.
int main()
{
//regiter the monitoring device
static bool raw_input_initialized = false;
if (raw_input_initialized == false)
{
RAWINPUTDEVICE rid;
rid.usUsagePage = 0x01; //Mouse
rid.usUsage = 0x02;
rid.dwFlags = 0;
rid.hwndTarget = NULL;
if (RegisterRawInputDevices(&rid, 1, sizeof(rid)) == FALSE)
{
exit(-1);
}
raw_input_initialized = true;
}
HWND targetWindow = { 0 };
while (true)
{
targetWindow = GetForegroundWindow(); // get the window runing in the formost window;
std::cout << targetWindow << '\n';
}
return 0;
}
// enterd every time there is a rawinput to ForegroundWindow, or alternatively just a rawinput in general
LRESULT CALLBACK targetWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
// print out the values that I need
case WM_INPUT:
UINT dataSize;
GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, NULL, &dataSize, sizeof(RAWINPUTHEADER)); //Need to populate data size first
std::cout << GET_RAWINPUT_CODE_WPARAM(wParam) << " code thing\n";
if (dataSize > 0)
{
std::unique_ptr<BYTE[]> rawdata = std::make_unique<BYTE[]>(dataSize);
if (GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, rawdata.get(), &dataSize, sizeof(RAWINPUTHEADER)) == dataSize)
{
RAWINPUT* raw = reinterpret_cast<RAWINPUT*>(rawdata.get());
if (raw->header.dwType == RIM_TYPEMOUSE)
{
std::cout << raw->data.mouse.lLastX << std::endl;
}
}
}
break;
}
}
But I have no idea how to receive WM_INPUT while the program is in the background.
You need to specify the RIDEV_INPUTSINK flag when registering the device, per the RAWINPUTDEVICE documentation:
dwFlags
Type: DWORD
Mode flag that specifies how to interpret the information provided by usUsagePage and usUsage. It can be zero (the default) or one of the following values. By default, the operating system sends raw input from devices with the specified top level collection (TLC) to the registered application as long as it has the window focus.
...
RIDEV_INPUTSINK
0x00000100
If set, this enables the caller to receive the input even when the caller is not in the foreground. Note that hwndTarget must be specified.
As such, you must specify an HWND to receive the WM_INPUT messages, and have a message loop to service that window.
Try this:
#include <iostream>
#include <vector>
#include <Windows.h>
LRESULT CALLBACK targetWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
int main()
{
HINSTANCE hInstance = GetModuleHandle(NULL);
WNDCLASS wc = {};
wc.lpfnWndProc = targetWindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = TEXT("MyRawInputWnd");
if (!RegisterClass(&wc))
return -1;
HWND targetWindow = CreateWindowEx(0, wc.lpszClassName, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, hInstance, NULL);
if (!targetWindow)
return -1;
//register the monitoring device
RAWINPUTDEVICE rid = {};
rid.usUsagePage = 0x01; //Mouse
rid.usUsage = 0x02;
rid.dwFlags = RIDEV_INPUTSINK;
rid.hwndTarget = targetWindow;
if (!RegisterRawInputDevices(&rid, 1, sizeof(rid)))
return -1;
MSG msg;
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
DestroyWindow(targetWindow);
return 0;
}
LRESULT CALLBACK targetWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
// print out the values that I need
case WM_INPUT: {
UINT dataSize;
GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, NULL, &dataSize, sizeof(RAWINPUTHEADER)); //Need to populate data size first
std::cout << GET_RAWINPUT_CODE_WPARAM(wParam) << " code thing\n";
if (dataSize > 0)
{
std::vector<BYTE> rawdata(dataSize);
if (GetRawInputData(reinterpret_cast<HRAWINPUT>(lParam), RID_INPUT, rawdata.data(), &dataSize, sizeof(RAWINPUTHEADER)) == dataSize)
{
RAWINPUT* raw = reinterpret_cast<RAWINPUT*>(rawdata.data());
if (raw->header.dwType == RIM_TYPEMOUSE)
{
std::cout << raw->data.mouse.lLastX << std::endl;
}
}
}
return 0;
}
}
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}

Win32 SendInput mousemove lag and freeze

I am trying to move the mouse (on Windows 10) using SendInput when I perform a physical mouse click. It works fine if I click once or twice, but if clicking for examples 6 times in quick succession the mouse lags for a few seconds then the program stops responding.
Is there any obvious reason why this is happening?
(Edited)
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
LRESULT CALLBACK MouseHook(int, WPARAM, LPARAM);
int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE, LPWSTR, int)
{
HHOOK hook = SetWindowsHookEx(WH_MOUSE_LL, MouseHook, NULL, 0);
MessageBox(NULL, L"Hello", L"Hello", MB_OK);
UnhookWindowsHookEx(hook);
return 0;
}
LRESULT CALLBACK MouseHook(int nCode, WPARAM wParam, LPARAM lParam) {
if (nCode == HC_ACTION) {
switch (wParam) {
case WM_RBUTTONUP:
INPUT buffer;
ZeroMemory(&buffer, sizeof(buffer));
buffer.type = INPUT_MOUSE;
buffer.mi.dx = 0;
buffer.mi.dy = 10;
buffer.mi.mouseData = 0;
buffer.mi.dwFlags = MOUSEEVENTF_MOVE;
buffer.mi.time = 0;
buffer.mi.dwExtraInfo = 0;
SendInput(1, &buffer, sizeof(INPUT));
}
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
Using Raw Input sample:
#include <windows.h>
#include <iostream>
using namespace std;
BOOL registerTouchpadForInput(HWND hWnd)
{
RAWINPUTDEVICE rid;
rid.dwFlags = RIDEV_NOLEGACY | RIDEV_INPUTSINK;
rid.usUsagePage = 1;
rid.usUsage = 2;
rid.hwndTarget = hWnd;
return RegisterRawInputDevices(&rid, 1, sizeof(rid));
}
void getinputdata(LPARAM lparam)
{
HRAWINPUT hInput = (HRAWINPUT)lparam;
RAWINPUT input = { 0 };
UINT size = sizeof(RAWINPUT);
GetRawInputData(hInput, RID_INPUT,&input, &size,sizeof(RAWINPUTHEADER));
if (RIM_TYPEMOUSE == input.header.dwType)
{
if (input.data.mouse.usButtonFlags & RI_MOUSE_RIGHT_BUTTON_DOWN)
{
INPUT buffer;
ZeroMemory(&buffer, sizeof(buffer));
buffer.type = INPUT_MOUSE;
buffer.mi.dx = 0;
buffer.mi.dy = 10;
buffer.mi.mouseData = 0;
buffer.mi.dwFlags = MOUSEEVENTF_MOVE;
buffer.mi.time = 0;
buffer.mi.dwExtraInfo = 0;
SendInput(1, &buffer, sizeof(INPUT));
}
}
return;
}
static LRESULT CALLBACK NVTouch_WindowProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
BOOL registrationStatus = false;
switch (message)
{
case WM_CREATE:
registrationStatus = registerTouchpadForInput(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_INPUT:
getinputdata(lParam);
return DefWindowProc(hwnd, message, wParam, lParam);
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}
int main()
{
WNDCLASSEX wndclass = {
sizeof(WNDCLASSEX),
CS_DBLCLKS,
NVTouch_WindowProc,
0,
0,
GetModuleHandle(0),
LoadIcon(0,IDI_APPLICATION),
LoadCursor(0,IDC_ARROW),
HBRUSH(COLOR_WINDOW + 1),
0,
L"myclass",
LoadIcon(0,IDI_APPLICATION)
};
bool isClassRegistered = false;
isClassRegistered = RegisterClassEx(&wndclass);
if (isClassRegistered)
{
HWND window = CreateWindow(wndclass.lpszClassName, NULL, 0, 0, 0, 0, 0, HWND_MESSAGE, NULL, GetModuleHandle(0), NULL);
if (window)
{
ShowWindow(window, SW_SHOWDEFAULT);
MSG msg;
while (GetMessage(&msg, 0, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
}

Is it possible to simulate mouse move and mouse clicks at hidden window?

So is there a way to make my Win32 application "think" that the mouse is moving over its window and making some clicks when the actual window is hidden (i mean ShowWindow(hWnd, SW_HIDE);)?
I tried to simulate mouse moving with PostMessage and SendMessage but no luck so far.
int x = 0;
int y = 0;
while (true)
{
SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
x += 10;
y += 10;
Sleep(100);
}
Is this even possible?
Yes it's possible. This is a test hidden window:
#define UNICODE
#include <Windows.h>
#include <Strsafe.h>
#include <Windowsx.h>
LRESULT CALLBACK WndProc(HWND Hwnd, UINT Msg, WPARAM WParam, LPARAM LParam);
INT CALLBACK
WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
INT nCmdShow)
{
WNDCLASSEX WndClass;
ZeroMemory(&WndClass, sizeof(WNDCLASSEX));
WndClass.cbSize = sizeof(WNDCLASSEX);
WndClass.lpfnWndProc = WndProc;
WndClass.hInstance = hInstance;
WndClass.lpszClassName = L"HiddenWinClass";
if(RegisterClassEx(&WndClass))
{
HWND Hwnd;
MSG Msg;
Hwnd = CreateWindowEx(0, L"HiddenWinClass", L"Nan",
0, 0, 0, 0, 0, NULL, NULL, hInstance, NULL);
if(Hwnd)
{
UpdateWindow(Hwnd);
ShowWindow(Hwnd, SW_HIDE);
while(GetMessage(&Msg, 0, 0, 0) > 0)
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
}
}
return 0;
}
LRESULT CALLBACK
WndProc(HWND Hwnd,
UINT Msg,
WPARAM WParam,
LPARAM LParam)
{
TCHAR Message[255];
switch(Msg)
{
case WM_MOUSEMOVE:
StringCbPrintf(Message, sizeof(Message), L"(%d, %d)",
GET_X_LPARAM(LParam), GET_Y_LPARAM(LParam));
MessageBox(NULL, Message, L"WM_MOUSEMOVE", MB_OK);
break;
default:
return DefWindowProc(Hwnd, Msg, WParam, LParam);
}
return 0;
}
and this is your code:
#define UNICODE
#include <Windows.h>
int
main(int argc, char **argv)
{
HWND Hwnd;
if((Hwnd = FindWindow(L"HiddenWinClass", L"Nan")))
{
int x, y;
for(x = y = 0 ; ; x += 10, y += 10)
{
SendMessage(Hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));
Sleep(100);
}
}
return 0;
}
It works nicely.

Why PeekMessage doesn't call keyboard hook proc on message posted by PostMessage?

I have an app that handle keyboard input via keyboard hooks. It sets the hook by SetWindowsHookEx(WH_KEYBOARD,...).
When PeekMessage in message processing loop receives usual WM_KEYUP and WM_KEYDOWN messages from real hardware keyboard, it calls callback and all is fine, but it completely ignores messages sent by PostThreadMessage (and any other messaging function). It just doesn't call hook procedure!
There is piece of terrible code (mostly auto-generated by Visual Studio) below to illustrate that behavior. There is the window with message-processing loop and the thread, sending WM_KEYDOWN messages every 1 second. All calls of keyboard callback writes log records to the console window, and I can see them, when I press key on my keyboard, but there are no records for messages sent by second thread!
I cannot change main application implementation. In many reasons it should work via keyboard hooks. But how can I make GetMessage or PeekMessage handle my keyboard messages as it handles messages from hardware keyboard? Great thanks for any help, I've spent whole week for that problem!
I've tested it on Windows 8.1 64-bit with 32-bit application.
Here is the code:
#include "windows.h"
#include <iostream>
#include <fstream>
using namespace std;
#define MAX_LOADSTRING 100
HINSTANCE hInst;
TCHAR *szTitle = TEXT("Title");
TCHAR *szWindowClass = TEXT("HookedWindowClass");
LRESULT CALLBACK KeyboardCallback(
_In_ int code,
_In_ WPARAM wParam,
_In_ LPARAM lParam
)
{
wcout << L"KeyboardCallback " << endl;
MSG *msg = (MSG*)lParam;
wcout << L"KBHOOK " << code << L" wParam " << hex << wParam << L" lParam " << lParam << dec << endl;
return CallNextHookEx(NULL,code,wParam,lParam);
}
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
DWORD mainThreadId = 0 ;
HWND window = 0;
DWORD threadFn(void*)
{
while(true)
{
if(!PostThreadMessage(mainThreadId,WM_KEYDOWN,0xd,0x1c0001))
{
cout << "Error posting message to " << hex << mainThreadId << " error " << GetLastError() << endl;
}
Sleep(1000);
}
}
int APIENTRY WinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
MSG msg;
MyRegisterClass(hInstance);
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
AllocConsole();
wfstream console_out("CONOUT$");
wcout.rdbuf(console_out.rdbuf());
HHOOK hHook = SetWindowsHookEx(WH_KEYBOARD,KeyboardCallback,GetModuleHandle(NULL),GetCurrentThreadId());
mainThreadId = GetCurrentThreadId();
CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)threadFn,NULL,0,NULL);
while (GetMessage(&msg, NULL, 0, 0))
{
switch (msg.message)
{
case WM_KEYDOWN:
wcout << L"WM_KEYDOWN" << L" wParam " << hex << msg.wParam << L" lParam " << msg.lParam << dec << endl;
break;
default:
break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return (int) msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = NULL;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = NULL;
wcex.hIconSm = NULL;
wcex.lpszClassName = szWindowClass;
return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
HWND hWnd;
hInst = hInstance;
hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
window = hWnd;
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
int wmId, wmEvent;
PAINTSTRUCT ps;
HDC hdc;
switch (message)
{
case WM_PAINT:
hdc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_QUIT:
ExitProcess(0);
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}