How to send text to Notepad in C#/Win32? - c#

I'm trying to use SendMessage to Notepad, so that I can insert written text without making Notepad the active window.
I have done something like this in the past using SendText, but that required giving Notepad focus.
Now, first I'm retrieving the Windows handle:
Process[] processes = Process.GetProcessesByName("notepad");
Console.WriteLine(processes[0].MainWindowHandle.ToString());
I've confirmed it's the right handle for Notepad, the same shown within Windows Task Manager.
[DllImport("User32.dll", EntryPoint = "SendMessage")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
From here, I haven't been able to get SendMessage to work in all my experimentation. Am I going in the wrong direction?

[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
private void button1_Click(object sender, EventArgs e)
{
Process [] notepads=Process.GetProcessesByName("notepad");
if(notepads.Length==0)return;
if (notepads[0] != null)
{
IntPtr child= FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);
SendMessage(child, 0x000C, 0, textBox1.Text);
}
}
WM_SETTEXT=0x000c

You first have to find the child window where the text is entered. You can do this by finding the child window with the window class "Edit".
Once you have that window handle, use WM_GETTEXT to get the text which is already entered, then modify that text (e.g., add your own), then use WM_SETTEXT to send the modified text back.

using System.Diagnostics;
using System.Runtime.InteropServices;
static class Notepad
{
#region Imports
[DllImport("user32.dll")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
private static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
//this is a constant indicating the window that we want to send a text message
const int WM_SETTEXT = 0X000C;
#endregion
public static void SendText(string text)
{
Process notepad = Process.Start(#"notepad.exe");
System.Threading.Thread.Sleep(50);
IntPtr notepadTextbox = FindWindowEx(notepad.MainWindowHandle, IntPtr.Zero, "Edit", null);
SendMessage(notepadTextbox, WM_SETTEXT, 0, text);
}
}

Related

Sending keys to background window (not notepad)

So I've been stuck for about 10 hours now. This code works perfectly for notepad. It sends the keystrokes to notepad in the background without having to click on it or anything. Problem is, no other window or process works and I've no idea why. I tried changing the names and using the window name instead of the process name stuff like that and nothing works. Only Notepad.
No, I can't use SendKeys because I need this to be in the background.
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("user32.dll")]
private static extern bool PostMessage(
IntPtr hWnd, // handle to destination window
UInt32 Msg, // message
Int32 wParam, // first message parameter
Int32 lParam // second message parameter
);
public void Start()
{
const int WM_KEYDOWN = 0x100;
// IntPtr Notepad = FindWindow(null, "");
//Console.WriteLine(Notepad);
Process notepadProccess = Process.GetProcessesByName("notepad")[0];
Console.WriteLine(notepadProccess.MainWindowHandle);
Console.WriteLine(notepadProccess.MainWindowTitle);
IntPtr editx = FindWindowEx(notepadProccess.MainWindowHandle, IntPtr.Zero, "Edit", null);
PostMessage(editx, WM_KEYDOWN, (int)Keys.X, 0);
}

C# Win32 Interop Crashes when Enumerating Window Handles

I have a C# wrapper for some Win32 operations involving window handles, but I am experiencing an unexpected crash, with no details, when I call a Win32 function.
Interestingly, this whole code sample works fine when the class is constructed (at application init time), but later fails when the same buildCache() method is called.
Here is the relevant code:
public delegate bool CallBack(int hWnd, int lParam);
public class Win32Interop {
private Dictionary<int, string> windowCache = new Dictionary<int, string>();
public Win32Interop() {
buildCache();
}
public void buildCache() {
windowCache.Clear();
CallBack hWndCacher = new CallBack(saveHWndHandler);
EnumWindows(hWndCacher, 0);
}
public void doThings(string title, uint message, bool rebuildCache = false) {
//Use the window title to get its handle
int hWnd = titleToHWnd(title, rebuildCache);
SendMessage(hWnd, message, 0, 0);
}
private bool saveHWndHandler(int hWnd, int lParam) {
if(IsWindow(hWnd) != 0) { / ***** CRASHES HERE ***** /
int length = GetWindowTextLength(hWnd);
StringBuilder title = new StringBuilder(length + 1);
GetWindowText(hWnd, title, title.Capacity);
string formatted = title.ToString().Trim();
windowCache.Add(hWnd, formatted);
}
return true;
}
private int titleToHWnd(string title, bool rebuildCache = false) {
if(rebuildCache)
buildCache();
if(windowCache.ContainsValue(title)) {
return windowCache.FirstOrDefault(x => x.Value.Contains(title)).Key;
} else {
throw new KeyNotFoundException(string.Format("\"{0}\" is not a window title which is available in the cache.", title));
}
}
#region Win32 API Functions
[DllImport("user32.dll")]
private static extern int EnumWindows(CallBack lpEnumFunc, int lParam);
[DllImport("user32.dll")]
private static extern int GetWindowText(int hWnd, StringBuilder lpString, int maxCount);
[DllImport("user32.dll")]
private static extern int GetWindowTextLength(int hWnd);
[DllImport("user32.dll")]
private static extern int IsWindow(int hWnd);
[DllImport("user32.lib")]
private static extern int SendMessage(int hWnd, uint Msg, int wParam, int lParam);
#endregion
}
Inside of the saveHWndHandler() method, I've marked the line were the debugger shows execution stops. Interestingly, there are usually ~300 window handles returned by EnumWindows(), and it always crashes on iteration number 45 or 46. The window handle it crashes on is a reasonable value, such as 12345.
According to MSDN, IsWindow() should return 0, if a window was not associated with the handle, not crash the thread.
Does anyone know why this is happening? There isn't a thrown exception, or any details in the Windows event log.
Thank you.
For those who don't want to figure out the buildCache() process works:
(1.) When buildCache() is called, the dictionary of <HWnd, Title> values is cleared.
(2.) The Win32 function EnumWindows() is called, which calls the saveHWndHandler() method for each window handle.
(3.) saveHWndHandler() will check if the current window handle still exists, the call another Win32 to function to get the window title from the handle.
(4.) The title and window handle are added to the dictionary.
I can't reproduce your issue, but a likely problem is that all of your P/Invoke signatures are wrong. HWND, LPARAM, and WPARAM data types need to at least be mapped to IntPtr in P/Invoke code.
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumWindows(EnumWindowsProc lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString,
int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
private static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindow(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam,
IntPtr lParam);
You'll need to change your corresponding instance method signatures and usages to match these correct signatures.

SendMessage Return Key and Text to WindowHandle in C#

I have searched google on this topic for hours and I am slightly confused. I want to send key strokes to fill in text fields and simulate press of the enter key using c#.
So far I have:
class Program
{
static void Main(string[] args)
{
Process p = Process.Start("File.exe");
p.WaitForInputIdle();
IntPtr hWnd = p.MainWindowHandle;
Command.SendMessage(hWnd,/* */);
}
}
public class Command
{
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, uint Msg, uint wParam, uint lParam);
public void sendReturn(IntPtr hWnd)
{
// do something cool here
}
}
Is there an easier way to do this?
Use PostMessage instead of send message
Usage:
[DllImport("User32.Dll")]
public static extern Int32 PostMessage(IntPtr hWnd, int msg, int wParam, int lParam);
const int WM_KEYDOWN = 0x0100;
const int VK_TAB = 0x09;
PostMessage(process.MainWindowHandle, WM_KEYDOWN, VK_TAB, 1);

Controlling external Qt application - Open File

I need to be able to control an external Qt application so that I can open a file in the application.
I have tried using Process to get the Window Handle and then via PInvoke using GetMenu, GetSubMenu and GetMenuItemID to get all the parameters for using SendMessage and "Click" on the open menu in the external application
This works perfectly if I try it with Notepad as the external app, but not with the actual application which is written using Qt.
I do get the Window handle but GetMenu returns 0.
I have this code
[DllImport("user32.dll")]
private static extern IntPtr GetMenu(IntPtr hWnd);
[DllImport("user32.dll")]
private static extern IntPtr GetSubMenu(IntPtr hMenu, int nPos);
[DllImport("user32.dll")]
private static extern uint GetMenuItemID(IntPtr hMenu, int nPos);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, IntPtr lParam);
private void OpenButton_Click(object sender, EventArgs e)
{
OpenDocument("notepad", "test.doc");
}
public void OpenDocument(string windowTitle, string document)
{
IntPtr hWnd = GetWindow(windowTitle);
IntPtr hMenu = GetMenu(hWnd);
IntPtr hSubMenu = GetSubMenu(hMenu, 0); // File menu
uint menuItemId = GetMenuItemID(hSubMenu, 2); // Open
IntPtr ptr = SendMessage(hWnd, (uint)WM.COMMAND, (IntPtr)menuItemId, IntPtr.Zero);
}
private static IntPtr GetWindow(string windowTitle)
{
IntPtr hWnd = IntPtr.Zero;
Process[] processes = Process.GetProcesses();
foreach (Process p in processes)
{
if (p.MainWindowTitle.IndexOf(windowTitle, StringComparison.InvariantCultureIgnoreCase) > -1)
{
hWnd = p.MainWindowHandle;
break;
}
}
return hWnd;
}
How can I get the handles of the menu and submenu and the menuitemid from a Qt application?
// Anders

IPC in C#, sending text from one exe to another exe

I would like to send a message from a WPF application's textbox to an open notepad. After I click the button next to the the textbox, I would like the content is written into the notepad, I mean.
How can I send messages between 2 different applications ?
[DllImport("user32.dll", EntryPoint = "FindWindowEx")]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
[DllImport("User32.dll")]
public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);
private static void DoSendMessage(string message)
{
Process notepad = Process.Start(new ProcessStartInfo("notepad.exe"));
notepad.WaitForInputIdle();
if (notepad != null)
{
IntPtr child = FindWindowEx(notepad.MainWindowHandle, new IntPtr(0), "Edit", null);
SendMessage(child, 0x000C, 0, message);
}
}
For sending data between two applications you control, you could use NamedPipeClientStream and NamedPipeServerStream

Categories