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!

Get process handle and waitforsingleobject

Status
Not open for further replies.

sunaj

Technical User
Feb 13, 2001
1,474
DK
Hi,

I'm using shell to run a program, and I want my VB program to wait until the process is finished. Unfortunately the program is constructed in such way that it opens a new process, so I have to get the processhandle before i can use WaitforSingleObject.

I found some code that Jmarler posted that returns the processID. As I understand it the ProcessID is not the same as the Process Handle And I'm now asking for help to modify the code to return the handle so I can wait for the new process (or any other code that will work).

This is what I got (notepad is just an example of course):
----------------------------------------------------------
Dim hproc As Long, WaitReturn As Long
Shell "notepad"
hproc = GetProcessHandle("notepad.exe")
If hproc <> 0 Then
WaitReturn = WaitForSingleObject(hproc, -1&)
If WaitReturn = WAIT_FAILED Then MsgBox &quot;Wait failed&quot;
CloseHandle hproc
End If
----------------------------------------------------------

But WaitForSingleObject keeps returning WAIT_FAILED
JMarlers code is posted at thread222-69608

Sunaj
 
I've done this just today and it works fine but I used another API then JMarlers did. I'll put another post tomorrow to give you the code I used (I'm at home right now).
There's a lot more options and functionality than the code above.
 
Here's the code, There's also an API to change the default printer in there, if you ever need it.
Tell me if it helps.
-----------------------------------------------------------

Public Declare Function GetProfileString Lib &quot;kernel32&quot; Alias &quot;GetProfileStringA&quot; (ByVal lpAppName As String, ByVal lpKeyName As String, ByVal lpDefault As String, ByVal lpReturnedString As String, ByVal nSize As Long) As Long
Declare Function WriteProfileString Lib &quot;kernel32.dll&quot; Alias &quot;WriteProfileStringA&quot; (ByVal lpszSection As String, ByVal lpszKeyName As String, ByVal lpszString As String) As Long
Public Type SHELLEXECUTEINFO
cbSize As Long
fMask As Long
hwnd As Long
lpVerb As String
lpFile As String
lpParameters As String
lpDirectory As String
nShow As Long
hInstApp As Long
lpIDList As Long
lpClass As String
hkeyClass As Long
dwHotKey As Long
hIcon As Long
hProcess As Long
End Type
Public Const SEE_MASK_NOCLOSEPROCESS = &H40
Public Const SW_SHOWNORMAL = 1
Public Declare Function ShellExecuteEx Lib &quot;shell32.dll&quot; Alias &quot;ShellExecuteExA&quot; (lpExecInfo As _
SHELLEXECUTEINFO) As Long
Public Const SE_ERR_FNF = 2
Public Const SE_ERR_NOASSOC = 31
Public Declare Function WaitForSingleObject Lib &quot;kernel32.dll&quot; (ByVal hHandle As Long, ByVal _
dwMilliseconds As Long) As Long
Public Const INFINITE = &HFFFF
Public Const WAIT_TIMEOUT = &H102


Private Sub Main()
Dim sei As SHELLEXECUTEINFO ' structure used by the function
Dim hprog, hProc, retval, rc As Long
Dim def$
Dim def2$
' Load the information needed to open C:\Docs\readme.txt
' into the structure.
With sei
' Size of the structure
.cbSize = Len(sei)
' Use the optional hProcess element of the structure.
.fMask = SEE_MASK_NOCLOSEPROCESS
' Handle to the window calling this function.
'.hwnd = Form1.hwnd
' The action to perform: open the file.
.lpVerb = &quot;open&quot;
' The file to open.
.lpFile = &quot;C:\program files\olympic\miseproduct.exe&quot;
' No additional parameters are needed here.
.lpParameters = &quot;&quot;
' The default directory -- not really necessary in this case.
'.lpDirectory = &quot;C:\Docs\&quot;
' Simply display the window.
.nShow = SW_SHOWNORMAL
' The other elements of the structure are either not used
' or will be set when the function returns.
End With

' Modify the default printer
def$ = String$(128, 0) 'Sert pour celle par defaut au debut
def2$ = String$(128, 0) 'Sert pour aller chercher la full string de la sato, a cause du port qui change d'un ordi a l'autre
retval = GetProfileString(&quot;windows&quot;, &quot;device&quot;, &quot;&quot;, def$, 127)
retval = GetProfileString(&quot;devices&quot;, &quot;\\Xcd_023594\BINARY_P1&quot;, &quot;&quot;, def2$, 127)
retval = WriteProfileString(&quot;windows&quot;, &quot;device&quot;, &quot;\\Xcd_023594\BINARY_P1,&quot; & def2$)

' Open the file using its associated program.
retval = ShellExecuteEx(sei)
If retval = 0 Then
' The function failed, so report the error. Err.LastDllError
' could also be used instead, if you wish.
Select Case sei.hInstApp
Case SE_ERR_FNF
Debug.Print &quot;Mise en Production n'est pas disponible.&quot;
Case Else
Debug.Print &quot;Erreur inconnue.&quot;
End Select
Else
' Wait for the opened process to close before continuing. Instead
' of waiting once for a time of INFINITE, this example repeatedly checks to see if the
' is still open. This allows the DoEvents VB function to be called, preventing
' our program from appearing to lock up while it waits.
Do
DoEvents
retval = WaitForSingleObject(sei.hProcess, 0)
Loop While retval = WAIT_TIMEOUT
End If

' Put original default printer
retval = WriteProfileString(&quot;windows&quot;, &quot;device&quot;, def$)
End Sub

 
Hi,

Either I don't understand your program or it dosn't do what I want.
I don't want to open a program (like I think your code is designed to do), but I want to look through the running processes, find the one I want and then wait until its finished.

...

Sunaj
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top