With SendInput
:
void SetNumLock (bool active)
{
BYTE keyState[256]; GetKeyboardState((LPBYTE)&keyState);
bool active0= keyState[VK_NUMLOCK] & 1;
if (active0!=active) {
INPUT inp[2]; ZeroMemory(inp, sizeof(inp));
inp[0].type=INPUT_KEYBOARD;
inp[0].ki.dwFlags=0;
inp[0].ki.wVk= VK_NUMLOCK;
inp[1].type=INPUT_KEYBOARD;
inp[1].ki.dwFlags=KEYEVENTF_KEYUP;
inp[1].ki.wVk=VK_NUMLOCK;
SendInput(2, inp, sizeof(INPUT));
}
}