VC遍历全部窗口
#include "stdafx.h"
#include <windows.h>
#include <iostream>
//#include <shellapi.h>
#include <psapi.h>
//#include <HIDLL/HIDll.h>
#include <tlhelp32.h>
#include <string.h>
using namespace std;
BOOL CALLBACK EnumProc(HWND,LPARAM);
unsigned long GetPidByName(LPCSTR pname);
DWORD PID=0;
char* wname=NULL;
DWORD* LPID=NULL;
UINT NUMPID=0;
string propmt;
//help function
void ShowHelp(void);
int _tmain(int argc, _TCHAR* argv[])
{
propmt="S>:";
while(true){
cout<<propmt.c_str();
char buf[100];memset(buf,0,100);
gets_s(buf,100);
if( strcmp(buf,"run")==0 ){ ::EnumWindows(EnumProc,NULL);}
string str=buf;
if( str.compare("bye")==0 ){
break;
}
if( str.compare("set propmt")== 0){
cout<<"Input new prompt:";
memset(buf,0,100);
gets_s(buf,100);
propmt=buf; continue;
}
if( str.compare("?")==0 || str.compare("help")==0 ){
ShowHelp();continue;
}
if( str.compare("set pid")==0 ){
cout<<"PID:";
memset(buf,0,100);
gets_s(buf,100);
PID=atoi(buf);
if(0!=PID){
cout<<"Already set "<<" PID:"<<PID<<endl;
}
continue;
}
if( str.length()<=3 ){continue;}
if( str.substr(0,3).compare("set")==0 ){
NUMPID=0;
if( 0!=LPID ){ delete []LPID;LPID=0;NUMPID=0;}
PID=GetPidByName(str.substr(4).c_str());
if(0!=PID){
cout<<"Already set "<<((LPCSTR)(str.substr(4).c_str()))<<" PID:"<<PID<<endl;
}
/*while(PID){
NUMPID++;
cout<<"Already set "<<((LPCSTR)(str.substr(4).c_str()))<<" PID:"<<PID<<endl;break;
PID=GetPidByName(str.substr(4).c_str());
}*/
/*if( 0!=NUMPID ){ LPID=new DWORD[NUMPID] ;}
UINT numpid=NUMPID;
PID=GetPidByName(str.substr(4).c_str());
UINT TEMPPID=0;
while(PID!=TEMPPID){
LPID[--numpid]=PID;
cout<<"Already set "<<((LPCSTR)(str.substr(4).c_str()))<<" PID:"<<PID<<endl;
TEMPPID=PID;
PID=GetPidByName(str.substr(4).c_str());
}*/
}
Sleep(1000);
}
if( NULL!=LPID ){ delete [] LPID;}
return 0;
}
BOOL CALLBACK EnumProc(HWND hWnd,LPARAM){
if( NULL==hWnd ){ return FALSE;}
//for(UINT i=0;i<NUMPID;i++){
if( ::IsWindow(hWnd) ){
DWORD pid=0;
::GetWindowThreadProcessId(hWnd,&pid);
if( pid==PID ){
BOOL isS=::IsWindowVisible(hWnd);
isS=!isS;
ShowWindow(hWnd,isS);
char wt[255];memset(wt,0,255);
::GetWindowTextA(hWnd,wt,255);
cout<<"["<<wt<<"] | {"<<hWnd<< "} :" <<((!isS)?"hide":"show")<<endl;
return TRUE;
}
}
//}
return TRUE;
}
unsigned long GetPidByName(LPCSTR pname){
unsigned long pid=0;
string name = pname;
//char* dname = NULL;
name=::CharLowerA( (char*)name.c_str() );
PROCESSENTRY32 lp;
HANDLE lh=NULL;
memset(&lp,0,sizeof(lp));
lp.dwSize = sizeof(lp);
lh = ::CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, 0 );
BOOL ok = ::Process32First( lh,&lp);
while(ok){
char* dname = new char[(int)((wcslen(lp.szExeFile)+1)*sizeof(wchar_t))];
//wchar_t dname[] =lp.szExeFile;
::WideCharToMultiByte(CP_ACP,0,lp.szExeFile,-1,dname,static_cast<int>(wcslen(lp.szExeFile)+1),NULL,NULL);
dname = ::CharLowerA(dname);
if ( 0==name.compare(dname) ){
pid = lp.th32ProcessID;
delete [] dname;dname=NULL;
break;
}
delete [] dname;dname=NULL;
ok = ::Process32NextW(lh,&lp);
}
return pid;
}
void ShowHelp(){
cout<<"set [process name];"<<endl
<<"set prompt;"<<endl
<<"set [pid];"<<endl
<<"run;"<<endl;
}
|
