Results 1 to 3 of 3
  1. #1
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Closing SSH Tunnel Problem (VB6)

    I am using data on a MY SQL database (remote)
    and Connecting using SSH Tunneling.

    I have put the code below.

    If I run the Connect process from a form it opens the tunnel just fine
    and
    Closes the tunnel just fine.

    However, I want to run the process from a VB Application with NO form
    I tried this using the App.hInstance to set the Hwnd for the tunnel opening rather than the normal forms hWnd
    and it opens the tunnel just fine.
    BUT...
    It does not close the tunnel on termination (just leaves it open)
    It would appear to be something to do with the Hwnd ( I guess ).
    Any suggestions as to how I could get this to work ?

    My only though would be to open a hidden form and use that then close the form at the end.

    I would love a better suggestion

    For Obvious reasons I have NOT shown the real SSH arguments.
    Where arguments are supplied I have used (Argument Details)

    CODE

    Option Compare Database

    Global Const SW_HIDE = 0
    Global Const SEE_MASK_NOCLOSEPROCESS = &H40
    Global Const SSH_PARAMS = "-2 -ssh (mysqldatabasedetails) -i (myprivatekeyinfo) -L (4 digit port)ip address)4 digit port) -batch -v -N "
    Global Const SSH_PLINK = "(Location of PLINK.exe)plink.exe"
    Global Const SSH_OPEN = "Open"
    Global Const SSH_CLOSE = "Close"


    Type SHELLEXECUTEINFO
    cbSize As Long
    fMask As Long
    hwnd As Long
    lpVerb As String
    lpFile As String
    lpParameters As String
    lpDirectory As String
    nShow As Long
    hInstApp As Long
    'Optional fields
    lpIDList As Long
    lpClass As String
    hkeyClass As Long
    dwHotKey As Long
    hIcon As Long
    hProcess As Long
    End Type

    Public si As SHELLEXECUTEINFO

    Declare Function ShellExecuteEx Lib "shell32.dll" (lpExecInfo As SHELLEXECUTEINFO) As Boolean

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

    Declare Function TerminateProcess _
    Lib "kernel32" (ByVal hProcess As Long, _
    ByVal uExitCode As Long) As Long

    Private Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" (ByVal hwnd As Long, _
    ByVal lpszOp As String, _
    ByVal lpszFile As String, _
    ByVal lpszParams As String, _
    ByVal lpszdir As String, _
    ByVal FsShowCmd As Long) As Long

    Function CloseTunnel()

    Dim lngExitCode As Long

    If TerminateProcess(si.hProcess, lngExitCode) <> 0 Then
    CloseHandle (si.hProcess)
    'We Can eliminate this line if we do not need to be informed
    MsgBox "Tunnel Closed!", vbExclamation
    End If

    End Function

    Function OpenSSHTunnel(lngHwnd As Long)

    Dim strPath As String

    'Get Local folder Path
    'In VB we use App.path & ""
    strPath = App.Path & ""


    'Define the Parameters to pass to the Shell Exec Function
    si.cbSize = Len(si)
    si.fMask = SEE_MASK_NOCLOSEPROCESS
    si.hwnd = lngHwnd
    si.lpVerb = SSH_OPEN
    si.lpFile = SSH_PLINK
    si.lpParameters = SSH_PARAMS
    si.lpDirectory = strPath
    si.nShow = SW_HIDE

    'Check and Inform (Messages may be replaced by procedures
    If ShellExecuteEx(si) Then
    MsgBox "Tunnel Open!"
    Else
    MsgBox "Opening Failed!"
    End If

    End Function

    CODE TO OPEN THE TUNNEL

    Private Sub cmdOpenTunnel_Click()

    Dim lngHWnd As Long

    'lngHWnd = Me.hwnd
    lngHWnd = App.hInstance
    Call OpenSSHTunnel(lngHWnd)

    End Sub

    I do NOT want to open this from the form.
    If I use the forms handle it works just fine
    If I use the App handle it won't close.

    Any suggestions ?
    Andrew

  2. #2
    Plutonium Lounger
    Join Date
    Mar 2002
    Posts
    84,353
    Thanks
    0
    Thanked 29 Times in 29 Posts

    Re: Closing SSH Tunnel Problem (VB6)

    What happens if you pass 0 as hWnd?

  3. #3
    5 Star Lounger AndrewKKWalker's Avatar
    Join Date
    Apr 2001
    Location
    Cambridge, UK
    Posts
    1,020
    Thanks
    0
    Thanked 3 Times in 3 Posts

    Re: Closing SSH Tunnel Problem (VB6)

    Magic.

    That works just fine.

    You're a very fine chap!

    I almost feel embarrassed now by the length of my question.
    Andrew

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •