VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



API Console

by Ravage (3 Submissions)
Category: Windows API Call/Explanation
Compatability: Visual Basic 3.0
Difficulty: Intermediate
Date Added: Wed 3rd February 2021
Rating: (8 Votes)

This creates a console window through API. This is very basic, but it teaches you the basics about calling/creating a console through API. Well first:
1.) Create an exe
2.) Delete Form1
3.) Add a Module (NOT a Class Module)
4.) Insert the code below into the module
5.) Run the program and a console window should come up
I didn't document this very well because it is very basic concerning API. Feel free to use this code in your programs and also feel free to add on to this.

Rate API Console






New Page 1


Option Explicit



Public Declare Function AllocConsole Lib "kernel32" () As Long

Public Declare Function FreeConsole Lib "kernel32" () As Long

Public Declare Function GetStdHandle Lib "kernel32" (ByVal nStdHandle As Long) As Long

Public Const STD_INPUT_HANDLE = -10&

Public Const STD_OUTPUT_HANDLE = -11&

Public Const STD_ERROR_HANDLE = -12&

Public Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" (ByVal hConsoleOutput As Long, ByVal lpBuffer As Any, ByVal nNumberOfCharsToWrite As Long, lpNumberOfCharsWritten As Long, lpReserved As Any) As Long

Public Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" (ByVal lpConsoleTitle As String) As Long

Public Declare Function SetConsoleTextAttribute Lib "kernel32" (ByVal hConsoleOutput As Long, ByVal wAttributes As Long) As Long

Public Const FOREGROUND_BLUE = &H1   ' text color contains blue.

Public Const FOREGROUND_GREEN = &H2   ' text color contains green.

Public Const FOREGROUND_INTENSITY = &H8   ' text color is intensified.

Public Const FOREGROUND_RED = &H4   ' text color contains red.

Public Const BACKGROUND_BLUE = &H10   ' background color contains blue.

Public Const BACKGROUND_GREEN = &H20  ' background color contains green.

Public Const BACKGROUND_RED = &H40   ' background color contains red.

Public Const BACKGROUND_INTENSITY = &H80   ' background color is intensified

Public Declare Function SetConsoleMode Lib "kernel32" (ByVal hConsoleHandle As Long, ByVal dwMode As Long) As Long

Public Declare Function ReadConsole Lib "kernel32" Alias "ReadConsoleA" (ByVal hConsoleInput As Long, ByVal lpBuffer As Any, ByVal nNumberOfCharsToRead As Long, lpNumberOfCharsRead As Long, lpReserved As Any) As Long

Public Const ENABLE_LINE_INPUT = &H2

Public Const ENABLE_ECHO_INPUT = &H4

Public Const ENABLE_MOUSE_INPUT = &H10

Public Const ENABLE_PROCESSED_INPUT = &H1

Public Const ENABLE_WINDOW_INPUT = &H8

Public Const ENABLE_PROCESSED_OUTPUT = &H1

Public Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2

Private hConsoleIn As Long 'The console's input handle

Private hConsoleOut As Long 'The console's output handle

Private hConsoleErr As Long 'The console's error handle





Private Sub Main()

Dim szUserInput As String



AllocConsole

SetConsoleTitle "VB Console Example" 'Set the title on the console window



'Get the console's handle

hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)

hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)

hConsoleErr = GetStdHandle(STD_ERROR_HANDLE)



'Print the prompt to the user. Use the vbCrLf to get to a new line.

SetConsoleTextAttribute hConsoleOut, _

FOREGROUND_RED Or FOREGROUND_GREEN _

Or FOREGROUND_BLUE Or FOREGROUND_INTENSITY _

Or BACKGROUND_BLUE



ConsolePrint "VB Console Example" & vbCrLf

SetConsoleTextAttribute hConsoleOut, _

FOREGROUND_RED Or FOREGROUND_GREEN _

Or FOREGROUND_BLUE

ConsolePrint "Enter your name--> "



'Get the user's name

szUserInput = ConsoleRead()

If Not szUserInput = vbNullString Then

ConsolePrint "Hello, " & szUserInput & "!" & vbCrLf

Else

ConsolePrint "Hello, whoever you are!" & vbCrLf

End If



'End the program

ConsolePrint "Press enter to exit"

Call ConsoleRead



FreeConsole

End Sub

Private Sub ConsolePrint(szOut As String)

WriteConsole hConsoleOut, szOut, Len(szOut), vbNull, vbNull

End Sub

Private Function ConsoleRead() As String

Dim sUserInput As String * 256

Call ReadConsole(hConsoleIn, sUserInput, Len(sUserInput), vbNull, vbNull)

'Trim off the NULL charactors and the CRLF.

ConsoleRead = Left$(sUserInput, InStr(sUserInput, Chr$(0)) - 3)

End Function




Download this snippet    Add to My Saved Code

API Console Comments

No comments have been posted about API Console. Why not be the first to post a comment about API Console.

Post your comment

Subject:
Message:
0/1000 characters