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 Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Shell function issue - incorrect process id returned

Status
Not open for further replies.

leew

Programmer
Joined
Dec 19, 2001
Messages
2
Location
US
All,

Background:
I have developed an application that retrieves downloaded files and send them to another program via the Shell function. The app calls Shell, retrieves a process id, and then stores that process id in a database. Later the app goes out and checks to see if the process id is still out there. If not, I know that the process has ended. This method allows me to send many files at once.

Problem:
I have noticed on occasion that the Shell function will return a process id that does not correspond to the program I am expecting. For instance, I may find that one of the process ids I received corresponds to 'Explorer.exe'. As a results my app thinks the program I Shelled is still running. Has any one ever seen this? Is there a way to prevent it? Is it possible that someone is running some other program at the exact same time I Shell, therefore causing me to get the wrong process id?

Code:
'//This is the code I use with Shell, sProgram represents the program I am calling.
Dim lTaskId As Long
lTaskId = Shell(sProgram)

'//This is the code I use to check if the process is still active.
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

Public Function IsProcessOpen(ByVal lTaskId As Long, Optional ByVal lMsecs As Long = 0) As Boolean
'Test to see if a process is still running by examining the process handle after attempting to open the process. If the value is 0, the process has either failed or completed.

Dim lProcHandle As Long

'Get the process handle.
lProcHandle = OpenProcess(&H100000, True, lTaskId)

'Close the handle.
CloseHandle lProcHandle

If lProcHandle <> 0 Then IsProcessOpen = True

End Function

Thanks for your input.
--Lee
 
You can test the title (caption) of the window... I've had to do this for the EXACT same problem you are having.

Decalare Function GetWindowText& Lib &quot;user32&quot; Alias &quot;GetWindowTextA&quot; (ByVal hwnd as Long, ByVal lpString as String, ByVal cch as Long)

When using this function, you have to pre-define the string to be loaded with the window text... cch+1 I think it is.

For fun and games you can mess with the counterpart to this... SetWindowText... he he he. Put messages on captions on someone's computer.

A few years ago we played a trick on some guys that &quot;worked&quot; in the test lab at a major petrochemical company in Houston.

He wanted an little chat program to BS with his friends, so in my spare time I wrote him one. But nested in that little chat program was a series of jokes I could pay on the chat users. Like changing their icon sizes and changing their backgrounds and reversing their mouse buttons. Darn that was funny. They never figured out what was up... lol Tuna - It's fat free until you add the Mayo!
 
Oh yeah, if you need it, GetWindowTextLength will tell you the length of the title caption not including the null character at the end. ie. cch+1 from GetWindowText i told you about above.

Declare Function GetWindowTextLength& Lib &quot;user32&quot; Alias &quot;GetWindowTextLengthA&quot; (ByVal hwnd as Long)

Tuna - It's fat free until you add the Mayo!
 
Thanks for your info Tuna, after further reflection I think I have figured out the cause of my problem. Since the programs I Shell sometimes complete and shut down very fast, if other users are on the server and start up programs while my application is running the process id I stored in the database may be reused for another program.

For instance, (1) I Shell a program, (2) I receive the process id 3314, (3) the program completes its' task and ends, (4) a user logs on to the server and opens Explorer, (5) Explorer is now assigned the process id 3314, (6) my application checks to see if the program it Shelled earlier is still running and receives a positive response.

This explanation also explains why I encounter the problem infrequently.

As a solution, I can either use Tuna's suggestion or simply ignore the issue, since eventually the user will log out and their apps will terminate (at least on the Production server). The second solution would not be an option if I relied on the response for further activity (which I do not).

--Lee
 
I was going to ask if the process was ending but I assumed you thought of that (or it wasn't the case). Either way, I'm glad you worked it out.
Tuna - It's fat free until you add the Mayo!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top