如何用C#获得指定进程的窗口句柄!
不用FindWindow,而是根据进程PID获得窗口句柄!
如何实现,代码更好,回答有分
(只能给100不够另开帖补)
不用FindWindow,而是根据进程PID获得窗口句柄!
如何实现,代码更好,回答有分
(只能给100不够另开帖补)
解决方案 »
- 需要用C#写一个Web通讯的客户端,给点建议~
- 我是初学者,有个难题请教各位?先谢谢了
- C#网络编程有没有Client类?
- 为什么64位Vista不支持WMI的一些命令(例如JoinDomainOrWorkgroup)?
- 在查询分析器中select a from t where haha in(:val)?
- 有关Datagrid的问题?
- e.Control
- 如何用session传一个datatable?
- DropDownList1.Items.FindByText 提示:未将对象引用设置到对象的实例
- 求教,如何从主窗体调用子窗体的控件或事件!子窗体调用父窗体的控件和事件
- winform形式的水晶报表为什么运行一段时间就报load report failed的错误,怎么解决啊?
- 如何调整控件在窗体上的上下层顺序?
给出的方法我试过了,部分情况下能获得句柄,有的就不能获得。(比如QQ),请问这个怎么解决呢。
【50分】给你和ojlovecd
[DllImport("kernel32.dll")]
public static extern IntPtr OpenProcess(UInt32 dwDesiredAccess,Int32 bInheritHandle,UInt32 dwProcessId) [DllImport("kernel32.dll")]
public static extern Int32 CloseHandle(IntPtr hObject) 这个OpenProcess函数的功能是获取一个已经存在的进程的句柄,
在C#中已经集成了对此函数的托管类System.Diagnostics.Process类
而且使用起来要比这个函数方便的多
比如获取本机上的所有进程:
System.Diagnostics.Process[] ps=System.Diagnostics.Process.GetProcesses();
此时ps是Process 类型的数组,即每个元素是一个Process类的对象
还有根据进程ID找到此进程:
System.Diagnostics.Process p=System.Diagnostics.Process.GetProcessById();
根据进程名称找到此进程:
System.Diagnostics.Process p=System.Diagnostics.Process.GetProcessesByName();
获取进程句柄:
System.IntPtr ProcHandle=p.Handle; 还有参考下,不知道对你有没有用!
http://hi.baidu.com/jerry_ma/blog/item/a6605966cd50a620ab184c83.html
你给出的东西,我先研究下,下午5点结贴
///////////////
namespace notepadinsp
{
class inspector
{
static void Main()
{
Process[] procs = Process.GetProcessesByName("bqq3.1.5");
if (procs.Length != 0)
{
IntPtr hwnd = procs[0].MainWindowHandle;
// do something with the handle
//...
MessageBox.Show(new WindowWrapper(hwnd), "Hello World!");
}
else
MessageBox.Show("bqq is not running.");
}
}
public class WindowWrapper : System.Windows.Forms.IWin32Window
{
public WindowWrapper(IntPtr handle)
{
_hwnd = handle;
}
public IntPtr Handle
{
get { return _hwnd; }
}
private IntPtr _hwnd;
}
}
public delegate bool EnumWindowsProcCallBack(int hwnd, 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")]
public static extern int EnumWindows(EnumWindowsProcCallBack proc, int lParam);
[DllImport("user32.dll")]
public static extern int GetWindowThreadProcessId (int hwnd, ref int lpdwProcessId);
public void test()
{
EnumWindowsProcCallBack EnumWindowsProc = new EnumWindowsProcCallBack(myEnumWindowsProc);
EnumWindows(EnumWindowsProc, 0);
}
//call back function
public static bool myEnumWindowsProc(int hwnd, int lParam)
{
//add you code here
if(IsWindowVisible(hWnd))
{
int processId;
GetWindowThreadProcessId(hwnd, processId);//取进程ID,跟你的PID比较。
if(processId == yourProcessID)
{
return false; //停止枚举
}
//StringBuilder title = new StringBuilder(255);
//GetWindowText(hWnd, title, 255);
//Console.Write(title);
}
return true;
}