Skip to content

Instantly share code, notes, and snippets.

@AndreVallestero
Created May 28, 2019 13:49
Show Gist options
  • Save AndreVallestero/fa6d9f11d386ea5865b22abb04ce8f1a to your computer and use it in GitHub Desktop.
Save AndreVallestero/fa6d9f11d386ea5865b22abb04ce8f1a to your computer and use it in GitHub Desktop.
Gets text from applications occupying the windows tray
#include <windows.h>
#include <commctrl.h>
#include <iostream>
int main(void) {
HWND hTrayWnd = FindWindowEx(
FindWindowEx(
FindWindowEx(
FindWindow("Shell_TrayWnd", NULL)
, NULL, "TrayNotifyWnd", NULL)
, NULL, "SysPager", NULL)
, NULL, "ToolbarWindow32", NULL);
int buttonCount = SendMessage(hTrayWnd, TB_BUTTONCOUNT, 0, 0);
DWORD trayPid;
GetWindowThreadProcessId(hTrayWnd, &trayPid);
HANDLE hTrayProcess = OpenProcess(PROCESS_ALL_ACCESS, false, trayPid);
TBBUTTON buttonData = {0};
LPVOID pButtonDataBuffer = VirtualAllocEx(hTrayProcess, NULL, sizeof(buttonData), MEM_COMMIT, PAGE_READWRITE);
char buttonText[1024] = {0};
LPVOID pButtonTextDataBuffer = VirtualAllocEx(hTrayProcess, NULL, sizeof(buttonText), MEM_COMMIT, PAGE_READWRITE);
for(int i=0; i<buttonCount; ++i) {
SendMessage(hTrayWnd, TB_GETBUTTON, i, (LPARAM)pButtonDataBuffer);
ReadProcessMemory(hTrayProcess, pButtonDataBuffer, &buttonData, sizeof(buttonData), NULL);
SendMessage(hTrayWnd, TB_GETBUTTONTEXT, buttonData.idCommand, (LPARAM)pButtonTextDataBuffer);
ReadProcessMemory(hTrayProcess, pButtonTextDataBuffer, &buttonText, sizeof(buttonText), NULL);
if (!strcmp(buttonText, "Click to turn off English mode"))
std::cout << "English mode detected" << std::endl;
else if (!strcmp(buttonText, "Click to turn off Hangul mode"))
std::cout << "Hangul mode detected" << std::endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment