Wednesday 25 December 2019

GetConsoleColour.exe prints the current console colour and returns an errorlevel with the value

This uses the inbuilt compilers in Windows 10 - there are three VB.NET compilers and three C# compilers - just copy each text file into the same folder and double click the batch file to make the program.
;Two files follow
REM GetConsoleColour.bat
REM This file compiles GetConsoleColour.vb to GetConsoleColour.exe
REM GetConsoleColour.exe prints the current console colour and returns an errorlevel with the value
"C:\Windows\Microsoft.NET\Framework\v4.0.30319\vbc.exe" /target:exe /out:"%~dp0\GetConsoleColour.exe" "%~dp0\GetConsoleColour.vb" 
pause




'GetConsoleColour.vb
Imports System
Imports System.IO
Imports System.Runtime.InteropServices
Imports Microsoft.Win32

Public Module MyApplication 
 
Public Declare Function GetStdHandle Lib "kernel32" Alias "GetStdHandle" (ByVal nStdHandle As Long) As Long
Public Declare Function SetConsoleTextAttribute Lib "kernel32" Alias "SetConsoleTextAttribute" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long
Public Declare Function GetConsoleScreenBufferInfo Lib "kernel32" (ByVal hConsoleOutput As Integer, ByRef lpConsoleScreenBufferInfo As CONSOLE_SCREEN_BUFFER_INFO) As Integer
Public Const STD_ERROR_HANDLE = -12&
Public Const STD_INPUT_HANDLE = -10&
Public Const STD_OUTPUT_HANDLE = -11&

  _
Public Structure COORD
 Public x As Short
 Public y As Short
End Structure

  _
Public Structure SMALL_RECT
 Public Left As Short
 Public Top As Short
 Public Right As Short
 Public Bottom As Short
End Structure

  _
Public Structure CONSOLE_SCREEN_BUFFER_INFO
 Public dwSize As COORD
 Public dwCursorPosition As COORD
 Public wAttributes As Integer
 Public srWindow As SMALL_RECT
 Public dwMaximumWindowSize As COORD
End Structure 


Sub Main()
 Dim hOut as IntPtr
 Dim Ret as Integer
 Dim CSBI as Console_Screen_Buffer_Info
 hOut  = GetStdHandle(STD_OUTPUT_HANDLE)
 Ret = GetConsoleScreenBufferInfo(hOut, CSBI)
 Console.Writeline(Hex(CSBI.wAttributes))
 Environment.ExitCode = CSBI.wAttributes
End Sub
End Module

No comments:

Post a Comment