Announcement

Collapse
No announcement yet.

How to end a process

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to end a process

    Is there a way to end a running process in NT 4/2000 using a
    small PowerBasic program? I need a way to end a running process
    when the need arises without using task manager. Can anyone help?

    Patrick

    ------------------

  • #2
    the actual kill routine would look like this:
    <CODE>

    SUB KillProcess (ProcToKill AS LONG)
    ON ERROR RESUME NEXT
    DIM H AS LONG
    DIM r AS LONG
    PROCESS_TERMINATE% = &H1
    H = OpenProcess(PROCESS_TERMINATE%, 1, ProcToKill)
    r = TerminateProcess(H, 0&)
    CloseHandle H

    END SUB
    </CODE>

    The processID could be passed as a command line parameter or,
    if you want to do it programatically, you could use a routine
    like this:

    <CODE>
    FUNCTION GetProcID () AS LONG

    DIM cb AS DWORD
    DIM cbNeeded AS DWORD
    DIM NumElements AS LONG
    DIM ProcessIDs() AS DWORD
    REDIM ProcessIDs(1024) AS LOCAL DWORD
    DIM cbNeeded2 AS DWORD
    DIM NumElements2 AS LONG
    DIM Modules() AS DWORD
    REDIM Modules(1024) AS LOCAL DWORD
    DIM lRet AS LONG
    DIM ModuleName AS ASCIIZ * 256
    DIM nSize AS LONG
    DIM hProcess AS LONG
    DIM i AS LONG
    DIM dwProcessId AS DWORD
    DIM hMod AS DWORD
    DIM sProgName AS STRING

    'Get the array containing the process id's for each process object
    lRet = EnumProcesses(BYVAL VARPTR(ProcessIDs(0)), 4096, cbNeeded)

    NumElements = cbNeeded / 4

    FOR i = 1 TO NumElements
    dwProcessId = ProcessIDs(i)
    'Get a handle to the Process
    hProcess = OpenProcess(%PROCESS_QUERY_INFORMATION _
    OR %PROCESS_VM_READ, %FALSE, dwProcessID)

    'Got a Process handle
    IF hProcess <> 0 THEN
    'Get an array of the module handles for the specified
    'process
    lRet = EnumProcessModules(hProcess, BYVAL VARPTR(Modules(0)), 4096, _
    cbNeeded2)
    'If the Module Array is retrieved, Get the ModuleFileName
    IF lRet <> 0 THEN
    hMod = Modules(0)
    ModuleName = SPACE$(255)
    nSize = 256
    lRet = GetModuleFileNameEx(hProcess, hMod, _
    ModuleName, nSize)
    sProgName = UCASE$(LEFT$(ModuleName,lRet))
    IF INSTR(sProgName,"PROGTOKILL.EXE") > 0 THEN
    FUNCTION = dwProcessId
    lRet = CloseHandle(hProcess)
    EXIT FUNCTION
    END IF
    END IF
    END IF
    'Close the handle to the process
    NextProc:
    lRet = CloseHandle(hProcess)
    NEXT


    END FUNCTION
    </CODE>

    For this lookup code you'll need the psapi.inc file. I think that can be
    found in the support files area. If not, I have a copy I can
    send/post.


    ------------------
    R.

    Comment


    • #3
      Thanks! I'll try it!

      Patrick

      ------------------

      Comment


      • #4
        Randy:

        I have written the following:

        #COMPILE EXE

        FUNCTION PBMAIN()
        ON ERROR RESUME NEXT
        DIM h AS LONG
        DIM r AS LONG
        process_terminate%=%H1
        h=openprocess(process_terminate%,1,"qlaunch")
        r=terminateprocess(H, 0&)
        closehandle h



        END FUNCTION

        I am not familiar with API calls...I do program in VB but I have never
        learned about calling API calls Do I need the
        psapi.inc file for this? Can you email it to me ([email protected])?
        Also, if you could supply a brief description regarding how to incorporate
        that file into my compiler...I really appreciate the help.

        As it is, the compiler gives me an error on the process_terminate%=&H1 line...

        Thanks Patrick


        ------------------

        Comment


        • #5
          I've e-mailed you the file and some more code ideas.


          ------------------
          R.

          Comment


          • #6
            dang it...it still gives an error on the process_terminate%=&h1

            #COMPILE EXE
            #INCLUDE "win32api.inc"
            #INCLUDE "c:\psapi.inc"

            FUNCTION PBMAIN()
            ON ERROR RESUME NEXT
            DIM h AS LONG
            DIM r AS LONG
            process_terminate%=%H1
            h=openprocess(process_terminate%,1,VAL(COMMAND$))
            r=terminateprocess(H, 0&)
            closehandle h



            END FUNCTION


            error says: error 460...the help defines it as:
            460 Undefined equate - You used a named constant in your
            program without defining it first. Define the named constant
            or use a literal constant in the statement.

            The error occurs on this line:

            process_terminate%=%H1

            Help?! Patrick

            ------------------

            Comment


            • #7
              Try this one, it works for me (Thanks Author, you know who you are hehe)....


              Code:
              '--------------------------------------------------------------------------------
              Function KillProcess(ProcID As Long) As Long
              On Error Resume Next
                  Dim H   As Long
                  Dim r   As Long
                  PROCESS_TERMINATE% = &H1
                  H = OpenProcess(PROCESS_TERMINATE%, 1, ProcID)
                  r = TerminateProcess(H, 0&)
                  CloseHandle H
              End Function
              ------------------
              Scott
              Scott Turchin
              MCSE, MCP+I
              http://www.tngbbs.com
              ----------------------
              True Karate-do is this: that in daily life, one's mind and body be trained and developed in a spirit of humility; and that in critical times, one be devoted utterly to the cause of justice. -Gichin Funakoshi

              Comment


              • #8
                Thanks guys for all the help...the program now compiles
                but doesn't end the process specified on the command line.
                I tried to delete several:

                #COMPILE EXE
                #INCLUDE "win32api.inc"
                #INCLUDE "c:\psapi.inc"

                FUNCTION PBMAIN()
                ON ERROR RESUME NEXT
                DIM h AS LONG
                DIM r AS LONG

                process_terminate%=&H1
                h=openprocess(process_terminate%,1,VAL(COMMAND$))
                r=terminateprocess(H, 0&)
                closehandle h



                END FUNCTION

                I will continue to experiment but any continued help would be appreciated!

                Patrick



                ------------------

                Comment


                • #9
                  I've put the name of a running process right in the appropriate line with no luck:

                  i.e.,
                  h=openprocess(process_terminate%,1,VAL("cam.exe"))

                  and nothing happens when I execute it...

                  Patrick

                  ------------------

                  Comment


                  • #10
                    The last parameter of OpenProcess() is a "Process ID" number stored as a DWORD (or LONG) value. A Process ID is a unique value that is assigned to the application (by Windows) when it is launched. The value of a Process ID bears no relationship to the name of the EXE. Also, a Process ID is not the same as the Process Handle (the OpenProcess() API returns a Process Handle).

                    Check back with the first post that Randy made - it shows you how to enumerate the process ID's of running applications, and you should be able to work it out from there.

                    If you are looking for additional help with the parameter usage of these API functions, be sure to download WIN32.HLP

                    ------------------
                    Lance
                    PowerBASIC Support
                    mailto:[email protected][email protected]</A>
                    Lance
                    mailto:[email protected]

                    Comment


                    • #11
                      OH.....!! It makes sense now! My thanks to everyone for their help!

                      Patrick

                      ------------------

                      Comment

                      Working...
                      X