Skip to content

Instantly share code, notes, and snippets.

@NtRaiseHardError
Last active May 31, 2020 07:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save NtRaiseHardError/df8075c303d89ef9dc6003f5d0171dd1 to your computer and use it in GitHub Desktop.
Save NtRaiseHardError/df8075c303d89ef9dc6003f5d0171dd1 to your computer and use it in GitHub Desktop.
PoC code to demonstrate clipboard monitoring in Windows using an event-based listener.
// PoC code to demonstrate clipboard monitoring in Windows
// using an event-based listener.
#include <stdio.h>
#include <Windows.h>
#define CLASS_NAME L"MY_CLASS"
#define WINDOW_NAME L"MY_WINDOW"
LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
HANDLE hClipData = NULL;
LPVOID data = NULL;
switch (uMsg) {
case WM_CLIPBOARDUPDATE:
if (IsClipboardFormatAvailable(CF_TEXT)) {
if (OpenClipboard(hwnd)) {
hClipData = GetClipboardData(CF_TEXT);
data = GlobalLock(hClipData);
// Print the clipboard data.
printf("%s\n\n", (LPSTR)data);
GlobalUnlock(hClipData);
CloseClipboard();
}
}
break;
default:
break;
}
return DefWindowProc(hwnd, uMsg, wParam, lParam);;
}
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR lpCmdLine, int nShow) {
WNDCLASSEX wc;
HWND hwnd = NULL;
// Create console to print clipboard data.
AllocConsole();
AttachConsole(GetCurrentProcessId());
freopen("CON", "w", stdout);
ZeroMemory(&wc, sizeof(WNDCLASSEX));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WindowProc;
wc.hInstance = hInstance;
wc.lpszClassName = CLASS_NAME;
// Create invisible window.
RegisterClassEx(&wc);
hwnd = CreateWindowEx(0,
CLASS_NAME,
WINDOW_NAME,
0,
0,
0,
0,
0,
NULL,
NULL,
hInstance,
NULL
);
// Add listener.
AddClipboardFormatListener(hwnd);
MSG msg;
while (GetMessage(&msg, 0, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
RemoveClipboardFormatListener(hwnd);
FreeConsole();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment