- Aug
- 2,312
- 111
TCC has the _stdout internal variable,
it's purpose being to return 1 if STDOUT points to the console, or 0 if it has been redirected.
I would like to know where STDOUT is being redirected to.
If there is a method to do this, I have missed it in the help file.
The .btm that I wrote (below) is supposed to tell me,
but it always returns 0.
Here are a few sample runs;
I would appreciate a review of my code.
Maybe I am not understanding the Win API functions correctly.
Joe
it's purpose being to return 1 if STDOUT points to the console, or 0 if it has been redirected.
I would like to know where STDOUT is being redirected to.
If there is a method to do this, I have missed it in the help file.
The .btm that I wrote (below) is supposed to tell me,
but it always returns 0.
Here are a few sample runs;
Code:
E:\Utils>stdio.btm
STDIN points to the console
STDOUT points to the console
GetStdHandle: 84
FILE_TYPE: 0
Either the type of the specified file is unknown, or the function failed.
Code:
E:\Utils>stdio > clip9:
E:\Utils>type clip9:
STDIN points to the console
STDOUT is redirected
GetStdHandle: 3296
FILE_TYPE: 0
Either the type of the specified file is unknown, or the function failed.
Code:
E:\Utils>stdio.btm > r:\results.txt
E:\Utils>type r:\results.txt
STDIN points to the console
STDOUT is redirected
GetStdHandle: 3272
FILE_TYPE: 0
Either the type of the specified file is unknown, or the function failed.
Code:
stdio.btm | view
STDIN points to the console
STDOUT is redirected
GetStdHandle: 1188
FILE_TYPE: 0
Either the type of the specified file is unknown, or the function failed.
I would appreciate a review of my code.
Maybe I am not understanding the Win API functions correctly.
Code:
@setlocal
@echo off
::The PowerBASIC WinBase.inc has the following;
::
:: %STD_INPUT_HANDLE = &HFFFFFFF6??? '(DWORD)-10
:: %STD_OUTPUT_HANDLE = &HFFFFFFF5??? '(DWORD)-11
:: %STD_ERROR_HANDLE = &HFFFFFFF4??? '(DWORD)-12
::
:: https://learn.microsoft.com/en-us/windows/console/getstdhandle
::
:: Convert -10 to Decimal
set STD_INPUT_HANLDE=%@convert[16,10,FFFFFFF6???]
:: Convert -11 to Decimal
set STD_OUTPUT_HANDLE=%@convert[16,10,FFFFFFF5???]
:: Convert -12 to Decimal
set STD_ERROR_HANDLE=%@convert[16,10,FFFFFFF4???]
::
:: https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfiletype
set FILE_TYPE_CHAR=2
set FILE_TYPE_DISK=1
set FILE_TYPE_PIPE=3
set FILE_TYPE_REMOTE=32768
set FILE_TYPE_UNKNOWN=0
::
if %_stdin eq 1 (echo STDIN points to the console) else (echo STDIN is redirected)
if %_stdout eq 1 (echo STDOUT points to the console) else (echo STDOUT is redirected)
::
:: Get the Standard Output Handle
::
set hFile=%@winapi[kernel32.dll,GetStdHandle,%STD_OUTPUT_HANDLE]
echo GetStdHandle: %hFile
::
:: Get the File Type value
::
:: NOTE: hFile was missing the preceeding %
:: Ref. https://jpsoft.com/forums/threads/determine-where-stdout-is-redirected.11399/post-64820
set FILE_TYPE=%@winapi[kernel32.dll,GetFileType,%hFile]
echo FILE_TYPE: %FILE_TYPE
switch %FILE_TYPE
case %FILE_TYPE_CHAR
echo The specified file is a character file, typically an LPT device or a console.
case %FILE_TYPE_DISK
echo The specified file is a disk file.
case %FILE_TYPE_PIPE
echo The specified file is a socket, a named pipe, or an anonymous pipe.
case %FILE_TYPE_REMOTE
echo Unused.
case %FILE_TYPE_UNKNOWN
echo Either the type of the specified file is unknown, or the function failed.
default
echo hFile: %hFile
endswitch
endlocal
Joe
Last edited: