I am trying to read textbox value from another form application, so far everything has been successful, I did tests on the an notepad app, problems started when I want to get a value from the textbox which is part of the mdi client. My code completely does not see the text field that is part of another form in the mdi client.
My code:
{
public IntPtr hWnd;
public string Text;
}
const int WM_GETTEXT = 0x0D;
const int WM_GETTEXTLENGTH = 0x0E;
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll", SetLastError = true)]
public static extern int SendMessage(IntPtr hWnd, int msg, int Param, System.Text.StringBuilder text);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
List<WinText> windows = new List<WinText>();
//find the "first" window
IntPtr hWnd = FindWindow("notepad", null);
while (hWnd != IntPtr.Zero)
{
//find the control window that has the text
IntPtr hEdit = FindWindowEx(hWnd, IntPtr.Zero, "RichEditD2DPT", null);
//initialize the buffer. using a StringBuilder here
System.Text.StringBuilder sb = new System.Text.StringBuilder(255); // or length from call with GETTEXTLENGTH
//get the text from the child control
int RetVal = SendMessage(hEdit, WM_GETTEXT, sb.Capacity, sb);
windows.Add(new WinText() { hWnd = hWnd, Text = sb.ToString() });
label2.Text = "" + windows.Count();
//find the next window
hWnd = FindWindowEx(IntPtr.Zero, hWnd, "notepad", null);
}
//do something clever
windows.OrderBy(x => x.Text).ToList().ForEach(y => label1.Text = " {0} = {1}\n" + y.hWnd + "\n" + y.Text);
}
Related
I want to get a textbox handle from process name. I checked it with Spy++ (it's an exe found in the internet, so nothing special):
now i want to get this TEdit, but it always return NULL. What am I doing wrong?
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string lpszWindow);
private static void Main(string[] args)
{
var processes = Process.GetProcesses();
var proc = Array.Find(processes, x => string.Equals(x.MainWindowTitle, "ExeLock", StringComparison.OrdinalIgnoreCase));
var handle = proc.MainWindowHandle;
//IntPtr edit = FindWindowEx(handle, IntPtr.Zero, "TEdit", null);
IntPtr hwndparent = handle, hwndchild = IntPtr.Zero;
do
{
hwndchild = FindWindowEx(hwndparent, hwndchild, null, null);
} while (hwndchild != IntPtr.Zero);
}
I didn't found it because it creates multiple windows and some of them are hidden (including "main window"). Here is screenshot:
So I just enum every window of a process, find TFormPassDialog and then everything works fine. Here is my code:
class ExeLockFounder
{
const uint WM_SETTEXT = 0x000C;
delegate bool EnumDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
[DllImport("user32.dll", SetLastError = true)]
private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string className, string lpszWindow);
[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr SendMessage(IntPtr hWnd, UInt32 Msg, IntPtr wParam, [MarshalAs(UnmanagedType.LPStr)] string lParam);
private static IntPtr GetWindowByClassName(IEnumerable<IntPtr> windows, string className)
{
foreach (var window in windows)
{
var sb = new StringBuilder(256);
GetClassName(window, sb, sb.Capacity);
if (sb.ToString() == className)
return window;
}
return IntPtr.Zero;
}
static IEnumerable<IntPtr> EnumerateProcessWindowHandles(Process process)
{
var handles = new List<IntPtr>();
foreach (ProcessThread thread in process.Threads)
EnumThreadWindows(thread.Id, (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
return handles;
}
private readonly IntPtr _editHandle;
public ExeLockFounder()
{
var processes = Process.GetProcessesByName("Setup");
var proc = Array.Find(processes, x => string.Equals(x.MainWindowTitle, "ExeLock", StringComparison.OrdinalIgnoreCase));
var windows = EnumerateProcessWindowHandles(proc);
var hWnd = GetWindowByClassName(windows, "TFormPassDialog");
_editHandle = FindWindowEx(hWnd, IntPtr.Zero, "TEdit", null);
}
public void SendText(string message)
{
SendMessage(_editHandle, WM_SETTEXT, IntPtr.Zero, message);
}
}
I need to check what window the user currently has selected, and do stuff if they have a specific program selected.
I haven't used the GetForegroundWindow function before, and can't find any information on how to use it in this manner.
I simply need an if comparing the current window to see if its a specific program. However the GetForegroundWindow function doesn't give back a string or int it seems. So mainly I don't know how to find out the value of the program window I want to compare it to.
I currently have the code to get the current window:
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
IntPtr selectedWindow = GetForegroundWindow();
I need to be able to apply it as follows ideally:
If (selectedWindow!="SpecificProgram")
{
<Do this stuff>
}
I'm hoping the GetForegroundWindow value/object is unique to each program and doesn't function in some way that each specific program/window has different values each-time.
I'm also doing this as part of a windows form though I doubt it matters.
-Thanks for any help
Edit: This way works, and uses the tile of the current window, which makes it perfect for checking if the window is right easily:
[DllImport("user32.dll")]
static extern IntPtr GetForegroundWindow();
[DllImport("user32.dll")]
static extern int GetWindowText(IntPtr hWnd, StringBuilder text, int count);
private string GetActiveWindowTitle()
{
const int nChars = 256;
StringBuilder Buff = new StringBuilder(nChars);
IntPtr handle = GetForegroundWindow();
if (GetWindowText(handle, Buff, nChars) > 0)
{
return Buff.ToString();
}
return null;
}
and then I can just do:
if (GetActiveWindowTitle()=="Name of Window")
{
DoStuff.jpg
}
It has some code but it works:
#region Retrieve list of windows
[DllImport("user32")]
private static extern int GetWindowLongA(IntPtr hWnd, int index);
[DllImport("USER32.DLL")]
private static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("USER32.DLL")]
private static extern bool EnumWindows(EnumWindowsProc enumFunc, int lParam);
[DllImport("user32.dll")]
private static extern IntPtr GetForegroundWindow();
private const int GWL_STYLE = -16;
private const ulong WS_VISIBLE = 0x10000000L;
private const ulong WS_BORDER = 0x00800000L;
private const ulong TARGETWINDOW = WS_BORDER | WS_VISIBLE;
internal class Window
{
public string Title;
public IntPtr Handle;
public override string ToString()
{
return Title;
}
}
private List<Window> windows;
private void GetWindows()
{
windows = new List<Window>();
EnumWindows(Callback, 0);
}
private bool Callback(IntPtr hwnd, int lParam)
{
if (this.Handle != hwnd && (GetWindowLongA(hwnd, GWL_STYLE) & TARGETWINDOW) == TARGETWINDOW)
{
StringBuilder sb = new StringBuilder(100);
GetWindowText(hwnd, sb, sb.Capacity);
Window t = new Window();
t.Handle = hwnd;
t.Title = sb.ToString();
windows.Add(t);
}
return true; //continue enumeration
}
#endregion
And to check user window:
IntPtr selectedWindow = GetForegroundWindow();
GetWindows();
for (i = 0; i < windows.Count; i++)
{
if(selectedWindow == windows[i].Handle && windows[i].Title == "Program Title X")
{
//Do stuff
break;
}
}
Valter
I have a code
private const int WM_CLOSE = 16;
private const int BN_CLICKED = 245;
[DllImport("User32.dll")]
public static extern Int32 FindWindow(String lpClassName, String lpWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern int SendMessage(int hWnd, int msg, int wParam, IntPtr lParam);
[DllImport("user32.dll", SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string className, string windowTitle);
[DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
private static extern IntPtr FindWindowByCaption(IntPtr ZeroOnly, string lpWindowName);
public void Click(string _btnTitle)
{
int hwnd = 0;
IntPtr hwndChild = IntPtr.Zero;
//Get a handle for the Calculator Application main window
// foreach (Process p in Process.GetProcesses())
//{
hwnd = FindWindow(null, FrmTitle);
if (hwnd != 0)
{
hwndChild = FindWindowEx((IntPtr)hwnd, IntPtr.Zero, "Button", _btnTitle);
SendMessage((int)hwndChild, BN_CLICKED, 0, IntPtr.Zero);
}
}
I can't Click button "Yes" on MessageBox of application :(
Anyone got a tip? Tks
You aren't sending the correct message.
Try using BM_CLICK (0x00F5) in your call to SendMessage(). That should work provided that hwndChild is the window handle of the button, rather than the container dialog box.
BN_CLICKED doesn't work because that is a notification code, not a message.
I have been tasked with changing the text of a button in a window. I don't have and cannot access the source code as it's owned by a company we have a paid subscription with.
How can I change the button text with no source code? I'm trying with pInvoke and having problems. The window title changes depending on who you are working with:
"Order Entry Sheet - LASTNAME, FIRSTNAME"
So the window title may not be useable for me inside of the win32 call
FindWindow(string lpClassName, string lpWindowName);
I know both params are optional. I'm using Spy++ and I'm not sure what to use for lpClassName. The class name I see listed is #32770 (Dialog). I tried it and got a return of 0.
IntPtr windowHandle = FindWindow("#32770 (Dialog)", null);
How can I change the button text from another process?
UPDATE
According to MSDN I should be able to achieve this via SetWindowText.
Changes the text of the specified window's title bar (if it has one).
If the specified window is a control, the text of the control is
changed. However, SetWindowText cannot change the text of a control in
another application.
I can't use SetWindowText to do what I want. Is it possible to use something else?
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
static extern int GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowTextLength(IntPtr hWnd);
[DllImport("user32", SetLastError = true)]
public static extern int EnumWindows(CallBack x, int y);
[DllImport("user32.dll", SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
static extern bool EnumChildWindows(IntPtr hwndParent, CallBack lpEnumFunc, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr SendMessage(HandleRef hWnd, uint Msg, IntPtr wParam, string lParam);
public const uint WM_SETTEXT = 0x000C;
public delegate bool CallBack(int hwnd, int lParam);
public static void Main()
{
CallBack windowsCallback = new CallBack(IterateWindows);
EnumWindows(windowsCallback, 0);
}
public static bool IterateChildren(int hwnd, int lParam)
{
string newButtonText = "Some text";
bool continueIteratingChildren = true;
//Console.WriteLine("Child handle: " + hwnd);
int length = GetWindowTextLength((IntPtr)hwnd);
StringBuilder sb = new StringBuilder(length + 1);
GetWindowText((IntPtr)hwnd, sb, sb.Capacity);
//Console.WriteLine(sb);
if (sb.ToString().StartsWith("My Button Text "))
{
HandleRef hrefHWndTarget = new HandleRef(null, (IntPtr)hwnd);
SendMessage(hrefHWndTarget, WM_SETTEXT, IntPtr.Zero, newButtonText);
continueIteratingChildren = false;
}
return continueIteratingChildren;
}
public static bool IterateWindows(int hwnd, int lParam)
{
bool continueIteratingWindows = true;
int windowTextLength = GetWindowTextLength((IntPtr)hwnd);
StringBuilder sb = new StringBuilder(windowTextLength + 1);
GetWindowText((IntPtr)hwnd, sb, sb.Capacity);
if (sb.ToString().StartsWith("My Window Caption"))
{
//Console.Write("Window handle is ");
//Console.WriteLine(hwnd);
//Console.WriteLine(sb);
//Console.WriteLine(Marshal.GetLastWin32Error());
var childrenCallback = new CallBack(IterateChildren);
EnumChildWindows((IntPtr)hwnd, childrenCallback, IntPtr.Zero);
continueIteratingWindows = false;
}
return continueIteratingWindows;
}
I am basically writing a specialized macro player/recorder in C#. One thing I need to be able to do is wait for a pop up window (something like a Save As... dialog box) that I can then select to continue playing macro input into. Ideally, I would like to be able to poll for open windows and search through their titles for a matching window title.
Obviously I can't use Processes.GetProcesses() because a dialog most likely will not show up as a new process.
Where do I look to get open windows and their titles?
If you want to poll all open windows, you might use EnumWindows(). I didn't compile this code, but it should be pretty close to functional.
public class ProcessWindows
{
List<Window> visibleWindows = new List<Window>();
List<IntPtr> allWindows = new List<IntPtr>();
/// <summary>
/// Contains information about visible windows.
/// </summary>
public struct Window
{
public IntPtr Handle { get; set; }
public string Title { get; set; }
}
[DllImport("user32.dll")]
static extern int EnumWindows(EnumWindowsCallback lpEnumFunc, int lParam);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetWindowLong(IntPtr hWnd, int nIndex);
[DllImport("user32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
private static extern void GetWindowText(IntPtr hWnd, StringBuilder lpString, int nMaxCount);
delegate bool EnumWindowsCallback(IntPtr hwnd, int lParam);
public ProcessWindows()
{
int returnValue = EnumWindows(Callback, 0);
if (returnValue == 0)
{
throw new System.ComponentModel.Win32Exception(Marshal.GetLastWin32Error(), "EnumWindows() failed");
}
}
private bool Callback(IntPtr hwnd, int lParam)
{
const int WS_BORDER = 0x800000;
const int WS_VISIBLE = 0x10000000;
const int GWL_STYLE = (-16);
// You'll have to figure out which windows you want here...
int visibleWindow = WS_BORDER | WS_VISIBLE;
if ((GetWindowLong(hwnd, GWL_STYLE) & visibleWindow) == visibleWindow)
{
StringBuilder sb = new StringBuilder(100);
GetWindowText(hwnd, sb, sb.Capacity);
this.visibleWindows.Add(new Window()
{
Handle = hwnd,
Title = sb.ToString()
});
}
return true; //continue enumeration
}
public ReadOnlyCollection<Window> GetVisibleWindows()
{
return this.visibleWindows.AsReadOnly();
}
}
}
I think you want FindWindow().