这是一段vb的代码,据说是可以取QQ的句柄 CreateToolhelp32Snapshot API 和 PROCESSENTRY32 的定义,百度一下Dim uSnapShot As Long '系统快照返回值 Dim uResult As Long '遍历进程返回值 Dim uProcess As PROCESSENTRY32 '定义进程结构变量 Dim meHandle As Long '进程句柄
If uSnapShot Then uResult = Process32First(uSnapShot, uProcess) '取得第一个进程 Do While uResult
If InStr(Left(uProcess.szexeFile, InStr(uProcess.szexeFile, Chr(0)) - 1), "QQ.exe") > 0 Then meHandle = OpenProcess(PROCESS_ALL_ACCESS, True, uProcess.th32ProcessID) 'meHandle中就是你想要的QQ的句柄 End If uResult = Process32Next(uSnapShot, uProcess) '取得快照中的下一个进程 Loop End If
public class User32API
{
private static Hashtable processWnd = null; public delegate bool WNDENUMPROC(IntPtr hwnd, uint lParam); static User32API()
{
if (processWnd == null)
{
processWnd = new Hashtable();
}
} [DllImport("user32.dll", EntryPoint = "EnumWindows", SetLastError = true)]
public static extern bool EnumWindows(WNDENUMPROC lpEnumFunc, uint lParam); [DllImport("user32.dll", EntryPoint = "GetParent", SetLastError = true)]
public static extern IntPtr GetParent(IntPtr hWnd); [DllImport("user32.dll", EntryPoint = "GetWindowThreadProcessId")]
public static extern uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId); [DllImport("user32.dll", EntryPoint = "IsWindow")]
public static extern bool IsWindow(IntPtr hWnd); [DllImport("kernel32.dll", EntryPoint = "SetLastError")]
public static extern void SetLastError(uint dwErrCode); public static IntPtr GetCurrentWindowHandle()
{
IntPtr ptrWnd = IntPtr.Zero;
uint uiPid = (uint)Process.GetCurrentProcess().Id; // 当前进程 ID
object objWnd = processWnd[uiPid]; if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
if (ptrWnd != IntPtr.Zero && IsWindow(ptrWnd)) // 从缓存中获取句柄
{
return ptrWnd;
}
else
{
ptrWnd = IntPtr.Zero;
}
} bool bResult = EnumWindows(new WNDENUMPROC(EnumWindowsProc), uiPid);
// 枚举窗口返回 false 并且没有错误号时表明获取成功
if (!bResult && Marshal.GetLastWin32Error() == 0)
{
objWnd = processWnd[uiPid];
if (objWnd != null)
{
ptrWnd = (IntPtr)objWnd;
}
} return ptrWnd;
} private static bool EnumWindowsProc(IntPtr hwnd, uint lParam)
{
uint uiPid = 0; if (GetParent(hwnd) == IntPtr.Zero)
{
GetWindowThreadProcessId(hwnd, ref uiPid);
if (uiPid == lParam) // 找到进程对应的主窗口句柄
{
processWnd[uiPid] = hwnd; // 把句柄缓存起来
SetLastError(0); // 设置无错误
return false; // 返回 false 以终止枚举窗口
}
} return true;
}
}
上面的代码跟process.MainWindowHandle结果差不多,只不过在托盘里process.MainWindowHandle为0了,上面代码还是返回什么IME什么的
CreateToolhelp32Snapshot API
和
PROCESSENTRY32 的定义,百度一下Dim uSnapShot As Long '系统快照返回值
Dim uResult As Long '遍历进程返回值
Dim uProcess As PROCESSENTRY32 '定义进程结构变量
Dim meHandle As Long '进程句柄
uSnapShot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0&) '建立系统快照
uProcess.dwSize = Len(uProcess) '初始化进程信息长度
If uSnapShot Then
uResult = Process32First(uSnapShot, uProcess) '取得第一个进程
Do While uResult
If InStr(Left(uProcess.szexeFile, InStr(uProcess.szexeFile, Chr(0)) - 1), "QQ.exe") > 0 Then
meHandle = OpenProcess(PROCESS_ALL_ACCESS, True, uProcess.th32ProcessID)
'meHandle中就是你想要的QQ的句柄
End If
uResult = Process32Next(uSnapShot, uProcess) '取得快照中的下一个进程
Loop
End If
//if (!::EnumWindows((WNDENUMPROC)enumProc, 0)) // AfxMessageBox("Error"); BOOL CALLBACK enumProc(HWND hwnd, LPARAM lParam) { if (hwnd == NULL) return FALSE; if (::IsWindow(hwnd) )//&& ::IsWindowVisible(hwnd)) { TCHAR szCap[255] = {0}; ::GetWindowText(hwnd, szCap, 255); if (strlen(szCap) == 0) return TRUE; CString str ; str.Format(_T("%s"),szCap); if(str.Find(_T("ubbles")) != -1) ShowWindow(hwnd,SW_SHOWNORMAL); } return TRUE; }据说这个可以,可是怎么用C#写出来呢?