Winows11 22H2 console applications

Windows specific questions.
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Winows11 22H2 console applications

Post by UEZ »

Since Winows11 22H2 console applications are opened in a Terminal window by default instead of cmd window.
Any idea how to detect in which window the application was opened if the parent process is not CMD or PowerShell?
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

This seems to work but it is not the best solution:

Code: Select all

#Ifdef __Fb_64bit__
	#Inclib "gdiplus"
	#Include Once "win\gdiplus-c.bi"
#Else
	#Include Once "win\gdiplus.bi"
	Using Gdiplus
#Endif

Declare Function RtlGetVersion Lib "NtDll.dll" Alias "RtlGetVersion" (OsVersionInformation As RTL_OSVERSIONINFOW) As Long

Dim As RTL_OSVERSIONINFOW OS
OS.dwOSVersionInfoSize = Sizeof(RTL_OSVERSIONINFOW)
RtlGetVersion(OS)

Dim As Any Ptr hConsole = GetConsoleWindow()

If SendMessageW(hConsole, WM_GETICON, Iif(OS.dwBuildNumber < 9200, 1, 0), 0) = 0 Then
	? "Please open this app in command-line window (CMD) - Terminal window is not support yet!"
	Sleep
	End
Endif

? "Command-line Window detected"

Sleep
Let's see if there are other possibilities....
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

Hi UEZ :)
you probably know this but it took me several day to find the solution on the web, so I hope you don't mind me sharing this in this thread
if you want the command window to open using the old Windows Console Host follow these steps
open Settings, click on the Privacy & security tab on the left
click on For developers then near the bottom you will see Terminal, choose Windows Console Host from the list
another option that is tricky to find is Snap windows, here's how to turn it off
in Settings --> System --> Multitasking turn off Snap Windows
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

srvaldez wrote: Jun 04, 2023 11:02 Hi UEZ :)
you probably know this but it took me several day to find the solution on the web, so I hope you don't mind me sharing this in this thread
if you want the command window to open using the old Windows Console Host follow these steps
open Settings, click on the Privacy & security tab on the left
click on For developers then near the bottom you will see Terminal, choose Windows Console Host from the list
another option that is tricky to find is Snap windows, here's how to turn it off
in Settings --> System --> Multitasking turn off Snap Windows
The reason for the topic is that after Windows Update to Win11 22H2 Terminal Window got the default console and thus Radio Station amigaremix.com / Radio Station remix.kwed.org don't work properly anymore.
For sure I can adjust my computer to open it by default command-line window as you have described but that is not the problem. If you publish an app that will not work anymore with Win11 22H2, you need a way to check the window in which it starts or you change the code to work also with Terminal Window which is a PowerShell console, afaik. Maybe the command-line window code will removed from Windows a day...

I'm wondering why there is Windows API to do the check.

Anyhow, just searching for a way to start the executable in a command-line window directly, "Start" command is not working...
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

UEZ, while I was searching for a solution for the console problem I noticed that some application -- don't remember which program -- would open the console using the old Windows Console Host, so there must be a way for your program to trick Windows to choose Windows Console Host instead of terminal
adeyblue
Posts: 299
Joined: Nov 07, 2019 20:08

Re: Winows11 22H2 console applications

Post by adeyblue »

The source code to the terminal is on Gihub, here's how they create the window returned by GetConsoleWindow(). So you can check it for that class name, those styles or the fact that it never has WS_VISIBLE
would open the console using the old Windows Console Host, so there must be a way for your program to trick Windows to choose Windows Console Host instead of terminal
You can open an app with the console host directly with just
conhost.exe myapp.exe
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

adeyblue wrote: Jun 04, 2023 20:24 The source code to the terminal is on Gihub, here's how they create the window returned by GetConsoleWindow(). So you can check it for that class name, those styles or the fact that it never has WS_VISIBLE
would open the console using the old Windows Console Host, so there must be a way for your program to trick Windows to choose Windows Console Host instead of terminal
You can open an app with the console host directly with just
conhost.exe myapp.exe
Thanks for the hint!

Code: Select all

#Ifdef __Fb_64bit__
	#Inclib "gdiplus"
	#Include Once "win\gdiplus-c.bi"
#Else
	#Include Once "win\gdiplus.bi"
	Using Gdiplus
#Endif

Declare Function RtlGetVersion Lib "NtDll.dll" Alias "RtlGetVersion" (OsVersionInformation As RTL_OSVERSIONINFOW) As Long
Dim As RTL_OSVERSIONINFOW OS
OS.dwOSVersionInfoSize = Sizeof(RTL_OSVERSIONINFOW)
RtlGetVersion(OS)

Dim As Any Ptr hConsole
Dim As Handle hStdOut, hStdIn

AllocConsole()

hStdOut = GetStdHandle(STD_OUTPUT_HANDLE)
hStdIn = GetStdHandle(STD_INPUT_HANDLE)
hConsole = GetConsoleWindow()

Dim As Integer iStyle = GetWindowLong(hConsole, GWL_STYLE)

Dim As Wstring * 4096 sClassname
GetClassNameW(hConsole, @sClassname, 4096)

If (SendMessageW(hConsole, WM_GETICON, Iif(OS.dwBuildNumber < 9200, 1, 0), 0) = 0 And sClassname = "PseudoConsoleWindow" And (iStyle And WS_VISIBLE) = 0) Then
	? "Please open this app in command-line window (CMD) - Terminal window is not support yet!"
	Sleep
	End
Endif

? "Command-line Window detected"

Sleep
FreeConsole()
And yes, conhost.exe myapp.exe works although it opens a Terminal window first which starts afterwards the app in a cmd window.
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

@dodicat: as srvaldez wrote the handle to console doesn't work for graphical operations in a Terminal window.

This works without any problems.

Code: Select all

screen 0
dim as long owners=2000000000
dim as long coders=27000000
print "Percentage of computer owners who can write code ";(coders/owners)*100;"%"
sleep
 
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Winows11 22H2 console applications

Post by dodicat »

Thanks UEZ.
It is not common to do graphics on the console with bitblt and double buffering I reckon.
With only 1.35 percent of people writing code in the first place, very few percent will be disappointed.

But coders in Win 11 now have a little less choice.
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

dodicat wrote: Jun 05, 2023 14:54 Thanks UEZ.
It is not common to do graphics on the console with bitblt and double buffering I reckon.
With only 1.35 percent of people writing code in the first place, very few percent will be disappointed.

But coders in Win 11 now have a little less choice.
I absolutely agree with you that the console is not suitable for graphic gimmicks, but I just wanted to spruce up the CMD Radio Station Players console a little with a graphic / animation. Unfortunately, the keystrokes don't work in the terminal window either.

Every now and then it is interesting to go beyond the limits...
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

Any idea how to execute this code compiled as console and x86? Running on x64 works...

Code: Select all

Shell("conhost.exe ping.exe localhost")
Sleep
Getting em: 'conhost.exe' is not recognized as an internal or external command, operable program or batch file.

Might be an issue with System32 / SysWOW64 folders because conhost.exe doesn't exist in folder SysWOW64.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

I suggest that you investigate other means to open a console, perhaps KernelBase.dll has the functions needed
for example it has AllocConsole and other console functions https://learn.microsoft.com/en-us/windo ... locconsole
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

in reference to viewtopic.php?p=299151#p299151
dodicat wrote: Jun 06, 2023 10:14 ...
I'll move on, I have some console code to check (which looks ominous for Win 11)
For instance does runcommand cmd /c "<$file>" <$param> & pause, in fbide still work in Win 11?
@dodicat
I just downloaded fbide and compiled the example hello.bas without problem
yes, cmd /c "<$file>" <$param> & pause works
UEZ
Posts: 972
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

srvaldez wrote: Jun 06, 2023 10:47 I suggest that you investigate other means to open a console, perhaps KernelBase.dll has the functions needed
for example it has AllocConsole and other console functions https://learn.microsoft.com/en-us/windo ... locconsole
I tested it and the result is the same -> opening in a terminal window. conhost.exe seems work only when code is compiled as x64.
srvaldez wrote: Jun 06, 2023 11:25 in reference to viewtopic.php?p=299151#p299151
dodicat wrote: Jun 06, 2023 10:14 ...
I'll move on, I have some console code to check (which looks ominous for Win 11)
For instance does runcommand cmd /c "<$file>" <$param> & pause, in fbide still work in Win 11?
@dodicat
I just downloaded fbide and compiled the example hello.bas without problem
I don't understand what you mean by cmd /c "<$file>" <$param> & pause how would I test that in fbide?
ShellExecute(0, "open", "cmd.exe", "/c <yourapp.exe>", 0, SW_SHOW) ends up in terminal window when it set as default console.
srvaldez
Posts: 3373
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

UEZ, the last part of my quote, cmd /c "<$file>" <$param> & pause opens the terminal but it works as expected
Post Reply