-2

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;
}
4
  • 2
    ImmGetContext is usually called in a result to WM_IME_COMPOSITION learn.microsoft.com/en-us/windows/win32/intl/… not sure it works outside of this, and/or outside of any window(ed) context. Oct 9, 2020 at 13:53
  • @SimonMourier // Thank you for your answer. As you said, I tried ImmGetContext inside WndProc and it works well. However, as in the question, I want to create a Korean keylogger, but using WndProc cannot process messages from other processes. Is there any other way? (I tried using WH_KEYBOARD_LL, but it doesn't work.)
    – Sylvia
    Oct 9, 2020 at 15:00
  • "It doesn't work" is not a problem statement. Oct 9, 2020 at 15:35
  • @IInspectable // I'm not sure exactly how to explain it. To be as detailed as possible, in the WndProc callback function, the ImmGetContext function returns a non-zero. but in the LowLevelKeyboardHook callback function, the ImmGetContext function returns zero.
    – Sylvia
    Oct 9, 2020 at 15:43

1 Answer 1

3
#include <iostream>
#include <Windows.h>
#pragma comment (lib, "imm32.lib")
#define IMC_GETOPENSTATUS 0x0005
using namespace std;

int main() {
    while (true) {
        HWND hIME = ImmGetDefaultIMEWnd(GetForegroundWindow());
        LRESULT status = SendMessage(hIME, WM_IME_CONTROL, IMC_GETOPENSTATUS, 0);
        cout << (status ? "Korean" : "English") << endl;
        Sleep(100);
    }
    return 0;
}

< References >

ImmGetDefaultIMEWnd Function
https://learn.microsoft.com/ko-kr/windows/win32/api/imm/nf-imm-immgetdefaultimewnd

WM_IME_CONTROL Message
https://learn.microsoft.com/en-us/windows/win32/intl/wm-ime-control

IMC_GETOPENSTATUS(0x5) & IMC_GETCONVERSIONMODE(0x1)
https://learn.microsoft.com/en-us/previous-versions/windows/embedded/ms920838(v=msdn.10) https://learn.microsoft.com/en-us/previous-versions/windows/embedded/ms920931(v=msdn.10)

IMC_GETOPENSTATUS and IMC_GETCONVERSIONMODE are not officially supported in Windows 10. However, it seems to be almost the only way to implement what I want, and it works fine. I hope this article is helpful. (Google Translate)

1
  • 1
    I am glad you have got your solution and thanks for your sharing, I would appreciate it if you mark them as answer and this will be beneficial to other community.
    – Zeus
    Oct 13, 2020 at 9:13

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.