Unix-like $WINDOWID Environment Variable #14586

Closed
opened 2026-01-31 04:14:11 +00:00 by claunia · 2 comments
Owner

Originally created by @ghost on GitHub (Jul 20, 2021).

Description of the new feature/enhancement

Many Unix-likes such as Linux and the *BSD's have a $WINDOWID environment variable set for the current terminal window such as Xorg's xterm and KDE's Konsole, which if added to MSYS2 would act as the window handle (HWND) casted to a void * and then to an uintptr_t or an intptr_t. A lot of software may make use of this, including the --wid flag of mpv Media Player or even some VBScript features such as the BrowseForFolder dialog which takes a LONG_PTR for the hWnd parameter. We can know that this is safe because even Microsoft does this with VBScript, which they implemented into Windows themselves.

Proposed technical implementation details

As a side note, I'd love to implement this myself in a pull request, I just need to know what variable you store the window handle in if any, and what line this should be added specifically, (I can write it in C instead of C++ if needed):
SetEnvironmentVariableA("WINDOWID", std::to_string((std::uintptr_t)(void *)hWnd).c_str());

Originally created by @ghost on GitHub (Jul 20, 2021). # Description of the new feature/enhancement Many Unix-likes such as Linux and the *BSD's have a $WINDOWID environment variable set for the current terminal window such as Xorg's xterm and KDE's Konsole, which if added to MSYS2 would act as the window handle (HWND) casted to a void * and then to an uintptr_t or an intptr_t. A lot of software may make use of this, including the --wid flag of mpv Media Player or even some VBScript features such as the BrowseForFolder dialog which takes a LONG_PTR for the hWnd parameter. We can know that this is safe because even Microsoft does this with VBScript, which they implemented into Windows themselves. # Proposed technical implementation details As a side note, I'd love to implement this myself in a pull request, I just need to know what variable you store the window handle in if any, and what line this should be added specifically, (I can write it in C instead of C++ if needed): `SetEnvironmentVariableA("WINDOWID", std::to_string((std::uintptr_t)(void *)hWnd).c_str());`
Author
Owner

@zadjii-msft commented on GitHub (Jul 20, 2021):

So, we've already had the HWND of the console exposed for years via GetConsoleWindow, and that has been widely regarded as a bad move. It precipitated a bunch of bad hacks in 3rd party software that was relying on it. MSYS for example used it to determine if they were running attached to a console or if they were writing to a file. Some people use it to try and manipulate the console window that a commandline is running in. However, these hacks are never going to work right in remote (read: ssh) sessions, and they don't work correctly in ConPTY (which helps power the Terminal). The value you're going to get out of GetConsoleWindow in ConPTY is intentionally a garbage HWND, to prevent people from building hacks on the value.

We're going to need some seriously good justification to add this in some other way.

There's also #2988 which is tracking "make GetConsoleWindow return the actual Terminal's HWND", which is probably what you want here. There's a lot more discussion in that thread.

@zadjii-msft commented on GitHub (Jul 20, 2021): So, we've already had the HWND of the console exposed for years via `GetConsoleWindow`, and that has been widely regarded as a bad move. It precipitated a bunch of bad hacks in 3rd party software that was relying on it. MSYS for example used it to determine if they were running attached to a console or if they were writing to a file. Some people use it to try and manipulate the console window that a commandline is running in. However, these hacks are never going to work right in remote (read: `ssh`) sessions, and they don't work correctly in ConPTY (which helps power the Terminal). The value you're going to get out of `GetConsoleWindow` in ConPTY is _intentionally_ a garbage HWND, to prevent people from building hacks on the value. We're going to need some seriously good justification to add this in some other way. There's also #2988 which is tracking "make `GetConsoleWindow` return the actual Terminal's HWND", which is probably what you want here. There's a lot more discussion in that thread.
Author
Owner

@ghost commented on GitHub (Jul 20, 2021):

Thank you for the quick response and explanation! I guess this can be closed now.

@ghost commented on GitHub (Jul 20, 2021): Thank you for the quick response and explanation! I guess this can be closed now.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/terminal#14586