Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

win32api. ProcessIDs and exit codes

Options
  • 12-10-2001 11:30am
    #1
    Registered Users Posts: 7,468 ✭✭✭


    Folks,

    I'm using ShellExecute to print files from vb code on w2k pro/NT 4. What I need to know is when this process has finished. What I'm trying to do is monitor this process for it's ExitCode. Which isn't working so. Pretty much what I'm trying to do is loop through all the processes after I've made this call and try to match their parent id with the current processid (my vb app):

    I'm already trying option a but I can't find the parentids. I'm using the following api call to find the parentid of all current processes:
    Private Declare Function NtQueryInformationProcess _
        Lib "ntdll" (ByVal ProcessHandle As Long, _
        ByVal ProcessInformationClass As Long, _
        ByRef ProcessInformation As PROCESS_BASIC_INFORMATION, _
        ByVal lProcessInformationLength As Long, _
        ByRef lReturnLength As Long) As Long
    

    the PROCESS_BASIC_INFORMATION is the following Type:
    Public Type PROCESS_BASIC_INFORMATION
        ExitStatus As Long
        PebBaseAddress As Long
        AffinityMask As Long
        BasePriority As Long
        UniqueProcessId As Long
        InheritedFromUniqueProcessId As Long '// ParentProcessID
    End Type
    

    And here's the loop to get the parentprocessid
    For l = 1 To (lNeeded / 4)
            '// Get the ShellExecute process id
            '// This checks the Process information
            '// for each active process and compares
            '// it's parent process id to the lCurrid
            
            lProcessBasicInfo = 0 '//0 = REQUEST PROCESS INFORMATION FLAG
            lProcessInformationLength = Len(tProcessInformation)
            lResult = NtQueryInformationProcess(lCurrid, lProcessBasicInfo, tProcessInformation, lProcessInformationLength, lReturnLength)
            If (lCurrid& = tProcessInformation.InheritedFromUniqueProcessId&) Then
                lProcid& = tProcessInformation.UniqueProcessId
                Exit For
            End If
        Next
    

    But the ParentProcessID is always returned as a 0. So when I cant use the GetExitCodeProcess api call. Has anybody tried this before?


Comments

  • Registered Users Posts: 2,494 ✭✭✭kayos


    Private Type STARTUPINFO
      cb As Long
      lpReserved As String
      lpDesktop As String
      lpTitle As String
      dwX As Long
      dwY As Long
      dwXSize As Long
      dwYSize As Long
      dwXCountChars As Long
      dwYCountChars As Long
      dwFillAttribute As Long
      dwFlags As Long
      wShowWindow As Integer
      cbReserved2 As Integer
      lpReserved2 As Long
      hStdInput As Long
      hStdOutput As Long
      hStdError As Long
    End Type
    
    Private Type PROCESS_INFORMATION
      hProcess As Long
      hThread As Long
      dwProcessID As Long
      dwThreadID As Long
    End Type
    
    Private Declare Function WaitForSingleObject Lib "kernel32" (ByVal _
             hHandle As Long, ByVal dwMilliseconds As Long) As Long
    
    Private Declare Function CreateProcessA Lib "kernel32" (ByVal _
             lpApplicationName As Long, ByVal lpCommandLine As String, ByVal _
             lpProcessAttributes As Long, ByVal lpThreadAttributes As Long, _
             ByVal bInheritHandles As Long, ByVal dwCreationFlags As Long, _
             ByVal lpEnvironment As Long, ByVal lpCurrentDirectory As Long, _
             lpStartupInfo As STARTUPINFO, lpProcessInformation As _
             PROCESS_INFORMATION) As Long
    
    Private Declare Function CloseHandle Lib "kernel32" _
             (ByVal hObject As Long) As Long
    
    Private Declare Function GetExitCodeProcess Lib "kernel32" _
             (ByVal hProcess As Long, lpExitCode As Long) As Long
    
    Private Const NORMAL_PRIORITY_CLASS = &H20&
    
    Private Const INFINITE = -1&
    
    Private Function ExecCmd(cmdline$)
    Dim proc As PROCESS_INFORMATION
    Dim start As STARTUPINFO
    Dim ret&
             ' Initialize the STARTUPINFO structure:
             start.cb = Len(start)
             ' Start the shelled application:
      ret& = CreateProcessA(0&, cmdline$, 0&, 0&, 1&, _
                            NORMAL_PRIORITY_CLASS, 0&, 0&, start, proc)
             ' Wait for the shelled application to finish:
      ret& = WaitForSingleObject(proc.hProcess, INFINITE)
      Call GetExitCodeProcess(proc.hProcess, ret&)
      Call CloseHandle(proc.hProcess)
      ExecCmd = ret&
    End Function
    
    


    I use this to shell off a command wait for it to return so if you can afford to do it that way you'll get you exit code....

    [edit]
    Code tags didn't want to seem to keep the formatting
    [/edit]


  • Registered Users Posts: 7,468 ✭✭✭Evil Phil


    Oky doky that seems to be working. Now, any idea what I should use for cmdline$. I'm trying to print so far I'm using
    Print C:\stuff\Filename.txt
    

    which is opening up cmd prompt window but then doesn't do anything. Should I include the device name or will windows use the default printer?


Advertisement