$OPTIMIZE ON
$TYPECHECK ON
CONST NUM_PROCESSES = 1024
CONST NULL = 0
CONST STARTF_USESHOWWINDOW = &H00000001
CONST STARTF_USESIZE = &H00000002
CONST STARTF_USEPOSITION = &H00000004
CONST STARTF_USECOUNTCHARS = &H00000008
CONST STARTF_USEFILLATTRIBUTE = &H00000010
CONST STARTF_RUNFULLSCREEN = &H00000020
CONST STARTF_FORCEONFEEDBACK = &H00000040
CONST STARTF_FORCEOFFFEEDBACK = &H00000080
CONST STARTF_USESTDHANDLES = &H00000100
CONST SW_HIDE = 0
CONST SW_SHOWNORMAL = 1
CONST SW_NORMAL = 1
CONST SW_SHOWMINIMIZED = 2
CONST SW_SHOWMAXIMIZED = 3
CONST SW_MAXIMIZE = 3
CONST SW_SHOWNOACTIVATE = 4
CONST SW_SHOW = 5
CONST SW_MINIMIZE = 6
CONST SW_SHOWMINNOACTIVE = 7
CONST SW_SHOWNA = 8
CONST SW_RESTORE = 9
CONST SW_SHOWDEFAULT = 10
CONST SW_FORCEMINIMIZE = 11
CONST SW_MAX = 11
TYPE STARTUPINFO
cb AS LONG
lpReserved AS LONG
lpDesktop AS LONG
lpTitle AS LONG
dwX AS LONG
dwY AS LONG
dwWidth AS LONG
dwHeight AS LONG
dwCharWidth AS LONG
dwCharHeight AS LONG
dwFillAttribs AS LONG
dwFlags AS LONG
wShowWindow AS LONG
cbReserved2 AS LONG
lpReserved2 AS LONG
hStdInput AS LONG
hStdOutput AS LONG
hStdError AS LONG
END TYPE
TYPE PROCESS_INFORMATION
hProcess AS LONG
hThread AS LONG
dwPID AS LONG
dwTID AS LONG
END TYPE
DECLARE FUNCTION CreateProcess LIB "kernel32" ALIAS "CreateProcessA" (BYREF applicationName AS STRING, BYREF commandLine AS STRING, processAttribs AS LONG, threadAttribs AS LONG, inheritHandles AS LONG, flags AS LONG, enviroment AS LONG, BYREF currentDirectory AS STRING, lpStartupInfoStruct AS LONG, lpProcessInformationStruct AS LONG) AS LONG
DECLARE FUNCTION EnumProcesses LIB "psapi" ALIAS "EnumProcesses" (arrayPointer AS LONG, arraySize AS LONG, BYREF arrayUsed AS LONG) AS LONG
DECLARE FUNCTION ProcessExists(pid AS LONG) AS INTEGER
DIM si AS STARTUPINFO
WITH si
.cb = SIZEOF(si)
.lpReserved = NULL
.lpDesktop = NULL
.lpTitle = NULL
.dwX = 0
.dwY = 0
.dwWidth = 0
.dwHeight = 0
.dwCharWidth = 0
.dwCharHeight = 0
.dwFillAttribs = 0
.dwFlags = STARTF_USESHOWWINDOW
.wShowWindow = SW_MINIMIZE
.cbReserved2 = 0
.lpReserved2 = NULL
.hStdInput = NULL
.hStdOutput = NULL
.hStdError = NULL
END WITH
DIM pi AS PROCESS_INFORMATION
DIM siMem AS QMEMORYSTREAM, piMem AS QMEMORYSTREAM
siMem.WriteUDT(si)
piMem.WriteUDT(pi)
DEFSTR appName = "", cmdLine = "notepad.exe", curDir = ""
CreateProcess(appName, cmdLine, NULL, NULL, 0, 0, NULL, curDir, siMem.Pointer, piMem.Pointer)
piMem.Position = 0
piMem.ReadUDT(pi)
siMem.CLOSE
piMem.CLOSE
PRINT "Notepad is running as " + STR$(pi.dwPID)
WHILE (ProcessExists(pi.dwPID) = 1)
SLEEP .1
WEND
PRINT "Notepad is no longer running..."
FUNCTION ProcessExists(pid AS LONG) AS INTEGER
DIM processes(NUM_PROCESSES) AS LONG, size AS LONG, used AS LONG
size = = NUM_PROCESSES * SIZEOF(LONG)
EnumProcesses(VARPTR(processes(0)), size, used)
used = used / SIZEOF(LONG)
Result = 0
DEFINT i
FOR i = 0 TO used
IF (processes(i) = pid) THEN
Result = 1
EXIT FUNCTION
END IF
NEXT i
END FUNCTION
|