I tried to send a key to an application. For an easy test I just used notepad. That's what the code looks like:
[DllImport("USER32.DLL", EntryPoint = "SendMessageW", SetLastError = true,
CharSet = CharSet.Unicode, ExactSpelling = true,
CallingConvention = CallingConvention.StdCall)]
public static extern bool SendMessage(IntPtr hwnd, int Msg, int wParam, int lParam);
const int WM_KEYDOWN = 0x100;
const int WM_a = 0x41;
public void Press()
{
Process[] p = Process.GetProcessesByName("notepad");
IntPtr pHandle = p[0].MainWindowHandle;
SendMessage(pHandle, WM_KEYDOWN, WM_a, 0);
}
But nothing happens.
My main goal is to send the key to an elevated application, but I would be happy to send it to notepad first. I want to work with SendMessage, because I want to control how long I press a button, also I don't want to have the other application in the foreground. That's the reason I am not working with SendKeys.
Several problems:
The neck shot: Vista and Win7 UIPI (User Interface Privilege Isolation) prevents a restricted process from injecting messages into an elevated process.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With