Fenster(liste) oder HauptthreadId eines Prozesses ermitteln [gelöst]



  • Hallo,
    ich bin gerade dabei in einen fremden Prozess eine Dll zu injezieren (habe ich auch bereits geschafft, funktioniert soweit auch 🙂 ).
    Ziel der ganzen Aktion ist das Hauptfenster zu subclassen.Dazu muss ich allerdings irgendwie das HWND des Fensters herausfinden, weil SetWindowLongPtr() (was ich zum Ersetzen der WndProc benutze) das hwnd des Zielfensters als Parameter erfordert.

    Das Problem ist jetzt: wie komme ich aus einer Dll die ich zuvor (erfolgreich) in einen Prozess injeziert habe an das hwnd des Hauptfensters?

    mein Ansatz war GetCurrentThreadId() und dann EnumThreadWindws(); das hat sich allerding leider als untauglich herausgestellt weil ich zum injezieren einen RemoteThread benutze und damit die erhatene id nicht der des hauptthreads entspricht.

    Also bräuchte ich einen Weg wie ich entweder direkt an das HWND des Hauptfensters eines Prozesses rankomme oder alternativ einen Weg wie ich die ID des Mainthreads rankomme(das hwnd hole ich mir dann über EnumThreadWindows() ).

    Weiß jemand wie das funktioniert?

    danke schonmal,
    andi01.



  • ohne worte

    Alle windowHandler  finden:
    
    //---------------------------------------------------------------------------
    
    #pragma hdrstop
    #define STRICT 0x0001
    #include <windows.h>
    #include <stdio.h>
    #include <stdarg.h>
    #pragma comment(lib, "user32.lib")
    
    DWORD ProcessID;
    BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM lParam);
    
    BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM lParam)
    {
    int cTxtLen;
    PSTR pszMem;
    DWORD lpdwProcessId;
    LPTSTR  windowName;
    
    GetWindowThreadProcessId(hwnd,&lpdwProcessId);
    
    pszMem = (PSTR) VirtualAlloc((LPVOID) NULL,
                        (DWORD) (cTxtLen + 1), MEM_COMMIT,
                        PAGE_READWRITE);
    GetWindowText(hwnd, pszMem,cTxtLen + 1);
    printf("%s ",pszMem);
    
    return TRUE;
    }
    
    int main(int argc, char* argv[])
    {
    ProcessID=GetCurrentProcessId();
    EnumWindows(EnumThreadWndProc, 0);
        getc(stdin);
        return 0;
    }
    //---------------------------------------------------------------------------
    

    oder, findet alle fenster und die dazugehoerenden exe oder dlls als pfadangabe etc....

    // findwindow.cpp : Definiert den Einstiegspunkt für die Konsolenanwendung.
    //
    
    #include "stdafx.h"
    #include "stdio.h"
    #include "windows.h"
    #include "stdarg.h"
    #include "psapi.h"
    #pragma comment (lib,"psapi.lib");
    
    DWORD ProcessID;
    BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM lParam);
    
    BOOL CALLBACK EnumChildProc(      
        HWND Chwnd,
        LPARAM lParam
    	){
     TCHAR title[500];
     TCHAR wClass[250];
     ZeroMemory(wClass,sizeof(wClass));
        ZeroMemory(title, sizeof(title));
    
       GetWindowText(Chwnd, title, sizeof(title)/sizeof(title[0]));
       _tprintf(_T("       [ChildWindow TEXT: %s ] [CHWND: 0x%08X ] \n"), title,Chwnd);
       RealGetWindowClass(Chwnd,wClass,sizeof(wClass)/sizeof(TCHAR));
       _tprintf(_T("       [ChwndClass:   %s] \n\n"), wClass);
    return true;
    }
    
    BOOL CALLBACK EnumThreadWndProc(HWND hwnd, LPARAM lParam)
    {
    int cTxtLen;
    DWORD lpdwProcessId;
    LPTSTR  windowName;
    TCHAR wText=NULL;
    TCHAR ModuleName[200];
    TCHAR ProcessName[500];
    DWORD  size = 1024;
    
    GetWindowThreadProcessId(hwnd,&lpdwProcessId);
    cTxtLen = GetWindowTextLength(hwnd); 
     TCHAR title[500];
        ZeroMemory(title, sizeof(title));
     ZeroMemory(ModuleName,sizeof(ModuleName));
     ZeroMemory(ProcessName,sizeof(ProcessName));
      TCHAR wClass[250];
     ZeroMemory(wClass,sizeof(wClass));
    
    HANDLE hProcess = OpenProcess( PROCESS_QUERY_INFORMATION |
                                       PROCESS_VM_READ,
                                       FALSE, lpdwProcessId );
    QueryFullProcessImageName(hProcess,0,ProcessName,&size);
    GetModuleBaseName(hProcess ,NULL,ModuleName,sizeof(ModuleName)/sizeof(TCHAR));
        GetWindowText(hwnd, title, sizeof(title)/sizeof(title[0]));
    	printf("\n<NE> [PID: %d] [HWND: 0x%08X ] ",lpdwProcessId,hwnd);
    	_tprintf(_T("     [Window TEXT: %s ]\n"), title);
    	RealGetWindowClass(hwnd,wClass,sizeof(wClass)/sizeof(TCHAR));
        _tprintf(_T("     [ChwndClass %s] \n"), wClass);
    	_tprintf(_T("     [EXE:%s ]\n"),ModuleName); 
    	_tprintf(_T("     [EXE PAHT: %s ]\n\n"),ProcessName);
    EnumChildWindows(hwnd,EnumChildProc,0);
    return TRUE;
    }
    
    int _tmain(int argc, _TCHAR* argv[])
    {
    
    ProcessID=GetCurrentProcessId();
    EnumWindows(EnumThreadWndProc, 0);
        getc(stdin);
        return 0;
    }
    


  • wow, das ging ja mal schnell. sofort die perfekte Antwort 👍 genau danach hatte ich gesucht...

    danke,
    andi01.


Anmelden zum Antworten