Home > front end >  Unable to link imm32.dll in Visual Studio
Unable to link imm32.dll in Visual Studio

Time:09-02

I'm trying to use a ImmGetContext() from <imm.h> in a Visual Studio 2022 project, which gives the following errors:

1>Project.obj : error LNK2019: unresolved external symbol ImmGetContext referenced in function "void __cdecl activeWindowChangeHandler(struct HWINEVENTHOOK__ *,unsigned long,struct HWND__ *,long,long,unsigned long,unsigned long)" (?activeWindowChangeHandler@@YAXPEAUHWINEVENTHOOK__@@KPEAUHWND__@@JJKK@Z)
1>Project.obj : error LNK2019: unresolved external symbol ImmGetConversionStatus referenced in function "void __cdecl activeWindowChangeHandler(struct HWINEVENTHOOK__ *,unsigned long,struct HWND__ *,long,long,unsigned long,unsigned long)" (?activeWindowChangeHandler@@YAXPEAUHWINEVENTHOOK__@@KPEAUHWND__@@JJKK@Z)
1>C:\Users\username\Project\x64\Debug\Project.exe : fatal error LNK1120: 2 unresolved externals

I tried adding imm32.dll from the OS itself to the project's Project -> Linker -> General as well as Linker options. It seems to be recognized but couldn't be parsed, here are the new errors after the change:

1>C:\Windows\SysWOW64\imm32.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2D0

Either the one in C:\Windows\SysWOW64\ or C:\Windows\System32\ could not be parsed correctly.

Here is the code I'm trying to implement:

#include <iostream>
#include <windows.h>

using namespace std;

void CALLBACK activeWindowChangeHandler(HWINEVENTHOOK hWinEventHook, DWORD dwEvent, HWND hwnd, LONG idObject, LONG idChild, DWORD dwEventThread, DWORD dwmsEventTime) {
    DWORD dwConversion, dwSentence;
    auto himc = ImmGetContext(hwnd);
    if (ImmGetConversionStatus(himc, &dwConversion, &dwSentence)) {
        wcout << L"Current conversion mode: " << dwConversion << L"sentence mode: " << dwSentence << endl;
    }
    else {
        wcout << L"Failed to get conversion mode" << endl;
    }
}

int main() {
    auto hEvent = SetWinEventHook(EVENT_SYSTEM_FOREGROUND, EVENT_SYSTEM_FOREGROUND, NULL, activeWindowChangeHandler, 0, 0, WINEVENT_OUTOFCONTEXT | WINEVENT_SKIPOWNPROCESS);
    while (true);
}

Thank you!

CodePudding user response:

the library you are using is supposed to be for Win32 you should include imm.h and immdev.h

#include <immdev.h>
#include <imm.h>

CodePudding user response:

Seems like I'm doing it in a totally wrong way. Adding #pragma comment(lib, "imm32") to the code have solved the problem.

Thanks to ImmGetContext returns zero always

  • Related