We help IT Professionals succeed at work.
Get Started

OpenProcess returns ZERO

moonlight
moonlight asked
on
3,053 Views
Last Modified: 2007-12-19
I don't know about you guys.. this visual basic and I.. we don't match.. This is a totaaaaaal mess :(

Consider this proc, this is directly taken from the help file:

Public Function WaitOnProgram(ByVal idProg As Long, _
    Optional ByVal WaitDead As Boolean) As Long
    Dim cRead As Long, iExit As Long, hProg As Long   ' Get process handle
    hProg = OpenProcess(PROCESS_ALL_ACCESS, False, idProg)
    If WaitDead Then        ' Stop dead until process terminates
        Dim iResult As Long
        iResult = WaitForSingleObject(hProg, INFINITE)

        If iResult = WAIT_FAILED Then
            MsgBox CStr(iResult), 0, "info"
            Err.Raise Err.LastDllError        ' Get the return value
        End If
        GetExitCodeProcess hProg, iExit
    Else        ' Get the return value
        GetExitCodeProcess hProg, iExit        ' Wait, but allow painting and other processing
        Do While iExit = STILL_ACTIVE
            DoEvents
            GetExitCodeProcess hProg, iExit
        Loop
    End If
    CloseHandle hProg
    WaitOnProgram = iExit
End Function

first, if I have option explicit set, I cannot compile it. It doesnt understand PROCESS_ALL_ACCESS and not INFINITE.. If I declare it myself

Public Const PROCESS_ALL_ACCESS = &H1F0FFF

the program crashes in runtime.

if I take away the option, it doesnt do what it is meant to do, no errors no nothing.
Trying to undestand the error:
    If hProg = 0 Then MsgBox "Didnt work :("  yes hProg returned zero, something wrong right?
MsgBox Cstr(GetLastError) oh, but last error is zero too, nothing was obviously wrong!!!

iResult = WaitForSingleObject(hProg, INFINITE) oh and iResult is -1, so it went perfectly ok? well, maybe unerstandable if it doesnt even understand INFINITE !!!
so what did it say? what is -1 ?

        MsgBox CStr(iResult), 0, "iresult"
        MsgBox CStr(STILL_ACTIVE), 0, "still_active value"
        MsgBox CStr(WAIT_ABANDONED), 0, "WAIT_ABANDONED"
        MsgBox CStr(WAIT_OBJECT_0), 0, "WAIT_OBJECT_0"
        MsgBox CStr(WAIT_TIMEOUT), 0, "WAIT_TIMEOUT"
oh of course I just get empty msg boxes...

oh oh dont forget that Do While iExit = STILL_ACTIVE puts you in an infinite loop...
there you go, it understood infitite at last.. Grrrrrrrrrrr...

ok someone help me clear out this mess.. I really like answers to all the questions thank you. But my main question is, How do I make this work?
Comment
Watch Question
Commented:
This problem has been solved!
Unlock 2 Answers and 11 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE