Solved

Trouble with 'GetExitCodeProcess'

Posted on 1998-11-05
2
750 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: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

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

Suggested Solutions

When designing a form there are several BorderStyles to choose from, all of which can be classified as either 'Fixed' or 'Sizable' and I'd guess that 'Fixed Single' or one of the other fixed types is the most popular choice. I assume it's the most p…
Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
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…

730 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