Programmatically Paste Clipboard Text to a CMD Window (C# or C++)

To accomplish this, I have tried different approaches: posting WM_PASTE, SendKeys sending ctrl-v... None of the method seems to be working so well.

A friend from work suggested trying WM_COMMAND. Since Spy++ doesn't work with cmd.exe, we had to use brute force to find the correct WPARAM for pasting.

The result is really simple, just one line code with C++:

 // hwnd is the window handle of a CMD window
SendMessage(hwnd, WM_COMMAND, 0xfff1, 0);

And here's the C# version with PInvoke:

private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

private const uint WM_COMMAND = 0x0111;

SendMessage(hwnd, WM_COMMAND, (IntPtr)0xfff1, IntPtr.Zero);

Comments (2)

  1. Mike Viens says:

    This is fantastic and works exactly as I need it to.  Thank you for exploring and sharing!

  2. spaceman says:

    Hi Bill

    Thank you for this post.

    You wrote that the wParam for the Paste command is 0xfff1.

    Can you please elaborate on how you got this value?

    (and how can I get values for other MenuItems there, since Spy++ won't work here..)

    Thank you very much

Skip to main content