Solved

Trouble with 'GetExitCodeProcess'

Posted on 1998-11-05
2
739 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
DIR issue 7 54
SSRS expression Issue finding a string 10 79
MS Access Product Inventory Movement Transfer Form With Separate Records Combined Into one Form 55 78
MsgBox 4 61
When trying to find the cause of a problem in VBA or VB6 it's often valuable to know what procedures were executed prior to the error. You can use the Call Stack for that but it is often inadequate because it may show procedures you aren't intereste…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

773 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