首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在我的C#应用程序中获得类似于Spy++的功能?

如何在我的C#应用程序中获得类似于Spy++的功能?
EN

Stack Overflow用户
提问于 2009-12-28 09:02:22
回答 4查看 22.2K关注 0票数 10

我对开发开源密码管理器Keepass的插件很感兴趣。目前,Keepass可以根据窗口标题来检测要为您复制/粘贴的密码。这可以防止Keepass检测到你需要的当前密码,用于那些没有根据当前站点主动更新窗口标题的应用程序(例如Chrome)。

如何遍历另一个类似于Spy++工作方式的进程窗口元素(按钮、标签、文本框)?当您运行Spy++时,您可以将鼠标悬停在其他程序窗口上,并获取有关各种控件(标签、文本框等)的各种属性的各种信息。理想情况下,我希望我的Keepass插件能够通过遍历活动窗口的元素来增强当前窗口的检测,从而努力找到一个匹配的帐户来复制/粘贴密码。

如何遍历其他进程窗口元素并能够使用C#检索标签和文本框值?

EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2009-12-28 10:07:38

我在这里回答了类似的问题:How can I detect if a thread has windows handles?。正如它所述,其主要思想是使用EnumWindowsEnumChildWindows调用枚举进程窗口及其子窗口以获取窗口句柄,然后使用WM_GETTEXT调用GetWindowText或SendDlgItemMessage以获取窗口文本。我已经修改了代码来制作一个示例,它应该可以做你需要的事情(对不起,它有点长:)。它遍历进程及其窗口,并将窗口文本转储到控制台中。

代码语言:javascript
复制
static void Main(string[] args)
{
    foreach (Process procesInfo in Process.GetProcesses())
    {
        Console.WriteLine("process {0} {1:x}", procesInfo.ProcessName, procesInfo.Id);
        foreach (ProcessThread threadInfo in procesInfo.Threads)
        {
            // uncomment to dump thread handles
            //Console.WriteLine("\tthread {0:x}", threadInfo.Id);
            IntPtr[] windows = GetWindowHandlesForThread(threadInfo.Id);
            if (windows != null && windows.Length > 0)
                foreach (IntPtr hWnd in windows)
                    Console.WriteLine("\twindow {0:x} text:{1} caption:{2}",
                        hWnd.ToInt32(), GetText(hWnd), GetEditText(hWnd));
        }
    }
    Console.ReadLine();
}

private static IntPtr[] GetWindowHandlesForThread(int threadHandle)
{
    _results.Clear();
    EnumWindows(WindowEnum, threadHandle);
    return _results.ToArray();
}

// enum windows

private delegate int EnumWindowsProc(IntPtr hwnd, int lParam);

[DllImport("user32.Dll")]
private static extern int EnumWindows(EnumWindowsProc x, int y);
[DllImport("user32")]
private static extern bool EnumChildWindows(IntPtr window, EnumWindowsProc callback, int lParam);
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId(IntPtr handle, out int processId);

private static List<IntPtr> _results = new List<IntPtr>();

private static int WindowEnum(IntPtr hWnd, int lParam)
{
    int processID = 0;
    int threadID = GetWindowThreadProcessId(hWnd, out processID);
    if (threadID == lParam)
    {
        _results.Add(hWnd);
        EnumChildWindows(hWnd, WindowEnum, threadID);
    }
    return 1;
}

// get window text

[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);

private static string GetText(IntPtr hWnd)
{
    int length = GetWindowTextLength(hWnd);
    StringBuilder sb = new StringBuilder(length + 1);
    GetWindowText(hWnd, sb, sb.Capacity);
    return sb.ToString();
}

// get richedit text 

public const int GWL_ID = -12;
public const int WM_GETTEXT = 0x000D;

[DllImport("User32.dll")]
public static extern int GetWindowLong(IntPtr hWnd, int index);
[DllImport("User32.dll")]
public static extern IntPtr SendDlgItemMessage(IntPtr hWnd, int IDDlgItem, int uMsg, int nMaxCount, StringBuilder lpString);
[DllImport("User32.dll")]
public static extern IntPtr GetParent(IntPtr hWnd);

private static StringBuilder GetEditText(IntPtr hWnd)
{
    Int32 dwID = GetWindowLong(hWnd, GWL_ID);
    IntPtr hWndParent = GetParent(hWnd);
    StringBuilder title = new StringBuilder(128);
    SendDlgItemMessage(hWndParent, dwID, WM_GETTEXT, 128, title);
    return title;
}

希望这能有所帮助,致敬

票数 25
EN

Stack Overflow用户

发布于 2009-12-28 09:42:53

看看这篇文章here,它包含了关于托管间谍的信息以及作者为什么要写这个工具。

票数 3
EN

Stack Overflow用户

发布于 2016-01-12 14:20:26

您可以使用HWndSpy。源代码是here

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1967604

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档