Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

Detect Windows Terminal

Aug
1,916
68
Code:
     _x64: 1
   _admin: 1
_elevated: 1

TCC  26.02.42 x64   Windows 10 [Version 10.0.18363.959]

1597886077939.png


Here's my method of determining if I am running TCC.EXE from Windows Terminal;
Code:
function WinTerm=`%@execstr[echo %@pidcommand[%_ppid] |! ffind /t"WindowsTerminal" > nul & echo %_ffind_matches]`

1597886026253.png


Joe

Edit: Corrected function, which should have been wrapped in @execstr
Edit: Used _ppid per suggestion by Vince
 
Last edited:
Code:
function WinTerm=`echo %@pidcommand[%@ppid[%_pid]] |! ffind /t"WindowsTerminal" > nul & echo %_ffind_matches`
That's a funny-looking UDF; it looks more like an alias.

It doesn't work here because I launch it as WT.EXE.

_PPID should be the same as %@ppid[%_pid]

@REGEX is made for the job. Here's one.

1597938632085.png
 
Joe, in your terminal pic, it looks like there are icons in the tab. Do they do anything and how did they get there?

1597947071840.png
 
Joe, in your terminal pic, it looks like there are icons in the tab. Do they do anything and how did they get there?

View attachment 3034

Right-click the tab, and choose rename tab;
1597952820361.png


Adjust the cursor to desired location, then press WIN+.
Select the icon(s) that you want displayed in the tab title.

1597952943278.png


The TITLE command in CMD EXE can also be used to programmatically add icons to the name of a tab;

1597953667779.png


So, I now have a warning icon included with the tab name.

1597953693287.png


I could not get this to work with the TCC Title command. As Rex has said he will not be supporting Windows Terminal, I did not bother to pursue getting this to work any further.

For PowerShell, the same thing can be accomplished as follows;

1597953925348.png


I was going to provide a screen capture of the "PowerShell 64-bit ⚠" tab, but apparently the forum only allows a maximum of 5 attachements.

Joe
 
_PPID should be the same as %@ppid[%_pid]

Thanks for that @vefatica

If the help had more "See Also" links, I would have seen that.

Revised function to;
Code:
WinTerm=`%@execstr[echo %@pidcommand[%_ppid] |! ffind /t"WindowsTerminal" > nul & echo %_ffind_matches]`

Joe

Edit: Corrected function, which should have been wrapped in @execstr
 
Last edited:
Thanks, Joe. After spending quite a while figuring out how to dig the icon out of TCC.EXE I managed this.

1597954941856.png


by doing this in settings.json.

Code:
"icon": "d:\\tc26\\icon1.ico"

I wanted to try it your way but NOTHING happens when I right-click on a tab. Is that a newer feature ... or does it have to be enabled?
 
I wanted to try it your way but NOTHING happens when I right-click on a tab. Is that a newer feature ... or does it have to be enabled?

I'm using the Windows Store version of Windows Terminal, but if I remember correctly, you are using the non-store version (possibly from GitHub).

Renaming the tab is a default option, AFAIK, and does not need to be enabled (at least in the Windows Store version).

I update the Windows Store version of Windows Terminal via the Windows Store, but your version will probably have to be downloaded again, or refreshed via GitHub.

Here's the link to Microsoft Documentation in regards to renaming a tab, along with a link to the Tab Title Tutorial.

Joe
 
I updated from GitHub (rather easily). Now I have the right-click pop-up menu on tabs.

Does the store version update itself automatically, and let you know when that happens?
 
Does the store version update itself automatically, and let you know when that happens?

I usually just go to the Windows Store, click "Downloads and Updates", and then I either Update All, or just the available updates that I want.

1597962659798.png


Joe
 
There are these in the environment, but they don't provide a foolproof way of determining whether TCC is running in Terminal.

WSLENV=WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID={ecfa30ac-7c15-41c5-9f0f-886fff57c6ba}
WT_SESSION=e0a34120-48fd-425f-a319-ae27250bde53
 
Yet another way: get the console's HWND and send it WM_GETICON (127). conhost will return a valid HANDLE; openconsole will return 0.

Code:
set _wt=`%@if[%@winapi[user32,SendMessage,%@winapi[kernel32,GetConsoleWindow],127,0,0]==0,1,0]`

1598373365422.png
 
There are these in the environment, but they don't provide a foolproof way of determining whether TCC is running in Terminal.

WSLENV=WT_SESSION::WT_PROFILE_ID
WT_PROFILE_ID={ecfa30ac-7c15-41c5-9f0f-886fff57c6ba}
WT_SESSION=e0a34120-48fd-425f-a319-ae27250bde53

Actually, WT_PROFILE_ID works great in both TCC and PowerShell;
1598379789289.png

Note that the WT_PROFILE_ID matches the GUID of the Profile in settings.json

From TCC in Windows Terminal, the PSHELL command shows;
Code:
e:\utils>pshell /s "$env:wt_profile_id"
{35f8afbd-df78-4f1b-a658-451210ef5caa}
which shows that I am in a TCC PSHELL session under Windows Terminal.

