Winows11 22H2 console applications

Windows specific questions.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Winows11 22H2 console applications

Post by dodicat »

To test fbide with the run settings do

Code: Select all

#cmdline "-exx"
dim as integer ptr p
print *p 
You should see the runtime error on the console.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

srvaldez wrote: Jun 06, 2023 11:43 UEZ, the last part of my quote, cmd /c "<$file>" <$param> & pause opens the terminal but it works as expected
Also for the console clock by dodicat?
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

UEZ, no, of course not
dodicat, it displays the error in the terminal
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

windows 7 x86 has conhost.exe in system32, I will try and see if it can be used in Windows 11, it probably has a ton of dependent DLL's
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

@UEZ
if you have Windows 10 x86 then you should have api-ms-win-core-console-l1-1-0.dll somewhere in C:\Windows\WinSxS\ it exports the following functions
LIBRARY api-ms-win-core-console-l1-1-0.dll
EXPORTS
AllocConsole
GetConsoleCP
GetConsoleMode
GetConsoleOutputCP
GetNumberOfConsoleInputEvents
PeekConsoleInputA
ReadConsoleA
ReadConsoleInputA
ReadConsoleInputW
ReadConsoleW
SetConsoleCtrlHandler
SetConsoleMode
WriteConsoleA
WriteConsoleW

I don't see any dependencies

<edit> the DLL is also available in Windows 11 somewhere in C:\Windows\WinSxS\
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

UEZ
here's a quick and easy way for your console program to run using the conhost, create a shortcut of your program, then righr-click on the shortcut and select properties, click on the options tab and put a check mark on Use legacy console, that's it
<edit> after you made the short-cut and checked the legacy option you can delete the short-cut and your program will still use the legacy console, although for distribution, if your program is run on a different PC then it would probably require the short-cut
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

srvaldez wrote: Jun 06, 2023 21:24 UEZ
here's a quick and easy way for your console program to run using the conhost, create a shortcut of your program, then righr-click on the shortcut and select properties, click on the options tab and put a check mark on Use legacy console, that's it
<edit> after you made the short-cut and checked the legacy option you can delete the short-cut and your program will still use the legacy console, although for distribution, if your program is run on a different PC then it would probably require the short-cut
Thank you for your research and efforts on this thread, but my goal is not to solve this on my system, but from within the software, because I can't expect every user to change their system just to make the software run properly.

One idea is to restart the app using conhost.exe.

Code extract for the CMD Radio Station apps:

Code: Select all

...
Function _WinAPI_TerminateProcess(iPID As Integer, iExitCode As Integer = 0, bInheritHandle As Boolean = FALSE) As Boolean
	Dim As Long dwDesiredAccess = PROCESS_TERMINATE
	Dim As Handle hProcess = OpenProcess(dwDesiredAccess, bInheritHandle, iPID)
	If hProcess = Null Then Return False
	TerminateProcess(hProcess, iExitCode)
	CloseHandle(hProcess)
	Return True
End Function

Function _WinAPI_GetProcessName(iPid As DWORD) As String
	Dim As HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, iPid)
	If hSnapshot = 0 Then Return ""
	Dim As PROCESSENTRY32W tPROCESSENTRY32W
	tPROCESSENTRY32W.dwSize = Sizeof(PROCESSENTRY32W)
	Process32FirstW(hSnapshot, @tPROCESSENTRY32W)
	While True
		If tPROCESSENTRY32W.th32ProcessID = iPid Then Exit While
		If Process32NextW(hSnapshot,  @tPROCESSENTRY32W) = 0 Then Exit While
	Wend
	CloseHandle(hSnapshot)
	Return tPROCESSENTRY32W.szExeFile
End Function

Function _WinAPI_GetParentProcess(iPID As Integer = 0) As Integer
	Dim As DWORD pid = Iif(iPID = 0, GetCurrentProcessId(), iPID), pid_parent = 0
	Dim As HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0)
	Dim As PROCESSENTRY32 tPROCESSENTRY32
	tPROCESSENTRY32.dwSize = Sizeof(tPROCESSENTRY32)
	Process32First(hSnapshot, @tPROCESSENTRY32)
	While TRUE
		If tPROCESSENTRY32.th32ProcessID = pid Then
			pid_parent = tPROCESSENTRY32.th32ParentProcessID
			Exit While
		End If
		Process32Next(hSnapshot, @tPROCESSENTRY32)
	Wend
	CloseHandle(hSnapshot)
	Return pid_parent
End Function
...
Sub GetCommandLineArguments(Byref sArguments As String)
    Dim As Ushort i = 1
	Dim As String s
    While True
		s = Command(i)
		If Len(s) = 0 Then Exit While
        sArguments &= s & " "
		i += 1
	Wend
End Sub
...
hConsole = GetConsoleWindow()

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

Dim Shared As Integer iOldStyle
iOldStyle = GetWindowLong(hConsole, GWL_STYLE)

Dim As String sArguments = " "
GetCommandLineArguments(sArguments)

