Home > front end >  How to use handle in multiple subs
How to use handle in multiple subs

Time:08-31

I'm certainly no expert on VB, so hopefully someone is willing to help me out here. When I use the code down below, the msgbox in mybase.shown properly shows the handle number, but the one in button1.click throws the exception "No process is associated with this object". So apparently the handle is only available during the mybase.shown sub? How do I make it available for other subs too? Thanks for any help in advance! Kind regards, Eric

Option Strict On
    Public Class Form1
        Private WithEvents proc As New Process
        Private Const WM_SYSCOMMAND As Integer = &H112
        Private Const SC_MAXIMIZE As Integer = &HF030
        Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
        Declare Auto Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndParent As IntPtr) As Integer
        Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
            Dim proc As Process = Process.Start("notepad")
            Threading.Thread.Sleep(200)
            SetParent(proc.MainWindowHandle, Panel1.Handle)
            SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
            MsgBox(proc.MainWindowHandle)
        End Sub
        Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            MsgBox(proc.MainWindowHandle)
            SetParent(proc.MainWindowHandle, IntPtr.Zero)
        End Sub
    End Class

CodePudding user response:

Thank you very much everybody! With your help, I finally managed to make it work. I really appreciate your efforts! Kind regards, Eric

Options Strict On
Public Class Form1
    Private WithEvents proc As Process
    Private Const WM_SYSCOMMAND As Integer = &H112
    Private Const SC_MAXIMIZE As Integer = &HF030
    Private Declare Function GetParent Lib "user32" (ByVal hwnd As Long) As Long
    Declare Auto Function SetParent Lib "user32" (ByVal hWndChild As IntPtr, ByVal hWndParent As IntPtr) As Integer
    Declare Auto Function SendMessage Lib "user32" (ByVal hWnd As IntPtr, ByVal Msg As Integer, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
    Dim testje As Long
    Public Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Shown
        proc = Process.Start("notepad")
        Threading.Thread.Sleep(200)
        SetParent(proc.MainWindowHandle, Panel1.Handle)
        SendMessage(proc.MainWindowHandle, WM_SYSCOMMAND, SC_MAXIMIZE, 0)
    End Sub
    Public Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        SetParent(proc.MainWindowHandle, IntPtr.Zero)
    End Sub
End Class
  • Related