Jump to content

Write to console


Recommended Posts

Hello,

i want to open a console and write to it, but this only work if the script is compiled.

Is it even possible to do this?

#cs
   Option Explicit
   Private Declare Function GetStdHandle Lib "kernel32" _
           (ByVal nStdHandle As Long) As Long
   Private Declare Function WriteConsole Lib "kernel32" Alias "WriteConsoleA" _
           (ByVal hConsoleOutput As Long, _
            lpBuffer As Any, _
            ByVal nNumberOfCharsToWrite As Long, _
            lpNumberOfCharsWritten As Long, _
            lpReserved As Any) As Long

   Private Declare Function AllocConsole Lib "kernel32" () As Long
   Private Declare Function FreeConsole Lib "kernel32" () As Long
   Private Declare Function SetStdHandle Lib "kernel32" _
           (ByVal nStdHandle As Long, ByVal nHandle As Long) As Long

   Private Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long

   Private Declare Function CreateProcessA Lib "kernel32" _
           (ByVal lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
            lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
            ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
            ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
            lpStartupInfo As STARTUPINFO, lpProcessInformation As _
            PROCESS_INFORMATION) As Long

   Private Declare Function WaitForSingleObject Lib "kernel32" _
           (ByVal hHandle As Long, _
            ByVal dwMilliseconds As Long) As Long

    Private Const NORMAL_PRIORITY_CLASS = &H20&
    Private Const INFINITE = -1&
   Private Type STARTUPINFO
       cb As Long
       lpReserved As String
       lpDesktop As String
       lpTitle As String
       dwX As Long
       dwY As Long
       dwXSize As Long
       dwYSize As Long
       dwXCountChars As Long
       dwYCountChars As Long
       dwFillAttribute As Long
       dwflags As Long
       wShowWindow As Integer
       cbReserved2 As Integer
       lpReserved2 As Long
       hStdInput As Long
       hStdOutput As Long
       hStdError As Long
   End Type
   Private Type PROCESS_INFORMATION
       hProcess As Long
       hThread As Long
       dwProcessID As Long
       dwThreadID As Long
   End Type
   Private Const STD_INPUT_HANDLE = -10&
   Private Const STD_OUTPUT_HANDLE = -11&

  Private Sub Command1_Click()

       Dim Result As Long
       Dim hProcess As Long
       Dim hConsoleOutput As Long
       Dim lpNumberOfCharsWritten As Long
       Dim lpBuffer As String
       'Startet die Konsole und gibt einen Handle (hConsoleOutput) zurück
       If AllocConsole Then
           hConsoleOutput = GetStdHandle(STD_OUTPUT_HANDLE)
       Else
           End
       End If

       'Started den DOS-Prompt und liefert den Windows-Handle (hProcess)
       hProcess = ExecCmd("cmd.exe")
       'Mapped den Konsolen-Handle auf den gerade gestarteten Command-Prompt um
       Result = SetStdHandle(hConsoleOutput, hProcess)

       lpBuffer = "Hallo"
       'Schreibt in die Konsole den Text lpBuffer
       Result = WriteConsole(hConsoleOutput, ByVal lpBuffer, Len(lpBuffer), lpNumberOfCharsWritten, ByVal 0)

       'schließt die Konsole, die mit AllocConsole geöffnet wurde.
       FreeConsole 'ohne gibt es einen IPF

       'Warten bis der Commandprompt geschlossen wird
       Result = WaitForSingleObject(hProcess, INFINITE)
       Result = CloseHandle(hProcess)

       End
   End Sub

   'Funktion startet eine Anwendung mit CreateProcessA und liefert einen gültigen
   'Windows-Handle
   Private Function ExecCmd(cmdline As String) As Long
       Dim ret As Long
       Dim proc As PROCESS_INFORMATION
       Dim start As STARTUPINFO

              'Initialisiert die STARTUPINFO Struktur:
       start.cb = Len(start)
       'Startet die Anwendung:
       ret = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
             NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
       ExecCmd = proc.hProcess
   End Function

#ce
#include <WinAPI.au3>
#include <Array.au3>

Func GetStdHandle ($nStdHandle)
    return DllCall ("Kernel32.dll", "long", "GetStdHandle", "long", $nStdHandle)
EndFunc

Func AllocConsole ()
    return DllCall ("Kernel32.dll", "int", "AllocConsole")
EndFunc


Func FreeConsole ()
    return DllCall ("Kernel32.dll", "int", "FreeConsole")
EndFunc

Func SetStdHandle ($nStdHandle, $nHandle)
    Return DllCall ("Kernel32.dll", "int", "SetStdHandle", "long", $nStdHandle, "long", $nHandle)
EndFunc


DllCall ("Kernel32.dll", "int", "FreeConsole")

DllCall ("Kernel32.dll", "int", "AllocConsole")
$aReturnStdHandle = DllCall ("Kernel32.dll", "long", "GetStdHandle", "long", -11)
_ArrayDisplay ($aReturnStdHandle)
$vStartUp = DllStructCreate ($tagSTARTUPINFO)

$vPInfo = DllStructCreate ($tagPROCESS_INFORMATION)

_WinAPI_CreateProcess ("", "cmd.exe", 0, 0, 1, 0x00000020, 0, 0, DllStructGetPtr ($vStartUp), DllStructGetPtr ($vPInfo))
DllCall ("Kernel32.dll", "int", "SetStdHandle", "long", -11, "long", DllStructGetData ($vPInfo, "hProcess"))
ConsoleWrite ( DllStructGetData ($vPInfo, "hProcess"))
ConsoleWrite (_WinAPI_GetLastErrorMessage ())
_WinAPI_WriteConsole ($aReturnStdHandle[0], "test")
ConsoleWrite (_WinAPI_GetLastErrorMessage ())
ConsoleWrite ("test")
Sleep ( 5000 )

Thanks if you help me :D

Tom

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...