如何通过托盘图标得到该所属进程

void CTrayDlg::OnButton1() { // TODO: Add your control notification handler code here HWND wd=::FindWindow("Shell_TrayWnd",NULL); if (wd==NULL) { MessageBox("Error1"); return; } HWND wtd=FindWindowEx(wd,NULL,"TrayNotifyWnd",NULL); if (wtd==NULL) { MessageBox("Error2"); return; } HWND wd1=FindWindowEx(wtd,NULL,"ToolbarWindow32",NULL); if (wd1==NULL) { MessageBox("Error3"); return; } DWORD pid; pid=0; GetWindowThreadProcessId(wd1,&pid); if (pid==NULL) { MessageBox("Error4"); return; } HANDLE hd=OpenProcess(PROCESS_QUERY_INFORMATION ¦ PROCESS_ALL_ACCESS ,true,pid); if (hd==NULL) { MessageBox("Error6"); return; } int num=::SendMessage(wd1,TB_BUTTONCOUNT ,NULL,NULL); int i; unsigned long n; TBBUTTON p,*pp; CString x; wchar_t name[256]; unsigned long whd,proid; CString temp; TBBUTTON *sp; sp= (TBBUTTON *)0x20f00; //这里应该改成用VirtualAllocEx分配内存否则有可能出错,不过人懒,就先这么着吧 for(i=0;i<num;i++) { ::SendMessage(wd1,TB_GETBUTTON,i,(LPARAM)sp); pp=&p; ReadProcessMemory(hd,sp,pp,sizeof(p),&n); // x.Format("%x %x %x %x %x %x",p.iBitmap,p.idCommand,p.fsState,p.fsStyle, p.dwData, p.iString); name[0]=0; if (p.iString!=0xffffffff) { try { ReadProcessMemory(hd,(void *)p.iString,name,255,&n); name[n]=0; } catch() { } // x+=" "; // x+=name; temp=name; try { whd=0; ReadProcessMemory(hd,(void *)p.dwData,&whd,4,&n); } catch() { } proid=0; GetWindowThreadProcessId((HWND)whd,&proid); x.Format("位置=%d 名称=%s 窗口句柄=%08x 进程ID=%08x", i,(LPCTSTR )temp,whd,proid); m_list.AddString(x); } } }

 

另一段收集

 

void CTrayDlg::OnButton1() { // TODO: Add your control notification handler code here HWND wd=::FindWindow("Shell_TrayWnd",NULL); if (wd==NULL) { MessageBox("Error1"); return; } HWND wtd=FindWindowEx(wd,NULL,"TrayNotifyWnd",NULL); if (wtd==NULL) { MessageBox("Error2"); return; } HWND wd1=FindWindowEx(wtd,NULL,"ToolbarWindow32",NULL); if (wd1==NULL) { MessageBox("Error3"); return; } DWORD pid; pid=0; GetWindowThreadProcessId(wd1,&pid); if (pid==NULL) { MessageBox("Error4"); return; } HANDLE hd=OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_ALL_ACCESS ,true,pid); if (hd==NULL) { MessageBox("Error6"); return; } int num=::SendMessage(wd1,TB_BUTTONCOUNT ,NULL,NULL); int i; unsigned long n; TBBUTTON p,*pp; CString x; wchar_t name[256]; unsigned long whd,proid; CString temp; TBBUTTON *sp; sp= (TBBUTTON *)0x20f00; for(i=0;i<num;i++) { ::SendMessage(wd1,TB_GETBUTTON,i,(LPARAM)sp); pp=&p; ReadProcessMemory(hd,sp,pp,sizeof(p),&n); // x.Format("%x %x %x %x %x %x",p.iBitmap,p.idCommand,p.fsState,p.fsStyle, p.dwData, p.iString); name[0]=0; if (p.iString!=0xffffffff) { try { ReadProcessMemory(hd,(void *)p.iString,name,255,&n); name[n]=0; } catch(...) { } // x+=" "; // x+=name; temp=name; try { whd=0; ReadProcessMemory(hd,(void *)p.dwData,&whd,4,&n); } catch(...) { } proid=0; GetWindowThreadProcessId((HWND)whd,&proid); x.Format("位置=%d 名称=%s 窗口句柄=%08x 进程ID=%08x", i,(LPCTSTR )temp,whd,proid); m_list.AddString(x); } } }

你可能感兴趣的:(如何通过托盘图标得到该所属进程)