我想知道我将如何去做这件事,因为我已经尝试了无数不同的 user32 函数并在网上进行了相当广泛的研究,但不幸的是我还没有想出一个解决方案。
有一个应用程序有 5 个线程。只要进程的 PID,就可以通过 .NET Process 类的 GetProcessById 方法轻松访问这些线程。但是,似乎没有可用于提供线程 ID 并枚举其窗口(父窗口或子窗口)的函数。其中一个线程共有 10 个窗口,其中 9 个隐藏,1 个可见。该可见线程的标题是我试图以编程方式获得的。
我最近的方法是获取进程句柄,将其放入 EnumChildWindows,并尝试以这种方式将每个窗口句柄添加到一个集合中,但我的集合始终是空的。
这是我在 ProcessThreadsView 工具中看到的屏幕截图:
有什么我想念的吗?我给该工具的作者发了电子邮件,想看看他是怎么做的,但我想我会请大家看看是否有既定的方法。
更新:我试过使用 GetGUIThreadInfo,我是这样调用它的:
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
[StructLayout(LayoutKind.Sequential)]
public struct GUITHREADINFO
{
public uint cbSize;
public uint flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public Rect rcCaret;
}
static IEnumerable<IntPtr> EnumerateThreadWindowHandlesByProcessId(int processId)
{
List<IntPtr> threadWindowHandles = new List<IntPtr>();
foreach (ProcessThread thread in Process.GetProcessById(processId).Threads)
{
GUITHREADINFO threadInfo = new GUITHREADINFO();
threadInfo.cbSize = (uint)Marshal.SizeOf(threadInfo);
bool returnValue = GetGUIThreadInfo((uint)thread.Id, out threadInfo);
threadWindowHandles.Add(threadInfo.hwndActive);
}
return threadWindowHandles;
}
更新 2:
使用 EnumThreadWindows,这就是我得到的:
public delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
private static bool ThreadWindows(IntPtr handle, IntPtr param)
{
//get window from handle later, testing for now
logger.Info("foo bar");
return true;
}
[STAThread]
public void Execute()
{
Process[] processes = Process.GetProcessesByName("MyProcessName");
Process processOfInterest = processes[0];
foreach (ProcessThread thread in processOfInterest.Threads)
{
EnumThreadWindows(thread.Id, new EnumThreadDelegate(ThreadWindows), IntPtr.Zero);
}
}
最佳答案
我相信您正在寻找GetGUIThreadInfo .
更新:您的 p/invoke 有一些问题。
rect 域是错误的(在这里并不重要)。使用这个:
[StructLayout(LayoutKind.Sequential)]
public struct Rect
{
public int Left;
public int Top;
public int Right;
public int Bottom;
}
而不是 System.Drawing.Rectangle。
其他字段的类型声明也关闭了。应该是这样的:
[StructLayout(LayoutKind.Sequential)]
public struct GUITHREADINFO
{
public uint cbSize;
public uint flags;
public IntPtr hwndActive;
public IntPtr hwndFocus;
public IntPtr hwndCapture;
public IntPtr hwndMenuOwner;
public IntPtr hwndMoveSize;
public IntPtr hwndCaret;
public Rect rcCaret;
}
您最好检查返回值并在 DllImport 属性中使用 SetLastError = true,这样您就可以辨别函数调用失败的原因。还有,你
应该通过 ref 传递结构,因为您传递的是结构大小。
[DllImport("user32.dll", SetLastError=true)]
static extern bool GetGUIThreadInfo(uint idThread, ref GUITHREADINFO lpgui);
使用Marshal.GetLastWin32Error() 获取错误代码。但仅当 GetGUIThreadInfo 返回 false 时才检查此项。
通过上述更改,在给定有效的线程 ID 后,对 GetGUIThreadInfo 的调用将起作用。另请注意,其他进程需要获得输入焦点,以便 GetGUIThreadInfo 返回任何有用信息。
If the specified thread does not exist or have an input queue, the function will fail.
关于c# - 获取线程的可见窗口的标题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9318058/
我收到这个错误:RuntimeError(自动加载常量Apps时检测到循环依赖当我使用多线程时。下面是我的代码。为什么会这样?我尝试多线程的原因是因为我正在编写一个HTML抓取应用程序。对Nokogiri::HTML(open())的调用是一个同步阻塞调用,需要1秒才能返回,我有100,000多个页面要访问,所以我试图运行多个线程来解决这个问题。有更好的方法吗?classToolsController0)app.website=array.join(',')putsapp.websiteelseapp.website="NONE"endapp.saveapps=Apps.order("
我正在编写一个方法,它将在一个类中定义一个实例方法;类似于attr_accessor:classFoocustom_method(:foo)end我通过将custom_method函数添加到Module模块并使用define_method定义方法来实现它,效果很好。但我无法弄清楚如何考虑类(class)的可见性属性。例如,在下面的类中classFoocustom_method(:foo)privatecustom_method(:bar)end第一个生成的方法(foo)必须是公共(public)的,第二个(bar)必须是私有(private)的。我怎么做?或者,如何找到调用我的cust
我有一张背景图片,我想在其中添加一个文本框。我想弄清楚如何将标题放置在其顶部的正确位置。(我使用标题是因为我需要自动换行功能)。现在,我只能让文本显示在左上角,但我需要能够手动定位它的开始位置。require'RMagick'require'Pry'includeMagicktext="Loremipsumdolorsitamet"img=ImageList.new('template001.jpg')img 最佳答案 这是使用convert的ImageMagick命令行的答案。如果你想在Rmagick中使用这个方法,你必须自己移植
有没有办法在这个简单的get方法中添加超时选项?我正在使用法拉第3.3。Faraday.get(url)四处寻找,我只能先发起连接后应用超时选项,然后应用超时选项。或者有什么简单的方法?这就是我现在正在做的:conn=Faraday.newresponse=conn.getdo|req|req.urlurlreq.options.timeout=2#2secondsend 最佳答案 试试这个:conn=Faraday.newdo|conn|conn.options.timeout=20endresponse=conn.get(url
我有一个存储主机名的Ruby数组server_names。如果我打印出来,它看起来像这样:["hostname.abc.com","hostname2.abc.com","hostname3.abc.com"]相当标准。我想要做的是获取这些服务器的IP(可能将它们存储在另一个变量中)。看起来IPSocket类可以做到这一点,但我不确定如何使用IPSocket类遍历它。如果它只是尝试像这样打印出IP:server_names.eachdo|name|IPSocket::getaddress(name)pnameend它提示我没有提供服务器名称。这是语法问题还是我没有正确使用类?输出:ge
我想获取模块中定义的所有常量的值:moduleLettersA='apple'.freezeB='boy'.freezeendconstants给了我常量的名字:Letters.constants(false)#=>[:A,:B]如何获取它们的值的数组,即["apple","boy"]? 最佳答案 为了做到这一点,请使用mapLetters.constants(false).map&Letters.method(:const_get)这将返回["a","b"]第二种方式:Letters.constants(false).map{|c
我安装了ruby版本管理器,并将RVM安装的ruby实现设置为默认值,这样'哪个ruby'显示'~/.rvm/ruby-1.8.6-p383/bin/ruby'但是当我在emacs中打开inf-ruby缓冲区时,它使用安装在/usr/bin中的ruby。有没有办法让emacs像shell一样尊重ruby的路径?谢谢! 最佳答案 我创建了一个emacs扩展来将rvm集成到emacs中。如果您有兴趣,可以在这里获取:http://github.com/senny/rvm.el
如何在ruby中调用C#dll? 最佳答案 我能想到几种可能性:为您的DLL编写(或找人编写)一个COM包装器,如果它还没有,则使用Ruby的WIN32OLE库来调用它;看看RubyCLR,其中一位作者是JohnLam,他继续在Microsoft从事IronRuby方面的工作。(估计不会再维护了,可能不支持.Net2.0以上的版本);正如其他地方已经提到的,看看使用IronRuby,如果这是您的技术选择。有一个主题是here.请注意,最后一篇文章实际上来自JohnLam(看起来像是2009年3月),他似乎很自在地断言RubyCL
假设我有这个范围:("aaaaa".."zzzzz")如何在不事先/每次生成整个项目的情况下从范围中获取第N个项目? 最佳答案 一种快速简便的方法:("aaaaa".."zzzzz").first(42).last#==>"aaabp"如果出于某种原因你不得不一遍又一遍地这样做,或者如果你需要避免为前N个元素构建中间数组,你可以这样写:moduleEnumerabledefskip(n)returnto_enum:skip,nunlessblock_given?each_with_indexdo|item,index|yieldit
我正在尝试在Ruby中复制Convert.ToBase64String()行为。这是我的C#代码:varsha1=newSHA1CryptoServiceProvider();varpasswordBytes=Encoding.UTF8.GetBytes("password");varpasswordHash=sha1.ComputeHash(passwordBytes);returnConvert.ToBase64String(passwordHash);//returns"W6ph5Mm5Pz8GgiULbPgzG37mj9g="当我在Ruby中尝试同样的事情时,我得到了相同sha