Tigran84
Tigran84

Reputation: 193

How to get windows application text control value using control id

Using SPY++ I got window handle and using it I can get text control value from my code, but when I trying to find this window handle by application parent handle and control id through GetDlgItem(handle, ButtonId) function it throws error.

const int TextCtrlId = 0x0000E910;
IntPtr handle = IntPtr.Zero;
Process[] localAll = Process.GetProcesses();
foreach (Process p in localAll)
{
    if (p.MainWindowHandle != IntPtr.Zero)
    {
        ProcessModule pm = GetModule(p);
        if (pm != null && p.MainModule.FileName == fn)
            handle = p.MainWindowHandle;
    }
}
if (handle == IntPtr.Zero)
{
    Console.WriteLine("Not found");
    return;
}
GetText getText = new GetText();
Console.WriteLine("{0:X}", handle);
IntPtr hWndButton = GetDlgItem(handle, TextCtrlId);
string myText = getText.GetControlText(hWndButton);
Console.WriteLine("Error Code = {0}", GetLastError());
Console.WriteLine("Iput Window Text !!! = {0}", myText);

error code is ERROR_CONTROL_ID_NOT_FOUND

1421 (0x58D)

Control ID not found. but when I use direct handle (got it from SPY++) from function GetDlgItem(handle) I got same control id which in SPY++.

Upvotes: 0

Views: 466

Answers (1)

Mohammad
Mohammad

Reputation: 1990

Control ID is different from Windows Handle. What you have is a Windows Handle (HWND). Historically you could call GetWindowText() providing the handle to get the text of the control. But limitations have been imposed on this call (Since Windows XP if I remember well) for security reasons to counter password sniffing. Modern control inspection software depend on advanced techniques such as DLL injection to access controls in other processes with no restrictions (inject code in target process to read the control).

Upvotes: 1

Related Questions