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

  • Home
  • SEARCH
  • 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 3228122
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T16:39:18+00:00 2026-05-17T16:39:18+00:00

How do I get the console handle of an external application? I have a

  • 0

How do I get the console handle of an external application?

I have a program running as a console. I have a 2nd program that will call GetConsoleScreenBufferInfo, but for that I need the console handle of the first program. Is it possible that given the HWND of the 1st program I can get its console handle?

  • 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-05-17T16:39:18+00:00Added an answer on May 17, 2026 at 4:39 pm

    If you only have a HWND, call GetWindowThreadProcessId to obtain a PID from a given HWND. Afterwards, call AttachConsole to attach your calling process to the console of the given process, then call GetStdHandle to obtain a handle to STDOUT of your newly attached console. You can now call GetConsoleScreenBufferInfo using that handle.

    Remember to cleanup, freeing your handle to the console by calling FreeConsole.

    Edit: Here is some C++ code to go with that post

    #include <sstream>
    #include <windows.h>
    
    // ...
    // assuming hwnd contains the HWND to your target window    
    
    if (IsWindow(hwnd))
    {
        DWORD process_id = 0;
        GetWindowThreadProcessId(hwnd, &process_id);
        if (AttachConsole(process_id))
        {
            HANDLE hStdOut = GetStdHandle(STD_OUTPUT_HANDLE);
            if (hStdOut != NULL)
            {
                CONSOLE_SCREEN_BUFFER_INFO console_buffer_info = {0};
                if (GetConsoleScreenBufferInfo(hStdOut, &console_buffer_info))
                {
                    std::stringstream cursor_coordinates;
                    cursor_coordinates << console_buffer_info.dwCursorPosition.X << ", " << console_buffer_info.dwCursorPosition.Y;
                    MessageBox(HWND_DESKTOP, cursor_coordinates.str().c_str(), "Cursor Coordinates:", MB_OK);
                }
            }
            else
            {
                // error handling   
            }   
            FreeConsole();   
        }
        else
        {
            // error handling   
        }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple test application (C# console application) that does an HTTP GET
I'm trying to get console input in my Clojure program, but when it gives
I have a Delphi console application that at the end needs to launch one
I have a C++ Win32 application that runs as a console app if run
How do I handle big integers in C#? I have a function that will
I have a console-mode program that uses SQLite3 to maintain a database file. It
I have a console application that allows the users to specify variables to process.
I have a C sharp console application that captures a screenshot of a MS
Can someone tell me how to get the handle of a Windows console application
I have a multi-threaded console application acting as a server. The server spawns a

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.