Results 1 to 4 of 4

Thread: StartAndFind - Allows you to get a hwnd after opening

  1. #1

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    StartAndFind - Allows you to get a hwnd after opening

    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         MessageBox.Show(StartAndFind("C:\WINDOWS\system32\notepad.exe").ToString)
    3.     End Sub
    4.  
    5.     Private Function StartAndFind(ByVal Path As String) As Int32
    6.         Dim Info As New ProcessStartInfo
    7.         With Info
    8.             .WorkingDirectory = IO.Path.GetDirectoryName(Path)
    9.             .FileName = Path
    10.         End With
    11.         Process.Start(Info)
    12.         For i As Int32 = 0 To 100
    13.             My.Application.DoEvents()
    14.         Next
    15.         For Each tProc As Process In Process.GetProcessesByName(IO.Path.GetFileNameWithoutExtension(Path))
    16.             If (tProc.MainModule.FileName) = Path AndAlso (tProc.StartTime.ToShortTimeString() = Date.Now.ToShortTimeString()) Then
    17.                 Return tProc.MainWindowHandle.ToInt32
    18.             End If
    19.         Next
    20.         Return 0
    21.     End Function

    Just pass a path to the function, and it will open it and return the handle of the newly opening program.

  2. #2
    "The" RedHeadedLefty
    Join Date
    Aug 2005
    Location
    College Station, TX Preferred Nickname: Gig Current Mood: Just Peachy Turnons: String Manipulation
    Posts
    4,495

    Re: StartAndFind - Allows you to get a hwnd after opening

    You can probably do away with the DoEvents Loop using Process.WaitForInputIdle. The code execution in the prog should resume as soon as the program that is being opened is at the idle state.

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    105,467

    Re: StartAndFind - Allows you to get a hwnd after opening

    You also don't need the last loop either. Process.Start returns a Process object.
    VB Code:
    1. Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    2.         MessageBox.Show(StartAndFind("C:\WINDOWS\system32\notepad.exe").ToString)
    3.     End Sub
    4.  
    5.     Private Function StartAndFind(ByVal Path As String) As Int32
    6.         Dim Info As New ProcessStartInfo(Path)
    7.        
    8.         Info.WorkingDirectory = IO.Path.GetDirectoryName(Path)
    9.  
    10.         Dim proc As Process = Process.Start(Info)
    11.  
    12.         proc.WaitForInputIdle()
    13.         Return proc.MainWindowHandle.ToInt32()
    14.     End Function

  4. #4

    Thread Starter
    Admodistrator |2eM!x's Avatar
    Join Date
    Jan 2005
    Posts
    3,900

    Re: StartAndFind - Allows you to get a hwnd after opening

    Nice! Thanks for the info!

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