hWnd eines selbst gestarteten Prozesses bekommen
-
Hallo,
ich möchte eine exe-Datei starten, die ein Fenster erstellt.
Mit welchen Funktionen muss ich die Datei starten, um das hWnd zu bekommen?
MfG,
Max
-
Etwas missverständlich aus gedrückt. Startest du die *.exe per Doppelclick oder per eintippen oder ist es so gemeint, daß du ein eigense Programm gecodet hast, das dir einen andere *.exe startet?
-
#include <windows.h> BOOL __stdcall FindThreadWindowCb(HWND hwnd, HWND *pHwnd) { DWORD style = GetWindowLong(hwnd, GWL_STYLE); if ((style & WS_OVERLAPPEDWINDOW) == WS_OVERLAPPEDWINDOW) { *pHwnd = hwnd; } return !*pHwnd; } int APIENTRY _tWinMain(HINSTANCE,HINSTANCE,LPTSTR,int) { PROCESS_INFORMATION pi; STARTUPINFO si = {0}; si.cb = sizeof(si); TCHAR cmd[] = TEXT("notepad.exe"); // für UNICODE bug if (CreateProcess(0,cmd,0,0,0,0,0,0,&si,&pi)) { DWORD dwStartTime = GetTickCount(); WaitForInputIdle(pi.hProcess, 2000); HWND hwnd = 0; while (!hwnd) { DWORD dwTime = GetTickCount() - dwStartTime; if (dwTime > 10000) break; // 10s timeout EnumThreadWindows(pi.dwThreadId, (WNDENUMPROC)FindThreadWindowCb, (LPARAM)&hwnd); if (!hwnd) Sleep(100); } if (hwnd) { SendMessage(hwnd, WM_SETTEXT, 0, (LPARAM)TEXT("************")); HWND hwndEdit = GetWindow(hwnd, GW_CHILD); SendMessage(hwndEdit, WM_SETTEXT, 0, (LPARAM)TEXT("hello world")); SendMessage(hwndEdit, EM_SETMODIFY, 1, 0); PostMessage(hwnd, WM_CLOSE, 0, 0); } CloseHandle(pi.hProcess); CloseHandle(pi.hThread); } }
-
Genau das was sapero gepostet hat, habe ich gemeint!
Es funktioniert auch super, allerdings nicht bei Konsolenanwendungen.
Bei Konsolenanwendungen wird nicht einmal FindThreadWindowCb aufgerufen..
Wie kann ich trotzdem das Fensterhandle bekommen?
-
Naja, konsole Fenster gehört zu csrss.exe.
if (!AttachConsole(pi.dwProcessId) && (GetLastError() == ERROR_ACCESS_DENIED)) { // If the calling process is already attached to a console[...] FreeConsole(); AttachConsole(pi.dwProcessId); } hwnd = GetConsoleWindow(); FreeConsole();
-
Danke!
