I want to make Korean key logger.
To do that, I need to check IME mode of the current(foreground) window.
I tried to implement this using the ImmGetContext
function, but ImmGetContext(GetForegroundWindow())
returns zero always.
How can I fix this problem?
#include <iostream>
#include <stdio.h>
#include <Windows.h>
#include <immdev.h>
#include <imm.h>
#include <TlHelp32.h>
#pragma comment(lib, "imm32")
using namespace std;
void WINAPI GetProcessNameByProcessID(DWORD dwProcessID, LPWSTR lpszProcessName, size_t nMaxCount) {
HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, NULL);
if (hSnapshot != INVALID_HANDLE_VALUE) {
PROCESSENTRY32 processEntry;
processEntry.dwSize = sizeof(processEntry);
Process32First(hSnapshot, &processEntry);
do {
if (processEntry.th32ProcessID == dwProcessID) {
wcscpy_s(lpszProcessName, nMaxCount, processEntry.szExeFile);
break;
}
} while (Process32Next(hSnapshot, &processEntry));
CloseHandle(hSnapshot);
}
}
int main() {
//LoadLibrary(L"imm32.dll");
while (true) {
WCHAR szName[100];
DWORD foregroundProcess;
HWND foregroundWindow = GetForegroundWindow();
GetWindowThreadProcessId(foregroundWindow, &foregroundProcess);
GetProcessNameByProcessID(foregroundProcess, szName, 100);
HIMC hIMC;
DWORD dwConv, dwSent;
if (!(hIMC = ImmGetContext(foregroundWindow))) return 1; //Error
ImmSetOpenStatus(hIMC, true);
ImmGetConversionStatus(hIMC, &dwConv, &dwSent);
wcout << szName << L"(" << foregroundProcess << L") : "
<< (dwConv & IME_CMODE_HANGEUL ? L"KOREAN" : L"ENGLISH") << endl;
ImmReleaseContext(foregroundWindow, hIMC);
Sleep(1000);
}
return 0;
}