Re: Getting Handle for a window from itsTask Id returned from shell function.



On 30 Jan 2006 00:28:43 -0800, "itsMainak@xxxxxxxxx"
<itsMainak@xxxxxxxxx> wrote:

>HI,
> I am running FTP command from VB code.
> This FTP command opens a command prompt window.
> I am using Shell function to FTP a file. This is returning a
>process id.
> Now I want to detect from with in my code when this command prompt
>gets closed i.e when the FTP gets complete. So that I can run the code
>which is after that.
> Now I am facing a problem that before FTP gets complete the code
>after it gets executed and I am getting error due to that.
>
>So somebody please help me with a code patch which can detect whether a
>window is open or closed from its process or Task id.

Two examples follow
- one inspects the state of the spawned program
- the other uses WaitForSingleObject


======== EXAMPLE ONE ==========

Option Explicit

Private Declare Function OpenProcess Lib "kernel32" _
(ByVal dwDesiredAccess As Long, _
ByVal bInheritHandle As Long, _
ByVal dwProcessId As Long) As Long

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Private Const SYNCHRONIZE = &H100000
Private Const PROCESS_QUERY_INFORMATION = &H400

Private Declare Function GetExitCodeProcess _
Lib "kernel32" _
(ByVal hProcess As Long, _
lpExitCode As Long) As Long


Dim pID As Long

Sub Form_Load()
Command1.Caption = "Shell"
Command2.Caption = "Get Status"
End Sub

Private Sub Command2_Click()
Dim Ok&, ECode&
Dim hProcess&, Q&

Q = PROCESS_QUERY_INFORMATION Or SYNCHRONIZE
hProcess = OpenProcess(Q, False, pID)
' ---
Ok = GetExitCodeProcess(hProcess, ECode)
MsgBox Str$(Ok) + Str$(ECode)
' ---
If hProcess Then
CloseHandle hProcess
End If

End Sub


Sub Command1_Click()

Dim Style%, Cmd$

' Style = vbHide
Style = vbNormalFocus
Cmd$ = "DIR C:\ /P"

pID = Shell(Environ("Comspec") + " /C " + Cmd, Style)

End Sub

======== EXAMPLE TWO ==========

Option Explicit





Private Type STARTUPINFO
cb As Long
lpReserved As String
lpDesktop As String
lpTitle As String
dwX As Long
dwY As Long
dwXSize As Long
dwYSize As Long
dwXCountChars As Long
dwYCountChars As Long
dwFillAttribute As Long
dwFlags As Long
wShowWindow As Integer
cbReserved2 As Integer
lpReserved2 As Long
hStdInput As Long
hStdOutput As Long
hStdError As Long
End Type

Private Type PROCESS_INFORMATION
hProcess As Long
hThread As Long
dwProcessID As Long
dwThreadID As Long
End Type

Private Declare Function WaitForSingleObject Lib "kernel32"
(ByVal _
hHandle As Long, ByVal dwMilliseconds As Long) As Long

Private Declare Function CreateProcessA Lib "kernel32"
(ByVal _
lpApplicationName As String, ByVal lpCommandLine As
String, ByVal _
lpProcessAttributes As Long, ByVal lpThreadAttributes As
Long, _
ByVal bInheritHandles As Long, ByVal dwCreationFlags As
Long, _
ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As
String, _
lpStartupInfo As STARTUPINFO, lpProcessInformation As _
PROCESS_INFORMATION) As Long

Private Declare Function CloseHandle Lib "kernel32" _
(ByVal hObject As Long) As Long

Private Declare Function GetExitCodeProcess Lib "kernel32" _
(ByVal hProcess As Long, lpExitCode As Long) As Long

Private Const NORMAL_PRIORITY_CLASS = &H20&
Private Const INFINITE = -1&

Public Function ExecCmd(cmdline$)
Dim proc As PROCESS_INFORMATION
Dim start As STARTUPINFO
Dim Ret&

' Initialize the STARTUPINFO structure:
start.cb = Len(start)

' Start the shelled application:
Ret& = CreateProcessA(vbNullString, cmdline$, 0&, 0&, 1&,
_
NORMAL_PRIORITY_CLASS, 0&, vbNullString, start, proc)

' Wait for the shelled application to finish:
Ret& = WaitForSingleObject(proc.hProcess, INFINITE)
Call GetExitCodeProcess(proc.hProcess, Ret&)
Call CloseHandle(proc.hThread)
Call CloseHandle(proc.hProcess)
ExecCmd = Ret&
End Function

Sub Form_Click()
Dim retval As Long
retval = ExecCmd("notepad.exe")
MsgBox "Process Finished, Exit Code " & retval
End Sub


.



Relevant Pages

  • Re: Problem about Window Xp SP2 firewall and the buildin FTP command
    ... Problem about Window Xp SP2 firewall and the buildin FTP ... I find a problem that if running multiple FTP command at the same ... Windows XP SP2 to limit Max Connections/sec ...
    (microsoft.public.windowsxp.general)
  • Problem about Window XP SP2 firewall and the buildin FTP command
    ... Problem about Window Xp SP2 firewall and the buildin FTP ... I find a problem that if running multiple FTP command at the same ... Windows XP SP2 to limit Max Connections/sec ...
    (microsoft.public.windowsxp.perform_maintain)
  • Re: Need Help with code problem Willing to pay for help Please...
    ... you do not have to use FTP. ... programmer helping programmers. ... > other forums we have put this on perl forums because our site uses perl, ... > End Sub ...
    (microsoft.public.dotnet.general)
  • Re: FTP From Excel
    ... I use FTP Pro for the transfer ... The script language is for FTP Pro, ... The last sub which calls the FTP ... Dim TaskID As Long ...
    (microsoft.public.excel.programming)
  • Re: Mainframe to windows and windows to mainframe
    ... needed to prepend the SITE FTP command in order to send the FILETYPE FTP ... from Windows, in other words, what the Windows equivalent to the SITE FTP ... recognition when I spotted QUOTE so this may be the solution. ...
    (bit.listserv.ibm-main)