Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HANDLE question

Status
Not open for further replies.

OraMs

Programmer
Feb 12, 2000
40
US
Thanks in advance for your time. I'd like to use the WAITFORSINGLEOBJECT API to have VB pause until user logs into a shelled application. According to what I've read in this forum I need to:

SHELL application
OPENPROCESS using shelled application's pid
WAITFORSINGLEOBJECT using handle from OPENPROCESS
CLOSEHANDLE
etc

Since I need the system to pause during the login process , I attempted to use handle for the login window (I get it using GETWINDOW in a loop.) However, VB does not wait :-(. I thought since I already have the login handle, I do not need to OPENPROCESS:)I !!. Why is the WAITFORSINGLEOBJECT not pausing the VB:-( ? What am I missingX-) ? Here's sample code:
Code:
Function SomeProcess() As Long
...
varLogName = "Login"

'get login window handle
CurrWndHndl = CheckApp(TaskName, varLogName)

'check results
If CurrWndHndl = 0 Then
    MsgBox "Login cancelled.", vbOKOnly, ProcessName
    Exit Function
Else
    'now wait until user has logged in
    lngRetVal = WaitForSingleObject(CurrWndHndl, INFINITE)
End If
...
End Function

Function CheckApp(TaskName As String, ProcessName As String) As Long
    ...
[b]NOTE: [/b]
Here I get the handle using the window (caption) name. This is working fine as I use the handle at other times in my program without any problems. 
    ...
    CheckApp = CurrWndHndl
End Function
 
Handle is a generic term. The particular handle that WaitForSingleProcess requires is a handle to a process or thread, not the handle to a window (which is what you are trying to use). And one of the easiest ways to get that process/thread handle is to use OpenProcess as per what you have previously read in this forum.

Have fun.
 
OK. I got the PID (GETWINDOWTHREADID) for the window and used it to get the handle (OPENPROCESS). Now, the WAITFORSINGLEOBJECT works too well. Although I close the PID and handle, the API pauses until I get totally out of the shelled application. What am I still doing wrong?

Code:
'get login window handle
CurrWndHndl = CheckApp(TaskName, varLogName)
            
'check results
If CurrWndHndl = 0 Then
   MsgBox "Login cancelled.", vbOKOnly, ProcessName
   Exit Function
End If
            
'get process id
lngRetVal = GetWindowThreadProcessId(CurrWndHndl, lngProcID)
            
'open process
LoginWndHndl = OpenProcess(SYNCHRONIZE, False, lngProcID)
            
'call error handler
bolErr = ErrHndlr("OpenProcess")
        
'check results
If bolErr Then
   Exit Function
End If

'now wait until user has logged in
lngRetVal = WaitForSingleObject(LoginWndHndl, INFINITE)
            
'now close process
lngRetVal = CloseHandle(lngProcID)
            
'call error handler
bolErr = ErrHndlr("CloseProcess")
        
'check results
If bolErr Then
   Exit Function
End If
        
'now close handle
lngRetVal = CloseHandle(LoginWndHndl)
            
'call error handler
bolErr = ErrHndlr("CloseHandle")
        
'check results
If bolErr Then
   Exit Function
End If

 
You are doing nothing wrong per se. What you are doing is waiting for the process that opened the window to finish. Which is almost certainly the root process for the entire shelled app. So things are behaving the way they are supposed to (as far as the API calls are concerned).

What you really want to do is wait for the particular window's thread to end. Unfortunatley the Win32 API does not provide a way to get the thread handle from the thread identifier (the thread ID being what is actually returned by the GetWindowThreadProcessID call).
 
(having said that, I believe that W2000 has an OpenThread call, but none of the earlier Win32 platforms do)
 
B-( Thanks. I guess I have to use some type of sleep/wait command...

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top