为什么得到的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;请师姐师哥指教;