How to get the Executable name of a window

前端 未结 5 2070
闹比i
闹比i 2020-12-30 02:19

I try to get the name of executable name of all of my launched windows and my problem is that:

I use the method

UINT GetWindowModuleFileName(      
H         


        
5条回答
  •  隐瞒了意图╮
    2020-12-30 02:46

    This is an example of how get the name of executable that creates window, hope it can give you some ideas about:

        while(true)
        {
        Sleep(250);//reduce cpu usage
        CHAR __name[MAX_PATH];//name buffer
        HWND hwnd;//window handle
        DWORD pid;//process pid
        hwnd=FindWindow(NULL,NULL);//find any window
        PROCESSENTRY32 entry;//process structure containing info about processes
        entry.dwSize=sizeof(PROCESSENTRY32);
        HANDLE snapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);//get processes
        if(hwnd!=0)
        {
            GetWindowThreadProcessId(hwnd,&pid);//get found window pid
        }
        if (Process32First(snapshot,&entry)==TRUE)//start listing processes
        {
            while (Process32Next(snapshot,&entry)==TRUE)
            {
                if (stricmp(entry.szExeFile,"explorer.exe")==0)
                {
                    if(pid!=entry.th32ProcessID)//if found window pid is explorers one, skip it
                    {
                        HANDLE hProcess=OpenProcess(PROCESS_ALL_ACCESS,FALSE,pid);//open processusing PROCESS_ALL_ACCESS to get handle
                        if(hProcess!=NULL)
                        {
                            GetModuleFileNameEx(hProcess,NULL,__name,MAX_PATH);//get executable path
                            cout<<"Found: "<<__name<

    To use GetModuleFileNameEx() you probably will need to set linker settings to link library psapi. Also include psapi.h.

提交回复
热议问题