Option Compare Database
Option Explicit
Private Const ENABLE_LINE_INPUT = &H2&
Private Const ENABLE_ECHO_INPUT = &H4&
Private Const ENABLE_MOUSE_INPUT = &H10&
Private Const ENABLE_PROCESSED_INPUT = &H1&
Private Const ENABLE_WINDOW_INPUT = &H8&
Private Const ENABLE_PROCESSED_OUTPUT = &H1&
Private Const ENABLE_WRAP_AT_EOL_OUTPUT = &H2&
Private Const STD_OUTPUT_HANDLE = -11&
Private Const STD_INPUT_HANDLE = -10&
Private Const STD_ERROR_HANDLE = -12&
Private Const INVALID_HANDLE_VALUE = -1&
Private Const GW_HWNDNEXT = 2
Private Declare Function GetParent Lib "user32" _
(ByVal hwnd As Long) As Long
Private Declare Function SetParent Lib "user32" _
(ByVal hWndChild As Long, _
ByVal hWndNewParent As Long) As Long
Private Declare Function AllocConsole Lib "kernel32" () As Long
Private Declare Function FreeConsole Lib "kernel32" () As Long
Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long
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 ReadConsole Lib "kernel32" Alias "ReadConsoleA" _
(ByVal hConsoleInput As Long, _
ByVal lpBuffer As String, _
ByVal nNumberOfCharsToRead As Long, _
lpNumberOfCharsRead As Long, _
lpReserved As Any) As Long
Private Declare Function SetConsoleTitle Lib "kernel32" Alias "SetConsoleTitleA" _
(ByVal lpConsoleTitle As String) As Long
Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" _
(ByVal lpClassName As String, _
ByVal lpWindowName As String) As Long
Private Declare Function GetWindowThreadProcessId Lib "user32" _
(ByVal hwnd As Long, _
lpdwProcessId As Long) As Long
Private Declare Function GetWindow Lib "user32" _
(ByVal hwnd As Long, _
ByVal wCmd As Long) As Long
Private Declare Function GetConsoleMode Lib "kernel32" _
(ByVal hConsoleHandle As Integer, _
ByRef lpMode As Integer) As Integer
Private Declare Function SetConsoleMode Lib "kernel32" _
(ByVal hConsoleHandle As Integer, _
ByVal dwMode As Integer) As Integer
Private Declare Sub ExitProcess Lib "kernel32" _
(ByVal uExitCode As Long)
Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long
Private hWConHandle As Long
Private hWCmdHandle As Long
Private hConsoleOut As Long
Private hConsoleIn As Long
Private hConsoleErr As Long
Private ShellID As Variant
Private Sub Form_Load()
Dim test As Long
Dim var As Variant
Dim str As String
Dim i As Integer
'Create console
If AllocConsole() Then
hConsoleOut = GetStdHandle(STD_OUTPUT_HANDLE)
If hConsoleOut = INVALID_HANDLE_VALUE Then MsgBox "Unable to get STDOUT"
hConsoleIn = GetStdHandle(STD_INPUT_HANDLE)
If hConsoleOut = INVALID_HANDLE_VALUE Then MsgBox "Unable to get STDIN"
Else
MsgBox "Couldn't allocate console"
End If
'Set the caption of the console window
SetConsoleTitle "SSHConn"
'Get the handle of console
hWConHandle = FindWindow(vbNullString, "SSHConn")
Call StartCmd
If SetParent(hWConHandle, hWCmdHandle) Then
Debug.Print "OK"
End If
If GetConsoleMode(hConsoleOut, i) Then
Debug.Print i
End If
[color=red]'This is the problem area[/color]
ConsoleWriteLine "cd C:\putty"
ConsoleWriteLine "plink -v -ssh -load SecureConnection"
End Sub
Private Sub Form_Unload(Cancel As Integer)
'Delete console
CloseHandle hConsoleOut
CloseHandle hConsoleIn
FreeConsole
ExitProcess GetExitCodeProcess(hWConHandle, 0)
'Force the application to quit in order to destroy the cached connection.
'Application.Quit
End Sub
Sub ConsoleWriteLine(sInput As String)
ConsoleWrite sInput + vbCrLf
End Sub
Sub ConsoleWrite(sInput As String)
Dim cWritten As Long
WriteConsole hConsoleOut, ByVal sInput, Len(sInput), cWritten, ByVal 0&
End Sub
Function ConsoleReadLine() As String
Dim ZeroPos As Long
'Create a buffer
ConsoleReadLine = String(10, 0)
'Read the input
ReadConsole hConsoleIn, ConsoleReadLine, Len(ConsoleReadLine), vbNull, vbNull
'Strip off trailing vbCrLf and Chr$(0)'s
ZeroPos = InStr(ConsoleReadLine, Chr$(0))
If ZeroPos > 0 Then ConsoleReadLine = Left$(ConsoleReadLine, ZeroPos - 3)
End Function
Private Sub StartCmd()
Dim buf As String
Dim buf_len As Long
On Error GoTo ShellError
ShellID = Shell("C:\Windows\system32\cmd.exe", vbNormalFocus) 'vbHide
' Display the hWnd.
hWCmdHandle = InstanceToWnd(ShellID)
Exit Sub
ShellError:
MsgBox "Error Shelling file." & vbCrLf & _
Err.Description, vbOKOnly Or vbExclamation, _
"Error"
Exit Sub
End Sub
' Return the window handle for an instance handle.
Private Function InstanceToWnd(ByVal target_pid As Long) As _
Long
Dim test_hwnd As Long
Dim test_pid As Long
Dim test_thread_id As Long
' Get the first window handle.
test_hwnd = FindWindow(ByVal 0&, ByVal 0&)
' Loop until we find the target or we run out
' of windows.
Do While test_hwnd <> 0
' See if this window has a parent. If not,
' it is a top-level window.
If GetParent(test_hwnd) = 0 Then
' This is a top-level window. See if
' it has the target instance handle.
test_thread_id = _
GetWindowThreadProcessId(test_hwnd, _
test_pid)
If test_pid = target_pid Then
' This is the target.
InstanceToWnd = test_hwnd
Exit Do
End If
End If
' Examine the next window.
test_hwnd = GetWindow(test_hwnd, GW_HWNDNEXT)
Loop
End Function