Guidance
指路人
g.yi.org
software / rapidq / Examples / Memory Process Thread Message / FindProcessId.rqb

Register 
注册
Search 搜索
首页 
Home Home
Software
Upload

  
     $OPTIMIZE ON
     $TYPECHECK ON

     CONST NUM_PROCESSES = 1024
     CONST NUM_MODULES   = 1
     CONST NAME_LENGTH   = 256

     CONST NULL  = 0
     CONST FALSE = 0
     CONST TRUE  = 1

     CONST DELETE                          = &H00010000
     CONST READ_CONTROL                    = &H00020000
     CONST WRITE_DAC                       = &H00040000
     CONST WRITE_OWNER                     = &H00080000
     CONST SYNCHRONIZE                     = &H00100000

     CONST STANDARD_RIGHTS_REQUIRED        = &H000F0000

     CONST STANDARD_RIGHTS_READ            = READ_CONTROL
     CONST STANDARD_RIGHTS_WRITE           = READ_CONTROL
     CONST STANDARD_RIGHTS_EXECUTE         = READ_CONTROL

     CONST STANDARD_RIGHTS_ALL             = &H001F0000

     CONST SPECIFIC_RIGHTS_ALL             = &H0000FFFF

     CONST PROCESS_TERMINATE               = &H0001
     CONST PROCESS_CREATE_THREAD           = &H0002
     CONST PROCESS_SET_SESSIONID           = &H0004
     CONST PROCESS_VM_OPERATION            = &H0008
     CONST PROCESS_VM_READ                 = &H0010
     CONST PROCESS_VM_WRITE                = &H0020
     CONST PROCESS_DUP_HANDLE              = &H0040
     CONST PROCESS_CREATE_PROCESS          = &H0080
     CONST PROCESS_SET_QUOTA               = &H0100
     CONST PROCESS_SET_INFORMATION         = &H0200
     CONST PROCESS_QUERY_INFORMATION       = &H0400
     CONST PROCESS_SUSPEND_RESUME          = &H0800
     CONST PROCESS_ALL_ACCESS              = STANDARD_RIGHTS_REQUIRED OR SYNCHRONIZE OR &HFFF

' See the MSDN Documentation at http://shrinkster.com/286
     DECLARE FUNCTION EnumProcesses LIB "psapi" ALIAS "EnumProcesses" (BYVAL arrayPointer AS LONG, BYVAL arraySize AS LONG, BYREF arrayUsed AS LONG) AS LONG
     DECLARE FUNCTION EnumProcessModules LIB "psapi" ALIAS "EnumProcessModules" (BYVAL processHandle AS LONG, BYVAL arrayPointer AS LONG, BYVAL arraySize AS LONG, BYREF arrayUsed AS LONG) AS LONG
     DECLARE FUNCTION GetModuleBaseName LIB "psapi" ALIAS "GetModuleBaseNameA" (BYVAL processHandle AS LONG, BYVAL moduleHandle AS LONG, BYREF name AS STRING, BYVAL nameSize AS LONG) AS LONG
     DECLARE FUNCTION OpenProcess LIB "kernel32" ALIAS "OpenProcess" (desiredAccess AS LONG, inheritHandle AS LONG, pid AS LONG) AS LONG
' See the MSDN Documentation at http://shrinkster.com/3e7
     DECLARE FUNCTION CloseHandle LIB "kernel32" ALIAS "CloseHandle" (BYVAL handle AS LONG) AS LONG
' See the MSDN Documentation at http://shrinkster.com/3ea
     DECLARE FUNCTION GetLastError LIB "kernel32" ALIAS "GetLastError" AS LONG

     DECLARE FUNCTION CreateAPIString(length AS LONG) AS STRING
     DECLARE FUNCTION ReadAPIString(str AS STRING, length AS LONG) AS STRING
     DECLARE FUNCTION ReadNullTermString(str AS STRING) AS STRING

     DECLARE FUNCTION FindProcessId(name AS STRING) AS LONG
     DECLARE SUB PrintProcessInfo(pid AS LONG, text AS STRING)

'########
'# Main #
'########
     SUB Main()
      DEFSTR procName
      PRINT "Enter the process name (with the .exe or .dll)"
      INPUT ">", procName

      DIM procId AS LONG
      procId = FindProcessId(procName)

      IF (procId = -1) THEN
       PRINT procName + " was not found."
      ELSE
       PRINT procName + " has an ID of " + STR$(procID)
      END IF
     END SUB
     Main()

'#############
'# Functions #
'#############
     FUNCTION CreateAPIString(length AS LONG) AS STRING
      Result = SPACE$(length)
     END FUNCTION

     FUNCTION ReadAPIString(str AS STRING, length AS LONG) AS STRING
      DIM realLength AS LONG
      realLength = LEN(str)

      IF length < realLength THEN
       realLength = length
      END IF

      Result = LEFT$(str, realLength)
     END FUNCTION

     FUNCTION ReadNullTermString(str AS STRING) AS STRING
      DEFINT i

      FOR i = 1 TO LEN(str)
       IF (ASC(str[i]) = NULL) THEN
        Result = LTRIM$(RTRIM$(LEFT$(str, i - 1)))
        EXIT FOR
       END IF
      NEXT i
     END FUNCTION

     FUNCTION FindProcessId(name AS STRING) AS LONG
      name = UCASE$(name)
      DIM processes(NUM_PROCESSES) AS LONG, size AS LONG, procUsed AS LONG
      size = NUM_PROCESSES * SIZEOF(LONG)

      EnumProcesses(VARPTR(processes(0)), size, procUsed)

      procUsed = procUsed / SIZEOF(LONG)

      Result = -1

      DEFINT i
      FOR i = 0 TO procUsed - 1
       DIM pid AS LONG, pHandle AS LONG
       pid = processes(i)

       pHandle = OpenProcess(PROCESS_QUERY_INFORMATION OR PROCESS_VM_READ, FALSE, pid)
       IF (pHandle = NULL) THEN
        DIM errorNo AS LONG
        errorNo = GetLastError()
        IF (errorNo == 5) THEN
         PrintProcessInfo(pid, "Access Denied.")
        ELSE
         PrintProcessInfo(pid, "Unknown Error #" + STR$(errorNo))
        END IF
        GOTO LoopEnd
       END IF

       DIM module AS LONG, modSize AS LONG, modUsed AS LONG
       module = 0
       modSize = SIZEOF(LONG)
       modUsed = 0

       IF (EnumProcessModules(pHandle, VARPTR(module), modSize, modUsed) <> NULL) THEN

        DIM modName AS STRING, nameSize AS LONG
        modName = CreateAPIString(NAME_LENGTH)
        nameSize = NAME_LENGTH

        GetModuleBaseName(pHandle, module, modName, nameSize)
        modName = ReadNullTermString(modName)
        PrintProcessInfo(pid, modName)

        CloseHandle(pHandle)

        IF (UCASE$(modName) = name) THEN
         Result = pid
         EXIT FUNCTION
        END IF
       END IF
LoopEnd:
      NEXT i
     END FUNCTION

     SUB PrintProcessInfo(pid AS LONG, text AS STRING)
      PRINT "{" + FORMAT$("%5d", pid) + ", " + text + "}"
     END SUB
掌柜推荐
 
 
¥289.00 ·
 
 
¥1,080.00 ·
 
 
¥1,380.00 ·
 
 
¥638.00 ·
 
 
¥1,290.00 ·
 
 
¥1,420.00 ·
© Sun 2024-11-24  Guidance Laboratory Inc.
Email:webmaster1g.yi.org Hits:0 Last modified:2005-04-16 12:38:31