How do I use these PostMessage Lparam and Wparam's?


I'm really struggling to understand how PostMessage works. I'm from a web dev background so its all very foreign to me. I'm trying to send a single "a" charcter to a third party application. I've used spy++ to get the PostMessage (params below) required but I cant make sense of how to use the Lparam and Wparam.

This is what I have so far. I'm assuming 00000041 (and the others from spy++) is actually hexadecimal and I'm correct in putting 0x in font of it?

[return: MarshalAs(UnmanagedType.Bool)]
static extern bool PostMessage(IntPtr hWnd, uint msg, IntPtr wParam, IntPtr lParam);    

const int WM_KEYDOWN = 0x100;
const int WM_KEYUP   = 0x101;
const Int32 WM_CHAR  = 0x0102;

PostMessage(WindowHandle, WM_KEYDOWN, (IntPtr)(0x00000041), (IntPtr)(0x001E0001)); 
PostMessage(WindowHandle, WM_CHAR, (IntPtr)(0x00000061), (IntPtr)(0x001E0001)); 
PostMessage(WindowHandle, WM_KEYUP, (IntPtr)(0x00000041), (IntPtr)(0xC01E0001)); 

That gives me an algorithmic overflow...

And before anyone tells me to use sendinput this is for a window not in focus :-p

asked on Stack Overflow Jun 19, 2016 by Mr J • edited Jun 19, 2016 by Mr J

1 Answer


You don't send WM_CHAR, WM_CHAR is synthesized by the application in TranslateMessage - i.e. the application posts it to itself. Either send only the WM_CHAR, or send only WM_KEYDOWN and WM_KEYUP. If sending KEYUP you need to have a delay to allow the application to synthesize the WM_CHAR before you send the KEYDOWN, or they will be processed out of order. Even then you will have a problem with the async key state. However the long and short of it is: You can't synthesize keyboard input using PostMessage.

Here's some background reading:

Your best bet is probably to put the application to the foreground and use SendInput.

answered on Stack Overflow Jun 19, 2016 by Ben

User contributions licensed under CC BY-SA 3.0