3 Replies - 4125 Views - Last Post: 15 September 2011 - 06:49 AM Rate Topic: -----

#1 Tehdead   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 16-June 09

Can't SetParent after starting a process...

Posted 14 September 2011 - 07:21 PM

Ok so I've been struggling for a few hours now,

 Private tHandle As HandleRef
 Private tPlacement As xWinPlacement

 Private Sub Mainfrm_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim tProcess As Process
        Dim tIn32 As Int32

        'Check if already running
        If Not Process.GetProcessesByName("Target").Count = 0 Then

            'Get the first instance of Target
            tProcess = Process.GetProcessesByName("Target").First

            'Store the Process Object an the MainWindow handle in a HandleRef
            tHandle = New HandleRef(tProcess, tProcess.MainWindowHandle)

        Else

            'If no process is found then
            If MsgBox("Target must be running!" & vbCrLf & vbCrLf & "Would you like to start Target?", MsgBoxStyle.YesNo) = vbYes Then


                'Start target from appdata
                Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Loader.exe")

                'Wait for the process to finsh loading
                Do While Process.GetProcessesByName("Target").Count = 0
                    xSleep(10)
                Loop

                'Get the first instance of Target
                tProcess = Process.GetProcessesByName("Target").First

                Do Until tProcess.MainWindowHandle.ToInt32 > 0
                    xSleep(10)
                Loop

                xSleep(15000)

                'Store the Process Object an the MainWindow handle in a HandleRef
                tHandle = New HandleRef(tProcess, tProcess.MainWindowHandle)

            Else
                Me.Close()
            End If
        End If

        CaptureWindow()

    End Sub

    Private Sub CaptureWindow()

        tPlacement = xGetWinPlacement(tHandle)
        Me.Location = tPlacement.normalPosition.Location
        
        SetParent(tHandle.Handle, Me.Handle)

        MsgBox(ErrorToString(Err.LastDllError))

        tPlacement = xGetWinPlacement(tHandle)
        xSetWindowPos(tHandle, xhWnd.Top, 0, 100, tPlacement.normalPosition.Width, tPlacement.normalPosition.Height, xSWP_Flags.ShowWindow)

    End Sub



Alright so I want to set the parent of my target process to my main form. This works perfectly as long as the target was started before my program.

When I close the target an try to start the loader with my program an capture the window that way, SetParent errors saying "Application-defined or Object-Defined Error". Even though everything is nearly the same between the "Not running" and "Running" conditions.

My only thought was that the Target needed to finish loading, at first there was only a check to see if the GetprocessesbyName("target").count was greater then 0. Quick check on the MainWindowHandle proved that if it was going to quickly the MainWindowHandle would return 0.

I added the second check thinking it should be good after it gets the MainWindowHandle, this still wasn't the case.

Out of frustration I added a 15 sec buffer which combined with both checks doesn't attempt to capture the window till well after the Target program is done loading an has a chance to be idle for a few seconds.

Again this still didn't help my situation, running out of ideas my self I figured I'd make my first question post in the VB forums, in the 5 years I've been programing for my personal use I can say half or more of what I know is from source code from this very site.

Any thoughts are greatly appreciated.... note that's quite possible that I'm just missing something or something I have no clue about since I've never taken a VB class or any other programming language or gone to college for that matter(maybe at somepoint... hopefully... maybe for C++).

Is This A Good Question/Topic? 0
  • +

Replies To: Can't SetParent after starting a process...

#2 _HAWK_   User is offline

  • Master(Of Foo)
  • member icon

Reputation: 1162
  • View blog
  • Posts: 4,444
  • Joined: 02-July 08

Re: Can't SetParent after starting a process...

Posted 14 September 2011 - 07:48 PM

It must be the way those programs start. There are several programs I have tried that do that. Some work if you use Process.WaitForInputIdle , but not all.

This post has been edited by hawkvalley1: 14 September 2011 - 07:49 PM

Was This Post Helpful? 0
  • +
  • -

#3 Tehdead   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 16-June 09

Re: Can't SetParent after starting a process...

Posted 14 September 2011 - 09:59 PM

No luck having tried now another couple hours, I try .WaitForInputIdle but it still didn't work, tried using Shell() didn't work either, I tried creating a new form and setting the childwindow to that handle again still nothing.

The things that I find weird are the fact that once I close the program and rerun it it captures the window fine without me restarting the target program...

I'm so tired right now I can't even think of what to try next, it's got my headed wrapped just in the fact that I'm starting the Loader.

either the Loader is holding the
Target hostage when its started by another program or and mor likly I need to start it with some other permissions, ill take a look in the morning.

This post has been edited by Tehdead: 14 September 2011 - 10:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 Tehdead   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 16-June 09

Re: Can't SetParent after starting a process...

Posted 15 September 2011 - 06:49 AM

Well I figured out a work around by Restarting my program after the program has loaded using Application.Restart.

'Start target from appdata
	               Process.Start(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\Loader.exe")
	 
	               'Wait for the process to finsh loading
	               Do While Process.GetProcessesByName("Target").Count = 0
	                   xSleep(10)
	               Loop
	 
	               'Get the first instance of Target
	               tProcess = Process.GetProcessesByName("Target").First
	 
	               Do Until tProcess.MainWindowHandle.ToInt32 > 0
	                   xSleep(10)
	               Loop
	 
	               xSleep(10000)

                Application.Restart()
	 



I still find it quite interesting as why I'm forced to restart, an if anyone that browses this has a clue or some knowledge of what might be happening in the background on this I'd be very interested.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1