type<br> PEnumInfo = ^TEnumInfo;<br> TEnumInfo = record<br> ProcessID : DWORD;<br> HWND : THandle;<br> end;<br><br>function EnumWindowsProc(Wnd: DWORD; var EI: TEnumInfo): Bool; stdcall;<br>var<br> PID : DWORD;<br>begin<br> GetWindowThreadProcessID(Wnd, @PID);<br> Result := (PID <> EI.ProcessID) or<br> (not IsWindowVisible(WND)) or<br> (not IsWindowEnabled(WND));<br><br> if not result then EI.HWND := WND;<br>end;<br><br>function FindMainWindow(PID: DWORD): DWORD;<br>var<br> EI : TEnumInfo;<br>begin<br> EI.ProcessID := PID;<br> EI.HWND := 0;<br> EnumWindows(@EnumWindowsProc, Integer(@EI));<br> Result := EI.HWND;<br>end;<br><br><br>procedure TForm1.Button1Click(Sender: TObject);<br>var<br> SI : TStartupInfo;<br> PI : TProcessInformation;<br> H : THandle;<br> S : String;<br>begin<br> ZeroMemory(@SI, SizeOf(SI));<br> ZeroMemory(@PI, SizeOf(PI));<br> SI.cb := SizeOf(SI);<br>if CreateProcess(nil,'CALC.EXE', nil, nil, FALSE, 0 ,nil,nil, SI, PI) then<br> begin<br> //注意!<br> WaitForInputIdle(PI.hProcess, INFINITE);<br><br> H := FindMainWindow(PI.dwProcessID);<br> if H > 0 then<br> begin<br> SetLength(S, 255);<br> GetWindowText(H, PChar(S), 255);<br> SetLength(S, StrLen(PChar(S)));<br> ShowMessage(S);<br> end;<br><br> CloseHandle(PI.hProcess);<br> CloseHandle(PI.hThread);<br> end;<br>end;<br>end.<br>