NET环境中,用C#获得某个Process对象的主窗口句柄的方法是使用Process的属性:Process.MainWindowHandle:
//C#编程获取进程的主窗口代码
using System;
using System.Text;
using System.Diagnostics;
using System.Runtime.InteropServices;
class MyApp {
......
// 迭代所有进程
// Process.GetProcesses 返回进程数组
//
Int32 count=0;
Process[] procs = Process.GetProcesses();
bool bFirstModule=true;
for (int i=0, len=procs.GetLength(0); i<len; i++) {
Process p = procs[i];
if (p.Id!=0) {
int hwnd = p.MainWindowHandle.ToInt32();
if (hwnd!=0) { // 如果有一个主窗口:
ProcessModule pm = p.MainModule;