CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 8 of 8

Thread: CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not

  1. #1
    Join Date
    Jan 2013
    Posts
    4

    CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not

    I need immediate help to launch iexplore using CreateProcess, then through next command button pinfo.dwThreadId to find Active or not

    VB form has 3 command buttons

    first command button
    lngRetValue = CreateProcess(vbNullString, "c:\program files (x86)\internet explorer\iexplore.exe", sec1, sec2, False, pclass, 0&, "C:\", sinfo, pinfo)
    hnd = pinfo.dwThreadId

    It launching a new iexplore only when existing iexplore is there and I'm getting dwthreadid but when I transfer hwnd to threadid always shows same wrong id.
    ThreadID = hnd
    lhWnd = FindWindowEx(AddressOf EnumThreadWndActive, lhWnd, "IEFrame", vbNullString)
    pid = GetWindowThreadProcessId(hwnd, lpId)

    and tried with below code also

    If GetWindowThreadProcessId(lhWnd, tId) <> 0 Then 'Get the window's thread id
    If tId = pid Then InstanceToWnd = hwnd: ' If pid matches then exit
    End If

    Expected the foll.functionality:
    I wanted to open iexplore and when ever ipress the button next next iexplore should be open with different threadid using createprocess.

    In next command button based on that threadid I wanted to check whether iexplore window is active or not and another 3rd command button to close the particular Active iexplore based on threadid.

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not

    FindWindowEx returns after it has found out the first window having given class name and title. So it may and may not be the one you are looking for.
    To find out all the windows having given class name and title you could use EnumWindows API
    Victor Nijegorodov

  3. #3
    Join Date
    Jan 2013
    Posts
    4

    Re: CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not

    Please can you explain based on the foll.code. cmdlaunch to launching iexplore and getting dwThreadId but how do i connect this with GetWindowThreadProcessId->threadid beacuse I am not getting 'active' status everytime,one time it says 'Active' and next time 'Not Active' and when try to close the window it's not getting closing the particular window. I used ->[If lhWnd Then PostMessage lhWnd, WM_CLOSE, 0&, 0&].
    Code:
    Private Sub cmdLaunch_Click()
     sec1.nLength = Len(sec1)
        sec2.nLength = Len(sec2)
        sinfo.cb = Len(sinfo)
        sinfo.dwFlags = &H1
        sinfo.wShowWindow = 1
        pclass = &H80
    
        lngRetValue = CreateProcess(vbNullString, "c:\program files (x86)\internet explorer\iexplore.exe", sec1, sec2, False, pclass, 0&, "C:\", sinfo, pinfo)
        hnd = pinfo.dwThreadId
        Debug.Print "dwThreadId ->" & CStr(hnd)
    End Sub
    
    Private Sub cmdStatus_Click()
        Dim ThreadID As Long
        Dim lProcessId As Long
        Dim lthreadid As Long
        ThreadID = hnd
       
        On Error Resume Next
        appStatus = False
        
        lthreadid = GetWindowThreadProcessId(hWnd, lProcessId)
        lCurrentThreadId = lthreadid
        Debug.Print "Thread ID ->" & CStr(lthreadid)
        Call fEnumWindows(lthreadid)
    
        If appStatus Then
            MsgBox "Active"
        Else
            MsgBox "Not active"
        End If
    End Sub
    
    Public Function fEnumWindows(lcurrentThread As Long) As Boolean
    Dim hWnd As Long
    Dim lthreadid As Long
    fEnumWindows = False
    
    Call EnumWindows(AddressOf fEnumWindowsCallBack, lcurrentThread)
    fEnumWindows = True
    End Function
    Public Function fEnumWindowsCallBack(ByVal hWnd As Long, ByVal lpData As Long) As Long
    Dim lResult    As Long
    Dim lthreadid  As Long
    Dim lProcessId As Long
    Dim sWndName   As String
    Dim sClassName As String
    
    
    fEnumWindowsCallBack = 0
    lthreadid = GetWindowThreadProcessId(hWnd, lProcessId)
    Debug.Print "Enum Processid" & lProcessId
    If lthreadid = lpData Then
        Debug.Print "Enum " & lthreadid
        fEnumWindowsCallBack = lthreadid
        appStatus = True
        Exit Function
    End If
    
    End Function
    Last edited by DataMiser; January 30th, 2013 at 08:08 PM. Reason: added code tags for readability

  4. #4
    Join Date
    Jan 2013
    Posts
    4

    Re: CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not

    Hello! Help Please!
    VB Form and Module has below codes->It's working fine but not closing the exact "IExplore". createprocess dwThreadId not matching with lhWnd = FindWindowEx(0&, lhWnd, "IEFrame", vbNullString)
    lthreadid = GetWindowThreadProcessId(lhWnd, lProcessId)

    and tried with Call EnumWindows(AddressOf EnumCallback, hWnd) also but I couldn't match dwthreadid with GetWindowThreadProcessId processId. I don't have any problem with notepad but Iexplore instance threadid totally not matching.
    So that I couldn't test whether it is Active or Not.


    lngRetValue = CreateProcess(vbNullString, "c:\program files (x86)\internet explorer\iexplore.exe", sec1, sec2, False, pclass, ByVal 0&, "C:\", sinfo, pinfo)

    hnd = pinfo.dwThreadId
    pnd = pinfo.dwProcessID

    lProcessId = GetWindowThreadProcessId(lhWnd, lProcessId)

    pnd and lProcessId never matching. Help please.

    Expected Functionality.
    1. Open iexplore instance with createprocess--Working
    2. In next 'Status' command button to display the Status of iexplore ThreadId [Whether is active or not] - Not working with exact threadid. now just trying with Iframe
    3. 'Close' Command button to close that particular Iexplore based on threadid.
    Last edited by lalithaecw; February 1st, 2013 at 08:01 AM.

  5. #5
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not

    Please, stop posting unformatted code. It is very hars to read/understand.

    Second, please, explain what you are trying to achieve? Please, provide a complete explanation of your goal, not just "how do i connect this with GetWindowThreadProcessId->threadid" words!

    Third, why do you need a thread ID, not a process ID?

    Besides, my VB knowledge is very poor, but in VC++ I'd do the following to obtain the window handle of the process spawned by CreateProcess API:
    1. call CreateProcess and be sure it returns TRUE
    2. obtain process handle from the PROCESS_INFORMATION structure
    3. call WaitForInputIdle to wait until the specified process is waiting for user input (to be sure its main window has been created)
    4. enumerate the windows using EnumWindows and passing Process Id as LPARAM; in EnumWindowsProc call GetWindowThreadProcessId and compare the vaue of the process id with the LPARAM.
    5. if the process ID will be the same as the one of your spawned application then stop enumeration and save the window handle.


    Also see this KB
    Victor Nijegorodov

  6. #6
    Join Date
    Jan 2013
    Posts
    4

    Re: CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not

    Expected Functionality.
    1. Open iexplore instance with createprocess--Working
    2. In next 'Status' command button to display the Status of iexplore ThreadId [Whether is active or not] - Not working with exact threadid. now just trying with Iframe
    3. 'Close' Command button to close that particular Iexplore based on threadid. ->Now Closing all Internet explore or Explicitly setting threadid. Pls. see the below code

    lthreadid = GetWindowThreadProcessId(lhWnd, lProcessId)
    lCurrentThreadId = lthreadid
    Based on lcurrentthreaid and checking active status and closing the particular iexplore. but I could not match the pinfo.dwthreadid with hwnd threadid. If tried the same flow with processId also still not showing matching value.Help please.
    Last edited by lalithaecw; February 1st, 2013 at 08:11 AM.

  7. #7
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,517

    Re: CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not

    Quote Originally Posted by lalithaecw View Post
    Expected Functionality.
    ...
    2. In next 'Status' command button to display the Status of iexplore ThreadId [Whether is active or not] - Not working with exact threadid. now just trying with Iframe
    Define "Not working"
    Victor Nijegorodov

  8. #8
    Join Date
    Dec 2001
    Posts
    6,332

    Re: CreateProcess iexplore -getting dwThreadId.how to check status 'Active' or not

    Have you tried using GetActiveWindow or GetForegroundWindow? Also, have you considered ShellExecute or ShellExecuteEx to open a new iexplorer window? Seems to me you could use the window handle, and don't really need the ThreadID at all.
    Please remember to rate the posts and threads that you find useful.
    How can something be both new and improved at the same time?

Posting Permissions

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


Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center