C# Hooking a Console Application to Notepad
What i want to do is create a simple windows application that will hook itself onto NotePad and then simulate a keystroke. I have the process of opening up NotePad, bringing it to the foreground and then simulate the number 1 being pressed. However, if I click off of notepad, whatever is active becomes what is being typed in.
How do I bind this application to notepad, so that I can click and type in whatever, and this application will still push commands into notepad?
This is the DLL i'm using to simulate keypressing:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using WindowsInput;
namespace NotePadTesting
{
class Program
{
[DllImport("USER32.DLL", CharSet = CharSet.Unicode)]
public static extern IntPtr FindWindow(string lpClassName,
string lpWindowName);
// Activate an application window.
[DllImport("USER32.DLL")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
static void Main(string[] args)
{
Process[] processes = Process.GetProcessesByName("notepad");
if (processes.Length == 0)
{
Process.Start("notepad.exe");
processes = Process.GetProcessesByName("notepad");
}
if (processes.Length == 0)
{
throw new Exception("Could not find notepad huh....");
}
IntPtr WindowHandle = processes[0].MainWindowHandle;
SetForegroundWindow(WindowHandle);
for (int i = 0; i < 500; i++)
{
System.Threading.Thread.Sleep(100);
InputSimulator.SimulateKeyPress(VirtualKeyCode.VK_1);
}
}
}
}
If you already have the handle of the window you want to type in you can use the PostMessage function. You just have to google the virtual keycodes.
You will need to interface with Notepad.exe
via PostMessage. You will need to use P/Invoke
techniques to call this from User32.dll
:
using System.Runtime.InteropServices;
internal static class NativeMethods
{
// This method signature is derived from MSDN's PostMessage declaration.
[DllImport("user32.dll")]
public static extern bool PostMessage(IntPtr hwnd, uint msg, uint wParam, uint lParam);
// Other p/invoke methods go here, such as FindWindow...
}
You can locate Notepad
with FindWindow, allowing you to get the window's handle ( HWND
).
Once you do so, you can post Keyboard Notifications to that window. Those notifications simulate keyboard input and are only for that window, even if the window is minimized or otherwise not the foreground window.
The messages of importance will be WM_KEYDOWN, WM_KEYUP, and WM_CHAR. Many of these take in scan codes instead of virtual key codes , meaning you will need to translate back and forth. This is done with MapVirtualKey. All of the WM commands take specific forms of their LPARAM
and WPARAM
values, so check the MSDN documentation of what it expects.
There's a tool called Spy++
that (used to?) comes with Visual Studio that lets you peek at these messages. It's a great debugging/reverse engineering tool for this type of stuff.
Using all of the above Win32 API, you should be able to send keystrokes to an external window.
链接地址: http://www.djcxy.com/p/76458.html上一篇: mysql2sqlite.sh自动
下一篇: C#将控制台应用程序连接到记事本