If (SendMessageW(hConsole, WM_GETICON, Iif(OS.dwBuildNumber < 9200, 1, 0), 0) = 0 And sClassname = "PseudoConsoleWindow") Then 'restart app in CMD window
	#ifdef __FB_64BIT__
		Shell("conhost.exe """ &  Command(0) & """ restart" & sArguments)
	#else
		? "Please open this app in command-line window (CMD) - Terminal window is not support yet!"
		Sleep()
	#endif 
	FreeConsole()
	End 1000
Endif

If Command(1) = "restart" Then
	Dim As String exeName = Mid(Command(0), Instrrev(Command(0), "/") + 1, Len(Command(0)))
	Dim As Ubyte countParentPIDs = 0
	Dim As Integer iPID, parentPID = _WinAPI_GetParentProcess()
	While True
		iPID = _WinAPI_GetParentProcess(parentPID)
		If _WinAPI_GetProcessName(iPID) <> "cmd.exe" Then 
			parentPID = iPID
			countParentPIDs += 1
			If countParentPIDs > 5 Then Exit While
		Else
			_WinAPI_TerminateProcess(iPID, 0, True)
			Exit While
		Endif
	Wend
Endif
It works properly when compiled as x64. App will start in a terminal window but restart it in a cmd window and kill the parent useless processes. With x86 conhost.exe is not found.
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Winows11 22H2 console applications

Post by dodicat »

Here is a way to scroll a win 11 console by using the up/down arrow keys.

Code: Select all


Sub scrollpage(s As String)
    Var y=Hiword(Width)
    Dim As String i
    Redim As long page(1 To 1)
    page(1)=0
    For z As long=0 To Len(s)-1
        Print Chr(s[z]);
        If Csrlin>y-2 Then
            Redim Preserve page(1 To Ubound(page)+1)
            page(Ubound(page))=z
            print "(Use up/down arrow keys, PROGRESS:"+" ";z;" of ";len(s);" characters)"
            Sleep
            do
            i=Inkey
           ' sleep 1
            loop until i=chr(27) or i=Chr(256)+"H" or i=chr(256)+"P"
            If i=Chr(27) Then Exit Sub
            If i=Chr(256)+"H" Then  ''UP ARROW
                If Ubound(page)>2 Then
                    z=page(Ubound(page)-2)
                    Redim Preserve page(Ubound(page)-2)
                    If Ubound(page)=1 Then z=-1
                Else
                 'down arrow
                    z=-1
                End If
            End If 
            Cls
            Locate 1
        End If
        If z=Len(s)-1 Then 
            Print "end of file"
            Sleep
            If Inkey=Chr(256)+"H" Then Cls:scrollpage(s)
        End If
    Next  
    
End Sub



Function PipeToStringDC( s As String)As String
    Var f=Freefile
    Open Pipe s For Binary Access Read As #f
    function=Input(100000000,f)
    Close #f
End Function


var h=PipeToStringDC("tree " +"""c:/Program Files"""+" /F /A")
'var h=PipeToStringDC("tree " +"c:/windows"+" /F /A")

scrollpage( h)
 
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

@dodicat: nice - reminds me of the old DOS days... Thanks for sharing.
srvaldez
Posts: 3379
Joined: Sep 25, 2005 21:54

Re: Winows11 22H2 console applications

Post by srvaldez »

hi dodicat :)
your program seemed to hang because nothing was displayed for like 10 seconds, so I killed it
but later I let it run longer and it worked as advertised :)
dodicat
Posts: 7983
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: Winows11 22H2 console applications

Post by dodicat »

Thanks UEZ/srvaldez.
It takes windows a little time to create the tree.

In c, printf works in the console, and in c++, cout works in the console, the scrollbars become active.
I have not tested freepascal yet.
fb crt printf also works OK for a whole string from a shell command (the tree for example), but not properly from a string derived in fb, especially if it has been built by concatenation. -- LOOKS LIKE ANYWAY.
Also, here, If I want -s gui I get an error from windows.
Also freeconsole throws an error if using a gfx window.
So the whole shebang is a can of worms.
Cherry
Posts: 358
Joined: Oct 23, 2007 12:06
Location: Austria
Contact:

Re: Winows11 22H2 console applications

Post by Cherry »

Excuse my ignorance but what does listening to Internet radio have to do with somebody's default terminal application...?! I'm super confused. I also googled "amigaremix cmd" but nothing useful came up.
UEZ
Posts: 988
Joined: May 05, 2017 19:59
Location: Germany

Re: Winows11 22H2 console applications

Post by UEZ »

Cherry wrote: Oct 22, 2023 15:48 Excuse my ignorance but what does listening to Internet radio have to do with somebody's default terminal application...?! I'm super confused. I also googled "amigaremix cmd" but nothing useful came up.
The reason is simple: I coded Radio Stations which uses the command-line window (CMD) to display some information about the current played song which is hosted either on remix.kwed.org or amigaremix.com (two different apps!).
Since Terminal console is the default console app and not CMD, my app is not working properly anymore. The question was how to detect it and bypass it without any user intervention.
I hope it helps you to understand the problem.
Post Reply