为什么得到的pid是0,EnumWindows(ewp, 0);返回总是1;

北京大白兔 2013-10-10 02:03:23
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

namespace zhuchuangkou
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
[DllImport("user32.dll")]

//EnumWindows函数,EnumWindowsProc 为处理函数

private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);

//其他常用函数格式如下:
[DllImport("user32.dll")]
private static extern int GetWindowText(int hWnd, StringBuilder title, int size);
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(int hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowTextLength(int hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr FindWindow(string lpClassName,string lpWindowName);
[DllImport("USER32.DLL")]
private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int GetProcessIdOfThread(IntPtr ThreadHandle);
[DllImport("user32", EntryPoint = "GetWindowThreadProcessId")]
private static extern int GetWindowThreadProcessId(IntPtr HW, out int pid);

// 申明委托
public delegate bool EnumWindowsProc(int hWnd, int lParam);

// 实例委托
void GetWindow()
{
Form1 frm = new Form1();
EnumWindowsProc ewp = new EnumWindowsProc(frm.ADA_EnumWindowsProc);
EnumWindows(ewp, 0);
label1.Text = EnumWindows(ewp,0).ToString();
label2.Text = pid.ToString();
}

// 实现委托函数
int pid;

public bool ADA_EnumWindowsProc(int hWnd, int lParam)
{

//if (GetProcessIdOfThread((IntPtr)hWnd) == 4148)
IntPtr hWnd1 = new IntPtr(hWnd);
GetWindowThreadProcessId(hWnd1, out pid);
if(pid==4148) // 4148为进程ID
return false;
else
return true;
}

private void button1_Click(object sender, EventArgs e)
{
GetWindow();
}

}
}


这段代码的目的是想得到指定进程ID的主窗口句柄,
为什么得到的pid是0,EnumWindows(ewp, 0);返回总是1;请师姐师哥指教;



...全文
218 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
北京大白兔 2013-10-11
  • 打赏
  • 举报
回复
还没搞定,还是要谢谢浮云也疯狂的热心,我再研究下
  • 打赏
  • 举报
回复
SetProcessWindowStation(hWinStation))执行成功是前提,你可以查一下它的作用
  • 打赏
  • 举报
回复
public static void EnumDesktops() { IntPtr hndWinStation = IntPtr.Zero; try { //hWinStation = OpenWindowStation(WINDOW_STATION_CONSOLE, false, GENERIC_ALL); hndWinStation = GetDesktopWindow(); //获取当前活动用户的桌面句柄 if (hndWinStation == IntPtr.Zero) { Console.WriteLine("GetDesktopWindow failed!"); } else if(SetProcessWindowStation(hWinStation)) { //枚举窗口 WndEnumCallBack wndEnumProc = (hWnd, arg) => { if (IsWindowVisible(hWnd)) { var title = GetWindowTitle(hWnd); if (title.Length > 0) { //((List<string>)obj).Add(title); Console.WriteLine("句柄:{0}\t窗口:{1}", hWnd.ToString(), title); } } return true; //返回true保证枚举完所有窗口 }; //枚举桌面 var desktops = new List<string>(); DesktopEnumCallBack desktopEnumProc = (desktop, obj) => { Console.WriteLine("\r\nDesktop: " + desktop); desktops.Add(desktop); EnumWindows(wndEnumProc, IntPtr.Zero); return true; }; EnumDesktops(hndWinStation, desktopEnumProc, IntPtr.Zero); //枚举线程窗口 //int windowPId; //int threadId = GetWindowThreadProcessId(hndWinStation, out windowPId); //trace.Write("PID: {0}\tThreadID: {1}", windowPId, threadId); //枚举子窗口 EnumChildWindows(hndWinStation, wndEnumProc, IntPtr.Zero); } } finally { if (hndWinStation != IntPtr.Zero) CloseWindowStation(hndWinStation); } } 几个主要函数已经标红
  • 打赏
  • 举报
回复
引用 2 楼 u011050700 的回复:
为什么直接调用下面的代码,hw句柄是0 IntPtr hw= System.Diagnostics.Process.GetProcessById(7828).MainWindowHandle; label3.Text = hw.ToString();
你确定那个进程有窗口吗?用spy++对照一下 有一种可能:当有多个用户同时登陆系统后,在A用户中是取不到B用户进程的MainWindowHandle的,使用EnumWindows也只能枚举到A用户桌面下的窗口,这是Session隔离导致的 try..
北京大白兔 2013-10-10
  • 打赏
  • 举报
回复
为什么直接调用下面的代码,hw句柄是0 IntPtr hw= System.Diagnostics.Process.GetProcessById(7828).MainWindowHandle; label3.Text = hw.ToString();
  • 打赏
  • 举报
回复
EnumWindows(ewp, 0)你调用了两遍 EnumWindowsProc返回0(false)就会终止枚举,返回1是为了保证枚举完所有的窗口 单用if(pid==4148)来判断主窗口是够的,你还得判断IsWindowVisible,最主要的GetParent应该返回0 要获得主窗口句柄只需要这样 System.Diagnostics.Process.GetProcessById(4148).MainWindowHandle

110,571

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