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:UsersusernameProjectx64DebugProject.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:WindowsSysWOW64imm32.dll : fatal error LNK1107: invalid or corrupt file: cannot read at 0x2D0
Either the one in C:WindowsSysWOW64
or C:WindowsSystem32
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!
2
Answers
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
the library you are using is supposed to be for Win32 you should include imm.h and immdev.h