DLLIMPORT - SENDMESSAGE
-
Hi, Please let me know, where I am doing wrong. because following program is not receiving text from notepad. Thanks alot
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace READVALUES
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, long wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);public Form1() { InitializeComponent(); } private void Form1\_Load(object sender, EventArgs e) { } private void button1\_Click(object sender, EventArgs e) { Process\[\] processes = Process.GetProcessesByName("notepad"); foreach (Process p in processes) { string myGotWindow = p.MainWindowTitle; IntPtr pFoundWindow = p.MainWindowHandle; const uint WM\_GETTEXT = 0x0D; StringBuilder sb = new StringBuilder(); IntPtr retVal = SendMessage(pFoundWindow, WM\_GETTEXT, 100, sb); textBox1.Text = sb.ToString(); } } }
}
-
Hi, Please let me know, where I am doing wrong. because following program is not receiving text from notepad. Thanks alot
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace READVALUES
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, long wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);public Form1() { InitializeComponent(); } private void Form1\_Load(object sender, EventArgs e) { } private void button1\_Click(object sender, EventArgs e) { Process\[\] processes = Process.GetProcessesByName("notepad"); foreach (Process p in processes) { string myGotWindow = p.MainWindowTitle; IntPtr pFoundWindow = p.MainWindowHandle; const uint WM\_GETTEXT = 0x0D; StringBuilder sb = new StringBuilder(); IntPtr retVal = SendMessage(pFoundWindow, WM\_GETTEXT, 100, sb); textBox1.Text = sb.ToString(); } } }
}
-
Hi, Please let me know, where I am doing wrong. because following program is not receiving text from notepad. Thanks alot
using System;
using System.Diagnostics;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;namespace READVALUES
{
public partial class Form1 : Form
{
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, long wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);public Form1() { InitializeComponent(); } private void Form1\_Load(object sender, EventArgs e) { } private void button1\_Click(object sender, EventArgs e) { Process\[\] processes = Process.GetProcessesByName("notepad"); foreach (Process p in processes) { string myGotWindow = p.MainWindowTitle; IntPtr pFoundWindow = p.MainWindowHandle; const uint WM\_GETTEXT = 0x0D; StringBuilder sb = new StringBuilder(); IntPtr retVal = SendMessage(pFoundWindow, WM\_GETTEXT, 100, sb); textBox1.Text = sb.ToString(); } } }
}
Give these definitions a try.
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] StringBuilder lParam);[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);The reason for the FindWindowEx is so that you can find the child window that contains the text. If you use the MainWindowHandle, you will retrieve the window title. You need to find the child window whose class name is "Edit". Also give the string builder an ample initial size. The default size is 16 characters. I will leave it to you to write the logic to compare the returned number of characters against the stringbuilder's "Capacity" property to determine if you got all the text. If returned length is Capacity -1, there may be more text that you did not retrieve.
Process[] processes = Process.GetProcessesByName("notepad");
foreach (Process p in processes)
{
string myGotWindow = p.MainWindowTitle;
IntPtr pEditWindow = FindWindowEx(p.MainWindowHandle, IntPtr.Zero, "Edit", null);const uint WM\_GETTEXT = 0x0D; StringBuilder sb = new StringBuilder(1000); IntPtr retVal = SendMessage(pEditWindow, WM\_GETTEXT, (IntPtr)sb.Capacity, sb); textBox1.Text = sb.ToString();
}