C#尝试枚举每个进程线程的每个窗口
我试图制作一个小应用程序,在控制台中枚举每个进程线程中的每个窗口名 我目前使用的代码在Windows7机器上运行良好,但由于某些原因,它在Windows10上运行时没有任何堆栈跟踪或错误消息,我尝试在try catch中添加几乎所有内容,但我也没有得到任何消息,所以我迷失了方向 代码如下:C#尝试枚举每个进程线程的每个窗口,c#,C#,我试图制作一个小应用程序,在控制台中枚举每个进程线程中的每个窗口名 我目前使用的代码在Windows7机器上运行良好,但由于某些原因,它在Windows10上运行时没有任何堆栈跟踪或错误消息,我尝试在try catch中添加几乎所有内容,但我也没有得到任何消息,所以我迷失了方向 代码如下: using System; using System.Collections.Generic; using System.Diagnostics; using System.R
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
private delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
private static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
static void Main(string[] args)
{
Process[] processes = Process.GetProcesses();
Console.WriteLine("Detected: " + processes.Length + " processes.");
foreach (Process process in processes)
{
IEnumerable<IntPtr> windowHandles = null;
try
{
windowHandles = EnumerateProcessWindowHandles(process);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
Console.WriteLine("Checking process" + process.ProcessName);
foreach (var handle in windowHandles)
{
StringBuilder message = new StringBuilder();
try
{
SendMessage(handle, 0x000D, message.Capacity, message);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
if (message.Length == 0)
{
continue;
}
Console.WriteLine("Window name: " + message.ToString());
}
}
Console.WriteLine("Finished!");
Console.ReadLine();
}
private static IEnumerable<IntPtr> EnumerateProcessWindowHandles(Process process)
{
List<IntPtr> handles = new List<IntPtr>();
ProcessThreadCollection threads = null;
try
{
threads = process.Threads;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
return handles;
}
foreach (ProcessThread thread in threads)
{
try
{
EnumThreadWindows(thread.Id, (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
}
return handles;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.Runtime.InteropServices;
使用系统文本;
命名空间控制台应用程序3
{
班级计划
{
私有委托bool EnumThreadDelegate(IntPtr hWnd、IntPtr lParam);
[DllImport(“user32.dll”)]
私有静态外部bool EnumThreadWindows(int-dwThreadId、EnumThreadDelegate-lpfn、IntPtr-lParam);
[DllImport(“user32.dll”,CharSet=CharSet.Auto)]
私有静态外部IntPtr SendMessage(IntPtr hWnd、uint Msg、int wParam、StringBuilder LPRAM);
静态void Main(字符串[]参数)
{
Process[]processs=Process.getprocesss();
Console.WriteLine(“检测到:“+进程.长度+”进程”);
foreach(过程中的过程)
{
IEnumerable windowHandles=null;
尝试
{
windowHandles=枚举进程windowHandles(进程);
}
捕获(例外e)
{
控制台写入线(如StackTrace);
继续;
}
Console.WriteLine(“检查进程”+process.ProcessName);
foreach(windowHandles中的变量句柄)
{
StringBuilder消息=新建StringBuilder();
尝试
{
SendMessage(句柄,0x000D,message.Capacity,message);
}
捕获(例外e)
{
控制台写入线(如StackTrace);
继续;
}
if(message.Length==0)
{
继续;
}
Console.WriteLine(“窗口名称:+message.ToString());
}
}
控制台。WriteLine(“完成!”);
Console.ReadLine();
}
私有静态IEnumerable EnumerateProcessWindowHandles(进程)
{
列表句柄=新列表();
ProcessThreadCollection线程=null;
尝试
{
线程=进程线程;
}
捕获(例外e)
{
控制台写入线(如StackTrace);
返回手柄;
}
foreach(ProcessThread线程中的线程)
{
尝试
{
EnumThreadWindows(thread.Id,(hWnd,lParam)=>{handles.Add(hWnd);返回true;},IntPtr.Zero);
}
捕获(例外e)
{
控制台写入线(如StackTrace);
继续;
}
}
返回手柄;
}
}
}
知道它为什么会卡住吗
更新
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Text;
namespace ConsoleApplication3
{
class Program
{
private delegate bool EnumThreadDelegate(IntPtr hWnd, IntPtr lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool EnumThreadWindows(int dwThreadId, EnumThreadDelegate lpfn, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto)]
private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, int wParam, StringBuilder lParam);
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool IsWindowVisible(IntPtr handle);
static void Main(string[] args)
{
Process[] processes = Process.GetProcesses();
Console.WriteLine("Detected: " + processes.Length + " processes.");
foreach (Process process in processes)
{
IEnumerable<IntPtr> windowHandles = null;
try
{
windowHandles = EnumerateProcessWindowHandles(process);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
Console.WriteLine("Checking process: " + process.ProcessName);
foreach (IntPtr handle in windowHandles)
{
if (handle == null)
{
continue;
}
if (!IsWindowVisible(handle))
{
continue;
}
StringBuilder message = new StringBuilder();
int capacity = 0;
try
{
capacity = message.Capacity;
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
try
{
SendMessage(handle, 0x000D, capacity, message);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
if (message.Length == 0)
{
continue;
}
Console.WriteLine("Window name: " + message.ToString());
}
}
Console.WriteLine("Finished!");
Console.ReadLine();
}
private static IEnumerable<IntPtr> EnumerateProcessWindowHandles(Process process)
{
List<IntPtr> handles = new List<IntPtr>();
ProcessThreadCollection threads = null;
try
{
threads = process.Threads;
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
return handles;
}
foreach (ProcessThread thread in threads)
{
if (thread == null)
{
continue;
}
int threadId = 0;
try
{
threadId = thread.Id;
}
catch(Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
try
{
EnumThreadWindows(threadId, (hWnd, lParam) => { handles.Add(hWnd); return true; }, IntPtr.Zero);
}
catch (Exception e)
{
Console.WriteLine(e.StackTrace);
continue;
}
}
return handles;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统诊断;
使用System.Runtime.InteropServices;
使用系统文本;
命名空间控制台应用程序3
{
班级计划
{
私有委托bool EnumThreadDelegate(IntPtr hWnd、IntPtr lParam);
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
私有静态外部bool EnumThreadWindows(int-dwThreadId、EnumThreadDelegate-lpfn、IntPtr-lParam);
[DllImport(“user32.dll”,CharSet=CharSet.Auto)]
私有静态外部IntPtr SendMessage(IntPtr hWnd、uint Msg、int wParam、StringBuilder LPRAM);
[DllImport(“user32.dll”)]
[返回:Marshallas(UnmanagedType.Bool)]
私有静态外部bool IsWindowVisible(IntPtr句柄);
静态void Main(字符串[]参数)
{
Process[]processs=Process.getprocesss();
Console.WriteLine(“检测到:“+进程.长度+”进程”);
foreach(过程中的过程)
{
IEnumerable windowHandles=null;
尝试
{
windowHandles=枚举进程windowHandles(进程);
}
捕获(例外e)
{
控制台写入线(如StackTrace);
继续;
}
Console.WriteLine(“检查进程:+process.ProcessName”);
foreach(windowHandles中的IntPtr句柄)
{
if(handle==null)
{
继续;
}
如果(!IsWindowVisible(句柄))
{
继续;
}
StringBuilder消息=新建StringBuilder();
int容量=0;
尝试
{
容量=消息容量;
}
捕获(例外)