Results 1 to 11 of 11

Thread: [RESOLVED] Running another application inside a picturebox - issue with Windows7 permission

Threaded View

  1. #3
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Running another application inside a picturebox - issue with Windows7 permission

    try....

    When coding start VS as admin.
    For compile, set requestedExecutionLevel line in app.manifest to level="requireAdministrator".

    Code:
    ' When coding start VS as admin.
    ' For compile, set requestedExecutionLevel line in app.manifest to level="requireAdministrator".
    
    Imports System.Runtime.InteropServices
    
    Public Class Form1
    
        <DllImport("user32.dll")>
        Private Shared Function SetParent(ByVal hwndChild As IntPtr _
                         , ByVal hwndNewParent As IntPtr) As IntPtr
        End Function
    
        <DllImport("user32.dll", CharSet:=CharSet.Auto, SetLastError:=True)>
        Private Shared Function IsWindow(ByVal hWnd As IntPtr) As Boolean
        End Function
    
        ' variables used to restore launched app
        Private origOwner As IntPtr = IntPtr.Zero
        Private app_hWnd As IntPtr = IntPtr.Zero
    
        ' launch app, change parent to Me.
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim p As New Process
            p.StartInfo.FileName = "regedit.exe"
            p.Start()
            p.WaitForInputIdle(-1)
            ' wait upto 5 secs to get MainWindowHandle (not zero).
            For i = 1 To 50
                If Not p.MainWindowHandle = IntPtr.Zero Then
                    Exit For
                End If
                Threading.Thread.Sleep(100)
            Next
            If p.MainWindowHandle = IntPtr.Zero Then
                MessageBox.Show("Unable to get window handle")
            Else
                ' demo limited to one instance, so disable this button!
                Button1.Enabled = False
                ' save handle
                app_hWnd = p.MainWindowHandle
                ' save current parent/set new parent as Me.
                origOwner = SetParent(app_hWnd, Me.Handle)
            End If
        End Sub
    
        Private Sub Form1_FormClosing(sender As Object, e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
            ' restore app to original parent (if still running).
            If Not app_hWnd = IntPtr.Zero Then
                If IsWindow(app_hWnd) Then
                    SetParent(app_hWnd, origOwner)
                End If
            End If
        End Sub
    
    End Class
    Last edited by Edgemeal; Aug 20th, 2012 at 04:29 PM.

Posting Permissions

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



Click Here to Expand Forum to Full Width