注册 登录
  • 欢迎访问开心洋葱网站,在线教程,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站,欢迎加入开心洋葱 QQ群
  • 欢迎访问开心洋葱网站,手机也能访问哦~欢迎加入开心洋葱多维思维学习平台 QQ群
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏开心洋葱吧~~~~~~~~~~~~~!

VC遍历全部窗口

OC/C/C++ 开心洋葱 2954次浏览 已收录 0个评论 手机上查看

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;
}

 


开心洋葱 , 版权所有丨如未注明 , 均为原创丨未经授权请勿修改 , 转载请注明VC遍历全部窗口
喜欢 (0)
[开心洋葱]
分享 (0)
关于作者:
开心洋葱,开心洋葱头,水墨

您必须 登录 才能发表评论!

加载中……