From PowerShell in Windows Terminal, if I run TCC or TCCRT;
Code:
PS E:\Utils> tcc set wt*
WT_PROFILE_ID={61c54bbd-c2c6-5271-96e7-009a87ff44bf}
WT_SESSION=5ef89f6d-f8f9-4228-abb5-bd8e438d2767
PS E:\Utils>
PS E:\Utils> tccrt set wt*
WT_PROFILE_ID={61c54bbd-c2c6-5271-96e7-009a87ff44bf}
WT_SESSION=5ef89f6d-f8f9-4228-abb5-bd8e438d2767
I can see that TCC or TCCRT is running in the PowerShell Profile under Windows Terminal.

From Ubuntu in Windows Terminal, if I run TCC or TCCRT;
Code:
jlc@DESKTOP-H2JFFTF:/mnt/c/Program Files/JPSoft/TCC_RT_26$ tcc.exe /c set wt*
WT_PROFILE_ID={2c4de342-38b7-51cf-b940-2309a097f518}
WT_SESSION=7e21e9b2-4cfa-4a36-8f69-3ada03b695bb
I can see that TCC or TCCRT is running in the Ubuntu Profile under Windows Terminal.

Joe
 

I might be missing something, but isn't CONHOST returning 0, and OPENCONSOLE returning 1?

Joe
 
Yeah but if from TCC, CMD, or PowerShell in Terminal you START any CUI program (e.g., TCC) it'll start in a console and inherit all those variables (and they'll be misleading).
 
Yeah but if from TCC, CMD, or PowerShell in Terminal you START any CUI program (e.g., TCC) it'll start in a console and inherit all those variables (and they'll be misleading).
Ahhh...correct you are.

Thus, it would seem that your WinAPI Solution is the preferred method.

Joe
 

It would appear that using START (via CMD or TCC) causes the program to open using CONHOST instead of OPENCONSOLE, but I suppose that is to be expected.

I have my doubts that Microsoft will be adding an option to START to accommodate that.

Joe
 
Thus, it would seem that your WinAPI Solution is the preferred method.
In a BTM or at the command line it probably doesn't matter, your FFIND one, the @REGEX one, the @WINAPI one ...

I also did this with a plugin. There, the SendMessage one was easiest, and possibly fastest.

Code:
INT WINAPI _WT (LPWSTR psz)
{
    wsprintf(psz, L"%lu", !((BOOL) SendMessage(global.hWndConsole, WM_GETICON, 0, 0)) );
    return 0;
}
 
I've been doing it this way, tho it only works if you're past Windows 7:

Code:
    if "%SESSIONNAME%" eq "Console"  (set container=TCC)
    if defined WT_SESSION            (set container=WindowsTerminal)
 
I've been doing it this way, tho it only works if you're past Windows 7:

Code:
    if "%SESSIONNAME%" eq "Console"  (set container=TCC)
    if defined WT_SESSION            (set container=WindowsTerminal)
As noted earlier, those variables could be inherited (and misleading).

1710779114087.png
 
I check for an environment variable WT_SESSION. If it's not found, I assume TCC is not running under Windows Terminal. If it is....

Enumerate all top-level windows of class CASCADIA_HOSTING_WINDOW_CLASS. Look for one whose PID is an ancestor (parent, grandparent, great-grandparent...) of TCC's. Ugly, but it seems to work for me.
 
I check for an environment variable WT_SESSION. If it's not found, I assume TCC is not running under Windows Terminal. If it is....

Enumerate all top-level windows of class CASCADIA_HOSTING_WINDOW_CLASS. Look for one whose PID is an ancestor (parent, grandparent, great-grandparent...) of TCC's. Ugly, but it seems to work for me.
I was wondering if anyone was playing with this using the Win32 API. On GitHub, @zadjii-msft (Mike Griese) gave me a very helpful tip (see below). For both Conhost and OpenConsole, if you GetConsoleWindow(), and then GetWindowThreadProcessId() you'll be led to TCC itself. That's a bit of smoke and mirrors; the window really doesn't belong to TCC. Here's the tip and (an off-topic question about it further below). I do this inside my BOOL RunningInWindowsTerminal() function.

Code:
const auto GetRealWindowOwner = (DWORD(WINAPI*)(HWND)) GetProcAddress(GetModuleHandleW(L"user32.dll"), MAKEINTRESOURCEA(2712));
DWORD dwRealConsoleWindowOwnerPid = GetRealWindowOwner(hWndConsoleWindow);

Then I get the parent of that process and also look for the Cascadia window.

Here's the off-topic question. What is that? It looks like a (local) function definition (as such, forbidden). It's a "C" construct that I'm not familiar with.
 
Here's the off-topic question. What is that? It looks like a (local) function definition (as such, forbidden). It's a "C" construct that I'm not familiar with.
Code:
const auto GetRealWindowOwner = (DWORD(WINAPI*)(HWND)) GetProcAddress(GetModuleHandleW(L"user32.dll"), MAKEINTRESOURCEA(2712));

I reckon that's just another way of doing what I have done otherwise; namely, give the function's signature in a typedef, declare one, and set it equal to GetProcAddress(...).
 
Hey @ClioCJS, I know you're into colors and I just discovered how to do this. All of these tabs are my default profile (TCC31)

1710801075102.png
 
Back
Top