Solved

Trouble with 'GetExitCodeProcess'

Posted on 1998-11-05
2
745 Views
Last Modified: 2008-02-01
When using the Shell function in conjuction with the OpenProcess API and the GetExitCodeProcess, the program errors out when the GetExitCodeProcess in encountered.  It also closes out the VB IDE. This was tested in VB5 and VB6 under Windows NT 4.0 with the same results.  I have declared the API's properly.  The function I shelled to is notepad.exe.

Declare statements:
Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess as Long, ByVal bInheritHandle as Long, ByVal dwProcessId as Long) as Long
Declare Function GetExitCodeProcess Lib "kernel32" (ByVal hProcess as Long, ByVal lpExitCode as Long) as Long
Declare Function CloseHandle Lib "kernel32" (ByVal hObject as Long) as Long

Constants:
Private Const PROCESS_QUERY_INFORMATION = &H400&
Private Const STILL_ACTIVE = &H310&

Function:
Public Function WaitOnProgram() as Boolean
  Dim lExit as Long, lProg as Long, lReturnValue as Long
  Dim sShellCommand as String

  sShellCommand="c:\winnt\notepad.exe"
  lProg = OpenProcess(PROCESS_QUERY_INFORMATION, False, Shell(sShellCommand, vbNormalFocus))
  Do
    lReturnValue = GetExitCodeProcess(lProg, lExit)
    DoEvents
    Sleep 1000
  Loop While lExit = STILL_ACTIVE
  CloseHandle lProg
  WaitOnProgram = lExit
End Function

The error is a 'Start:VB5(6).exe - Application Error
The instruction at "0x77f199a9" referenced memory at "0x00000000". The memory could not be "written".
0
Comment
Question by:RhinoDude
2 Comments
 
LVL 13

Accepted Solution

by:
Mirkwood earned 100 total points
ID: 1443358
I would blame the doevents. You are making a recursive call.
try replacing doevents with a timer.
To after OpenProcess, activate a timer
on the timer tick, test the exitcode
when the exitcode is still_activate deactive the timer and close the application.

Much more safe.
0
 

Author Comment

by:RhinoDude
ID: 1443359
After the OpenProcess I enabled the timer.

I changed the do loop to:
Do
  doevents
Loop While ProgramRunning

This enables repaint if necessary.

The timer routine is:
Dim lReturnValue As Long
Dim lExit As Long
     
'get status
lReturnValue = GetExitCodeProcess(lProg, lExit)
Sleep 100

lProg and STILL_ACTIVE were made public.
The Declare for the GetExitCodeProcess was made public.

thanks for the quick response!

0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction While answering a recent question (http://www.experts-exchange.com/Q_27402310.html) in the VB classic zone, I wrote some VB code in the (Office) VBA environment, rather than fire up my older PC.  I didn't post completely correct code o…
Most everyone who has done any programming in VB6 knows that you can do something in code like Debug.Print MyVar and that when the program runs from the IDE, the value of MyVar will be displayed in the Immediate Window. Less well known is Debug.Asse…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

808 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question