Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8978569
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:38:45+00:00 2026-06-15T19:38:45+00:00

In my GUI app I run console app and need handle of its window.

  • 0

In my GUI app I run console app and need handle of its window. I tried with EnumWindows(), see code below, but it does not work. On list there is no my console app.

type
  TEnumWindowsData = record
    ProcessId: Cardinal;
    WinHandle: THandle;
    List: TStrings;                 // For test only
  end;
  PEnumWindowsData = ^TEnumWindowsData;

function FindWindow(hWnd: THandle; lParam: LPARAM): BOOL; stdcall;
var
  ParamData: TEnumWindowsData;
  ProcessId: Cardinal;
  WinTitle: array[0..200] of Char;  // For test only
begin
  ParamData := PEnumWindowsData(lParam)^;
  GetWindowThreadProcessId(hWnd, ProcessId);
  if ProcessId <> ParamData.ProcessId then
    Result := True
  else begin
    ParamData.WinHandle := hWnd;
    Result := False;
  end;
  // For test only
  GetWindowText(hWnd, WinTitle, Length(WinTitle) - 1);
  ParamData.List.Add(IntToStr(ProcessId) + ' ' + IntToStr(hWnd) + ' ' + WinTitle);
end;

procedure TForm1.Button1Click(Sender: TObject);

  function RunApp(const AProgram: string): Cardinal;
  var
    StartupInfo: TStartupInfo;
    ProcessInformation: TProcessInformation;
  begin
    Result := 0;
    ...
    if CreateProcess(nil, PChar(AProgram), nil, nil, False,
          NORMAL_PRIORITY_CLASS, nil, nil, StartupInfo, ProcessInformation)
    then
      Result := ProcessInformation.dwProcessId;
    ...
  end;

var
  ParamData: TEnumWindowsData;
begin
  ParamData.ProcessId := RunApp('cmd.exe /C D:\TMP\TEST.exe');
  ParamData.WinHandle := 0;
  ParamData.List := Memo1.Lines;
  EnumWindows(@FindWindow, THandle(@ParamData));

  FWindowHandle := ParamData.WinHandle;
end;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:38:46+00:00Added an answer on June 15, 2026 at 7:38 pm

    The following code simply creates the process (console application), attaches your process to the newly created console by AttachConsole function and from that attached console takes the window handle using the GetConsoleWindow function.

    The biggest weakness of the following code is that CreateProcess function returns immediately at the time, when the console is not yet fully initialized and when you try to attach the console immediately after, you will fail. Unfortunately, there’s no WaitForInputIdle function for console applications so as one possible workaround I would choose the attempt to attach the console in some limited loop count and once it succeed, get the handle and detach the console.

    In code that might look like follows. The RunApp function there should return the handle of the console window (assuming you’ll run only console applications from it), and should wait approx. 1 second for the console application you’ve started to be attachable. You can modify this value either by changing initial value of the Attempt variable and/or by changing Sleep interval.

    function GetConsoleWindow: HWND; stdcall;
      external kernel32 name 'GetConsoleWindow';
    function AttachConsole(dwProcessId: DWORD): BOOL; stdcall;
      external kernel32 name 'AttachConsole';
    
    function RunApp(const ACmdLine: string): HWND;
    var
      CmdLine: string;
      Attempt: Integer;
      StartupInfo: TStartupInfo;
      ProcessInfo: TProcessInformation;
    begin
      Result := 0;
      FillChar(StartupInfo, SizeOf(TStartupInfo), 0);
      FillChar(ProcessInfo, SizeOf(TProcessInformation), 0);
      StartupInfo.cb := SizeOf(TStartupInfo);
      StartupInfo.dwFlags := STARTF_USESHOWWINDOW;
      StartupInfo.wShowWindow := SW_SHOWNORMAL;
      CmdLine := ACmdLine;
      UniqueString(CmdLine);
      if CreateProcess(nil, PChar(CmdLine), nil, nil, False,
        CREATE_NEW_CONSOLE, nil, nil, StartupInfo, ProcessInfo) then
      begin
        Attempt := 100;
        while (Attempt > 0) do
        begin
          if AttachConsole(ProcessInfo.dwProcessId) then
          begin
            Result := GetConsoleWindow;
            FreeConsole;
            Break;
          end;
          Sleep(10);
          Dec(Attempt);
        end;
        CloseHandle(ProcessInfo.hThread);
        CloseHandle(ProcessInfo.hProcess);
      end;
    end;
    

    Then you can e.g. change the title of a console window of your lauched application this way:

    procedure TForm1.Button1Click(Sender: TObject);
    var
      S: string;
      ConsoleHandle: HWND;
    begin
      ConsoleHandle := RunApp('cmd.exe');
      if ConsoleHandle <> 0 then
      begin
        S := 'Hello! I''m your console, how can I serve ?';
        SendTextMessage(ConsoleHandle, WM_SETTEXT, 0, S);
      end;
    end;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I thought I'd try a simple GUI app using the world/universe mutation-free approach, but
I followed the steps on gSoap's page and tried to run the example code
From here I've copied an example of python gui app, but it's not working.
I have a console daemon that is run by a GUI application. When the
I have a C++ Win32 application that runs as a console app if run
I have a wxLua Gui app that has a Run button. Depending on selected
I am creating a GUI application(wxPython). I need to run another (.exe) application from
What I need to do is have a C# 2005 GUI app call a
the problem is simple I have GUI c#/xaml app and I want to run
I have a GUI app that is getting to be quite slow. I want

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.