Solved

Trouble with 'GetExitCodeProcess'

Posted on 1998-11-05
2
719 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 Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
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…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now