Processing the WM_IME_COMPOSITION Message

Applications that process the WM_IME_COMPOSITION message test the bits in lParam parameter and call the ImmGetCompositionString function to retrieve the indicated string or data. The following example checks for the result string, allocates sufficient memory for the string, and retrieves the result string from the IME:

HIMC hIMC;
HWND hWnd;
DWORD dwSize;
HGLOBAL hstr;
LPSTR lpstr;

case WM_IME_COMPOSITION:
if (lParam & GCS_RESULTSTR) {
hIMC = ImmGetContext(hWnd);

If (!hIMC)
MyError(ERROR_NULLCONTEXT);

// Get the size of the result string.
dwSize = ImmGetCompositionString(hIMC, GCS_RESULTSTR, NULL, 0);

// increase buffer size for NULL terminator,
// maybe it is in UNICODE
dwSize += sizeof(WCHAR);

hstr = GlobalAlloc(GHND,dwSize);
if (hstr == NULL)
MyError(ERROR_GLOBALALLOC);

lpstr = GlobalLock(hstr);
if (lpstr == NULL)
MyError(ERROR_GLOBALLOCK);

// Get the result strings that is generated by IME into lpstr.
ImmGetCompositionString(hIMC, GCS_RESULTSTR, lpstr, dwSize);
ImmReleaseContext(hWnd, hIMC);

// add this string into text buffer of application

GlobalUnlock(hstr);
GlobalFree(hstr);
}