| 1 |
|
|---|
| 2 | /*
|
|---|
| 3 | *@@sourcefile procstat.c:
|
|---|
| 4 | * functions for querying process information.
|
|---|
| 5 | * This is an easy-to-use interface to the
|
|---|
| 6 | * messy 16-bit DosQProcStatus function.
|
|---|
| 7 | *
|
|---|
| 8 | * Usage: All OS/2 programs.
|
|---|
| 9 | *
|
|---|
| 10 | * Function prefixes (new with V0.81):
|
|---|
| 11 | * -- prc* Query Process helper functions
|
|---|
| 12 | *
|
|---|
| 13 | * Based on Kai Uwe Rommel's "dosqproc" package
|
|---|
| 14 | * available at Hobbes:
|
|---|
| 15 | * Kai Uwe Rommel - Wed 25-Mar-1992
|
|---|
| 16 | * Sat 13-Aug-1994
|
|---|
| 17 | *
|
|---|
| 18 | * Note: If you link against procstat.obj, you
|
|---|
| 19 | * need to import the following in your .DEF file:
|
|---|
| 20 | *
|
|---|
| 21 | + IMPORTS
|
|---|
| 22 | + DOSQPROCSTATUS = DOSCALLS.154
|
|---|
| 23 | + DosQuerySysState = DOSCALLS.368
|
|---|
| 24 | *
|
|---|
| 25 | * or linking will fail.
|
|---|
| 26 | *
|
|---|
| 27 | * Note: Version numbering in this file relates to XWorkplace version
|
|---|
| 28 | * numbering.
|
|---|
| 29 | *
|
|---|
| 30 | *@@header "helpers\procstat.h"
|
|---|
| 31 | */
|
|---|
| 32 |
|
|---|
| 33 | /*
|
|---|
| 34 | * Copyright (C) 1992-1994 Kai Uwe Rommel.
|
|---|
| 35 | * Copyright (C) 1998-2000 Ulrich Mller.
|
|---|
| 36 | * This file is part of the "XWorkplace helpers" source package.
|
|---|
| 37 | * This is free software; you can redistribute it and/or modify
|
|---|
| 38 | * it under the terms of the GNU General Public License as published
|
|---|
| 39 | * by the Free Software Foundation, in version 2 as it comes in the
|
|---|
| 40 | * "COPYING" file of the XWorkplace main distribution.
|
|---|
| 41 | * This program is distributed in the hope that it will be useful,
|
|---|
| 42 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 43 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|---|
| 44 | * GNU General Public License for more details.
|
|---|
| 45 | */
|
|---|
| 46 |
|
|---|
| 47 | #define OS2EMX_PLAIN_CHAR
|
|---|
| 48 | // this is needed for "os2emx.h"; if this is defined,
|
|---|
| 49 | // emx will define PSZ as _signed_ char, otherwise
|
|---|
| 50 | // as unsigned char
|
|---|
| 51 |
|
|---|
| 52 | #define INCL_DOSMODULEMGR
|
|---|
| 53 | #define INCL_DOSERRORS
|
|---|
| 54 | #include <os2.h>
|
|---|
| 55 |
|
|---|
| 56 | #include <stdlib.h> // already #include'd
|
|---|
| 57 | #include <string.h> // already #include'd
|
|---|
| 58 | #include <stdio.h>
|
|---|
| 59 |
|
|---|
| 60 | #include "setup.h" // code generation and debugging options
|
|---|
| 61 |
|
|---|
| 62 | #include "helpers\procstat.h"
|
|---|
| 63 |
|
|---|
| 64 | #pragma hdrstop
|
|---|
| 65 |
|
|---|
| 66 | /*
|
|---|
| 67 | *@@category: Helpers\Control program helpers\Process status\16-bit DosQProcStat
|
|---|
| 68 | */
|
|---|
| 69 |
|
|---|
| 70 | /********************************************************************
|
|---|
| 71 | *
|
|---|
| 72 | * DosQProcStat (16-bit) interface
|
|---|
| 73 | *
|
|---|
| 74 | ********************************************************************/
|
|---|
| 75 |
|
|---|
| 76 | /*
|
|---|
| 77 | *@@ prc16GetInfo:
|
|---|
| 78 | * nifty interface to DosQProcStat (16-bit).
|
|---|
| 79 | * This returns the head of a newly
|
|---|
| 80 | * allocated buffer which has plenty
|
|---|
| 81 | * of pointers for subsequent browsing.
|
|---|
| 82 | *
|
|---|
| 83 | * Use prc16FreeInfo to free the buffer.
|
|---|
| 84 | *
|
|---|
| 85 | *@@added V0.9.3 (2000-05-05) [umoeller]
|
|---|
| 86 | *@@changed V0.9.10 (2001-04-08) [umoeller]: this returned != NULL even though item was freed, fixed
|
|---|
| 87 | *@@changed V0.9.10 (2001-04-08) [umoeller]: now using DosAllocMem, raised bufsize, changed prototype
|
|---|
| 88 | */
|
|---|
| 89 |
|
|---|
| 90 | APIRET prc16GetInfo(PQPROCSTAT16 *ppps) // out: error, ptr can be NULL
|
|---|
| 91 | {
|
|---|
| 92 | APIRET arc = NO_ERROR;
|
|---|
| 93 | PQPROCSTAT16 pps = NULL;
|
|---|
| 94 |
|
|---|
| 95 | if (!ppps)
|
|---|
| 96 | return ERROR_INVALID_PARAMETER;
|
|---|
| 97 |
|
|---|
| 98 | // changed allocation V0.9.10 (2001-04-08) [umoeller]:
|
|---|
| 99 | // malloc didn't guarantee that the object did not
|
|---|
| 100 | // cross a 64K boundary, which could cause DosQProcStat
|
|---|
| 101 | // to fail...
|
|---|
| 102 | #define BUF_SIZE 0xFFFF // raised from 0x8000
|
|---|
| 103 |
|
|---|
| 104 | if (!(arc = DosAllocMem((VOID**)&pps,
|
|---|
| 105 | BUF_SIZE,
|
|---|
| 106 | PAG_READ | PAG_WRITE | PAG_COMMIT
|
|---|
| 107 | | OBJ_TILE // 16-bit compatible, ignored really
|
|---|
| 108 | )))
|
|---|
| 109 | {
|
|---|
| 110 | if (arc = DosQProcStatus(pps, BUF_SIZE))
|
|---|
| 111 | {
|
|---|
| 112 | // error:
|
|---|
| 113 | DosFreeMem(pps); // V0.9.10 (2001-04-08) [umoeller]
|
|---|
| 114 |
|
|---|
| 115 | // and even worse, I forgot to set the return ptr
|
|---|
| 116 | // to NULL, so this was freed twice... I guess
|
|---|
| 117 | // this produced the crashes in WarpIN with the
|
|---|
| 118 | // KILLPROCESS attribute... V0.9.10 (2001-04-08) [umoeller]
|
|---|
| 119 | pps = NULL;
|
|---|
| 120 | }
|
|---|
| 121 | }
|
|---|
| 122 |
|
|---|
| 123 | *ppps = pps;
|
|---|
| 124 |
|
|---|
| 125 | return arc;
|
|---|
| 126 | }
|
|---|
| 127 |
|
|---|
| 128 | /*
|
|---|
| 129 | *@@ prc16FreeInfo:
|
|---|
| 130 | * frees memory allocated by prc16GetInfo.
|
|---|
| 131 | *
|
|---|
| 132 | *@@added V0.9.3 (2000-05-05) [umoeller]
|
|---|
| 133 | *@@changed V0.9.10 (2001-04-08) [umoeller]: now using DosFreeMem
|
|---|
| 134 | */
|
|---|
| 135 |
|
|---|
| 136 | APIRET prc16FreeInfo(PQPROCSTAT16 pInfo)
|
|---|
| 137 | {
|
|---|
| 138 | if (!pInfo)
|
|---|
| 139 | return ERROR_INVALID_PARAMETER;
|
|---|
| 140 |
|
|---|
| 141 | return DosFreeMem(pInfo);
|
|---|
| 142 | }
|
|---|
| 143 |
|
|---|
| 144 | /*
|
|---|
| 145 | *@@ prc16FindProcessFromName:
|
|---|
| 146 | * searches the specified buffer for a process
|
|---|
| 147 | * with the specified name and returns a pointer
|
|---|
| 148 | * to its data within pInfo.
|
|---|
| 149 | *
|
|---|
| 150 | * Returns NULL if not found.
|
|---|
| 151 | *
|
|---|
| 152 | *@@added V0.9.3 (2000-05-05) [umoeller]
|
|---|
| 153 | */
|
|---|
| 154 |
|
|---|
| 155 | PQPROCESS16 prc16FindProcessFromName(PQPROCSTAT16 pInfo, // in: from prc16GetInfo
|
|---|
| 156 | const char *pcszName) // in: e.g. "pmshell.exe"
|
|---|
| 157 | {
|
|---|
| 158 | PQPROCESS16 pProcess,
|
|---|
| 159 | pReturn = NULL;
|
|---|
| 160 | if (pInfo)
|
|---|
| 161 | {
|
|---|
| 162 | for ( pProcess = (PQPROCESS16)PTR(pInfo->ulProcesses, 0);
|
|---|
| 163 | pProcess->ulType != 3;
|
|---|
| 164 | pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
|
|---|
| 165 | pProcess->usThreads * sizeof(QTHREAD16))
|
|---|
| 166 | )
|
|---|
| 167 | {
|
|---|
| 168 | CHAR szModuleName[CCHMAXPATH];
|
|---|
| 169 | if (DosQueryModuleName(pProcess->usHModule,
|
|---|
| 170 | sizeof(szModuleName),
|
|---|
| 171 | szModuleName)
|
|---|
| 172 | == NO_ERROR)
|
|---|
| 173 | {
|
|---|
| 174 | // the module name is fully qualified, so find the
|
|---|
| 175 | // file name (after the last backslash)
|
|---|
| 176 | PSZ pLastBackslash = strrchr(szModuleName, '\\');
|
|---|
| 177 | if (pLastBackslash)
|
|---|
| 178 | // found:
|
|---|
| 179 | if (stricmp(pLastBackslash + 1, pcszName) == 0)
|
|---|
| 180 | {
|
|---|
| 181 | // matches:
|
|---|
| 182 | pReturn = pProcess;
|
|---|
| 183 | break;
|
|---|
| 184 | }
|
|---|
| 185 | }
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 |
|
|---|
| 189 | return pReturn;
|
|---|
| 190 | }
|
|---|
| 191 |
|
|---|
| 192 | /*
|
|---|
| 193 | *@@ prc16FindProcessFromPID:
|
|---|
| 194 | * searches the specified buffer for a process
|
|---|
| 195 | * with the specified PID and returns a pointer
|
|---|
| 196 | * to its data within pInfo.
|
|---|
| 197 | *
|
|---|
| 198 | * Returns NULL if not found.
|
|---|
| 199 | *
|
|---|
| 200 | *V0.9.5 (2000-09-29) [umoeller]
|
|---|
| 201 | */
|
|---|
| 202 |
|
|---|
| 203 | PQPROCESS16 prc16FindProcessFromPID(PQPROCSTAT16 pInfo, // in: from prc16GetInfo
|
|---|
| 204 | ULONG ulPID) // in: PID
|
|---|
| 205 | {
|
|---|
| 206 | PQPROCESS16 pProcess,
|
|---|
| 207 | pReturn = NULL;
|
|---|
| 208 | if (pInfo)
|
|---|
| 209 | {
|
|---|
| 210 | for ( pProcess = (PQPROCESS16)PTR(pInfo->ulProcesses, 0);
|
|---|
| 211 | pProcess->ulType != 3;
|
|---|
| 212 | pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
|
|---|
| 213 | pProcess->usThreads * sizeof(QTHREAD16))
|
|---|
| 214 | )
|
|---|
| 215 | {
|
|---|
| 216 | if (pProcess->usPID == ulPID)
|
|---|
| 217 | {
|
|---|
| 218 | pReturn = pProcess;
|
|---|
| 219 | break;
|
|---|
| 220 | }
|
|---|
| 221 | }
|
|---|
| 222 | }
|
|---|
| 223 |
|
|---|
| 224 | return pReturn;
|
|---|
| 225 | }
|
|---|
| 226 |
|
|---|
| 227 | /********************************************************************
|
|---|
| 228 | *
|
|---|
| 229 | * DosQProcStat (16-bit) helpers
|
|---|
| 230 | *
|
|---|
| 231 | ********************************************************************/
|
|---|
| 232 |
|
|---|
| 233 | /*
|
|---|
| 234 | * prcReport16:
|
|---|
| 235 | * fill PRCPROCESS structure
|
|---|
| 236 | */
|
|---|
| 237 |
|
|---|
| 238 | VOID prcReport16(PQPROCESS16 pProcess, PPRCPROCESS pprcp)
|
|---|
| 239 | {
|
|---|
| 240 | if (pProcess)
|
|---|
| 241 | {
|
|---|
| 242 | PQTHREAD16 pThread;
|
|---|
| 243 | int i;
|
|---|
| 244 |
|
|---|
| 245 | DosQueryModuleName(pProcess->usHModule,
|
|---|
| 246 | sizeof(pprcp->szModuleName),
|
|---|
| 247 | pprcp->szModuleName);
|
|---|
| 248 | // DosGetPrty(PRTYS_PROCESS, &(pprcp->usPriority), pProcess->usPID);
|
|---|
| 249 |
|
|---|
| 250 | // sum up CPU time for process
|
|---|
| 251 | for (pprcp->ulCPU = 0,
|
|---|
| 252 | i = 0,
|
|---|
| 253 | pThread = (PQTHREAD16)PTR(pProcess->ulThreadList, 0);
|
|---|
| 254 | i < pProcess->usThreads;
|
|---|
| 255 | i++, pThread++ )
|
|---|
| 256 | {
|
|---|
| 257 | pprcp->ulCPU += (pThread->ulSysTime + pThread->ulUserTime);
|
|---|
| 258 | }
|
|---|
| 259 |
|
|---|
| 260 | pprcp->usPID = pProcess->usPID;
|
|---|
| 261 | pprcp->usParentPID = pProcess->usParentPID;
|
|---|
| 262 | pprcp->usThreads = pProcess->usThreads;
|
|---|
| 263 | pprcp->ulSID = pProcess->ulSID;
|
|---|
| 264 | pprcp->ulSessionType = pProcess->ulSessionType;
|
|---|
| 265 | pprcp->ulStatus = pProcess->ulStatus;
|
|---|
| 266 | }
|
|---|
| 267 | }
|
|---|
| 268 |
|
|---|
| 269 | /*
|
|---|
| 270 | *@@ prc16QueryProcessInfo:
|
|---|
| 271 | * this searches for a given process ID (usPID) and
|
|---|
| 272 | * fills a given PRCPROCESS structure with lots of
|
|---|
| 273 | * information about this process.
|
|---|
| 274 | * Returns FALSE upon errors, e.g. if no process
|
|---|
| 275 | * of that ID is found.
|
|---|
| 276 | */
|
|---|
| 277 |
|
|---|
| 278 | BOOL prc16QueryProcessInfo(PQPROCSTAT16 pps, // in: from prc16GetInfo
|
|---|
| 279 | USHORT usPID, // in: PID to query
|
|---|
| 280 | PPRCPROCESS pprcp) // out: process info
|
|---|
| 281 | {
|
|---|
| 282 | BOOL rc = FALSE;
|
|---|
| 283 | if (pps)
|
|---|
| 284 | {
|
|---|
| 285 | PQPROCESS16 pProcess;
|
|---|
| 286 |
|
|---|
| 287 | for ( pProcess = (PQPROCESS16)PTR(pps->ulProcesses, 0);
|
|---|
| 288 | pProcess->ulType != 3;
|
|---|
| 289 | pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
|
|---|
| 290 | pProcess->usThreads * sizeof(QTHREAD16))
|
|---|
| 291 | )
|
|---|
| 292 | {
|
|---|
| 293 | if (pProcess->usPID == usPID)
|
|---|
| 294 | {
|
|---|
| 295 | prcReport16(pProcess, pprcp);
|
|---|
| 296 | rc = TRUE;
|
|---|
| 297 | break;
|
|---|
| 298 | }
|
|---|
| 299 | }
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | return rc;
|
|---|
| 303 | }
|
|---|
| 304 |
|
|---|
| 305 | /*
|
|---|
| 306 | *@@ prc16ForEachProcess:
|
|---|
| 307 | * this calls a given callback func for each running
|
|---|
| 308 | * process. The callback must be a FNWP, which will be
|
|---|
| 309 | * passed the following parameters for each call:
|
|---|
| 310 | * -- HWND hwnd: like hwnd passed to this func
|
|---|
| 311 | * -- ULONG msg: like msg passed to this func
|
|---|
| 312 | * -- MPARAM mp1: like mp1 passed to this func
|
|---|
| 313 | * -- PPRCPROCESS: mp2 pointer to a PRCPROCESS struct for each process
|
|---|
| 314 | *
|
|---|
| 315 | * This function returns the number of running processes on the
|
|---|
| 316 | * system. If pfnwpCallback is NULL, only this number will be
|
|---|
| 317 | * returned, so you can use this as a process counter too.
|
|---|
| 318 | *
|
|---|
| 319 | *@@changed V0.9.10 (2001-04-16) [pr]: now using DosAllocMem
|
|---|
| 320 | */
|
|---|
| 321 |
|
|---|
| 322 | ULONG prc16ForEachProcess(PFNWP pfnwpCallback, HWND hwnd, ULONG ulMsg, MPARAM mp1)
|
|---|
| 323 | {
|
|---|
| 324 | ULONG ulrc = 0;
|
|---|
| 325 | PQPROCSTAT16 pps;
|
|---|
| 326 | PQPROCESS16 pProcess;
|
|---|
| 327 | PRCPROCESS prcp;
|
|---|
| 328 |
|
|---|
| 329 | if (!DosAllocMem((PVOID*)&pps,
|
|---|
| 330 | BUF_SIZE,
|
|---|
| 331 | PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_TILE))
|
|---|
| 332 | {
|
|---|
| 333 | if (!DosQProcStatus(pps, BUF_SIZE))
|
|---|
| 334 | for ( pProcess = (PQPROCESS16)PTR(pps->ulProcesses, 0);
|
|---|
| 335 | pProcess->ulType != 3;
|
|---|
| 336 | pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
|
|---|
| 337 | pProcess->usThreads * sizeof(QTHREAD16))
|
|---|
| 338 | )
|
|---|
| 339 | {
|
|---|
| 340 | if (pfnwpCallback)
|
|---|
| 341 | {
|
|---|
| 342 | prcReport16(pProcess, &prcp);
|
|---|
| 343 | (*pfnwpCallback)(hwnd, ulMsg, mp1, &prcp);
|
|---|
| 344 | }
|
|---|
| 345 | ulrc++;
|
|---|
| 346 | }
|
|---|
| 347 |
|
|---|
| 348 | DosFreeMem(pps);
|
|---|
| 349 | }
|
|---|
| 350 |
|
|---|
| 351 | return ulrc;
|
|---|
| 352 | }
|
|---|
| 353 |
|
|---|
| 354 | /*
|
|---|
| 355 | *@@ prc16QueryThreadCount:
|
|---|
| 356 | * returns the total number of running threads
|
|---|
| 357 | * in the given process. If pid == 0, the
|
|---|
| 358 | * total thread count for the system is returned.
|
|---|
| 359 | *
|
|---|
| 360 | *@@changed V0.9.9 (2001-03-07) [umoeller]: added pps param
|
|---|
| 361 | */
|
|---|
| 362 |
|
|---|
| 363 | ULONG prc16QueryThreadCount(PQPROCSTAT16 pps, // in: from prc16GetInfo
|
|---|
| 364 | USHORT usPID)
|
|---|
| 365 | {
|
|---|
| 366 | ULONG ulrc = 0;
|
|---|
| 367 |
|
|---|
| 368 | if (pps)
|
|---|
| 369 | {
|
|---|
| 370 | if (usPID)
|
|---|
| 371 | {
|
|---|
| 372 | // process query:
|
|---|
| 373 | PQPROCESS16 pProcess;
|
|---|
| 374 | for ( pProcess = (PQPROCESS16)PTR(pps->ulProcesses, 0);
|
|---|
| 375 | pProcess->ulType != 3;
|
|---|
| 376 | pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
|
|---|
| 377 | pProcess->usThreads * sizeof(QTHREAD16))
|
|---|
| 378 | )
|
|---|
| 379 | {
|
|---|
| 380 | if (pProcess->usPID == usPID)
|
|---|
| 381 | {
|
|---|
| 382 | ulrc = pProcess->usThreads;
|
|---|
| 383 | break;
|
|---|
| 384 | }
|
|---|
| 385 | }
|
|---|
| 386 | }
|
|---|
| 387 | else
|
|---|
| 388 | {
|
|---|
| 389 | // global query:
|
|---|
| 390 | PQGLOBAL16 pg;
|
|---|
| 391 | pg = (PQGLOBAL16)PTR(pps->ulGlobal, 0);
|
|---|
| 392 | ulrc = pg->ulThreads;
|
|---|
| 393 | }
|
|---|
| 394 | }
|
|---|
| 395 |
|
|---|
| 396 | return ulrc;
|
|---|
| 397 | }
|
|---|
| 398 |
|
|---|
| 399 | /*
|
|---|
| 400 | *@@ prc16QueryThreadInfo:
|
|---|
| 401 | * this searches for a given thread in a given process
|
|---|
| 402 | * and fills a given PRCTHREAD structure with lots of
|
|---|
| 403 | * information about that thread.
|
|---|
| 404 | *
|
|---|
| 405 | * Returns FALSE upon errors.
|
|---|
| 406 | *
|
|---|
| 407 | * Note: This function loops thru all processes which
|
|---|
| 408 | * are currently running and is therefore not terribly
|
|---|
| 409 | * fast. Use economically.
|
|---|
| 410 | *
|
|---|
| 411 | *@@changed V0.9.9 (2001-03-07) [umoeller]: added pps param
|
|---|
| 412 | */
|
|---|
| 413 |
|
|---|
| 414 | BOOL prc16QueryThreadInfo(PQPROCSTAT16 pps, // in: from prc16GetInfo
|
|---|
| 415 | USHORT usPID,
|
|---|
| 416 | USHORT usTID,
|
|---|
| 417 | PPRCTHREAD pprct)
|
|---|
| 418 | {
|
|---|
| 419 | BOOL brc = FALSE;
|
|---|
| 420 | if (pps)
|
|---|
| 421 | {
|
|---|
| 422 | PQPROCESS16 pProcess;
|
|---|
| 423 |
|
|---|
| 424 | // find process:
|
|---|
| 425 | for ( pProcess = (PQPROCESS16)PTR(pps->ulProcesses, 0);
|
|---|
| 426 | pProcess->ulType != 3;
|
|---|
| 427 | pProcess = (PQPROCESS16)PTR(pProcess->ulThreadList,
|
|---|
| 428 | pProcess->usThreads * sizeof(QTHREAD16))
|
|---|
| 429 | )
|
|---|
| 430 | {
|
|---|
| 431 | if (pProcess->usPID == usPID)
|
|---|
| 432 | {
|
|---|
| 433 | PQTHREAD16 pThread;
|
|---|
| 434 | int i;
|
|---|
| 435 | // process found: find thread
|
|---|
| 436 | for ( i = 0, pThread = (PQTHREAD16)PTR(pProcess->ulThreadList, 0);
|
|---|
| 437 | i < pProcess->usThreads;
|
|---|
| 438 | i++, pThread++ )
|
|---|
| 439 | {
|
|---|
| 440 | if (pThread->usTID == usTID)
|
|---|
| 441 | {
|
|---|
| 442 | // thread found:
|
|---|
| 443 | pprct->usTID = pThread->usTID;
|
|---|
| 444 | pprct->usThreadSlotID = pThread->usThreadSlotID;
|
|---|
| 445 | pprct->ulBlockID = pThread->ulBlockID;
|
|---|
| 446 | pprct->ulPriority = pThread->ulPriority;
|
|---|
| 447 | pprct->ulSysTime = pThread->ulSysTime;
|
|---|
| 448 | pprct->ulUserTime = pThread->ulUserTime;
|
|---|
| 449 | pprct->ucStatus = pThread->ucStatus;
|
|---|
| 450 |
|
|---|
| 451 | brc = TRUE;
|
|---|
| 452 |
|
|---|
| 453 | break; // thread-for loop
|
|---|
| 454 | }
|
|---|
| 455 | } // end for thread
|
|---|
| 456 | break; // process-for loop
|
|---|
| 457 | }
|
|---|
| 458 | } // end for process
|
|---|
| 459 | }
|
|---|
| 460 |
|
|---|
| 461 | return brc;
|
|---|
| 462 | }
|
|---|
| 463 |
|
|---|
| 464 | /*
|
|---|
| 465 | *@@ prcQueryPriority:
|
|---|
| 466 | * shortcut to prc16QueryThreadInfo if you want the priority only.
|
|---|
| 467 | *
|
|---|
| 468 | * Returns -1 upon errors.
|
|---|
| 469 | *
|
|---|
| 470 | * Note: This function loops thru all processes which
|
|---|
| 471 | * are currently running and is therefore not terribly
|
|---|
| 472 | * fast. Use economically.
|
|---|
| 473 | *
|
|---|
| 474 | *@@changed V0.9.9 (2001-03-07) [umoeller]: added pps param
|
|---|
| 475 | */
|
|---|
| 476 |
|
|---|
| 477 | ULONG prc16QueryThreadPriority(PQPROCSTAT16 pps, // in: from prc16GetInfo
|
|---|
| 478 | USHORT usPID,
|
|---|
| 479 | USHORT usTID)
|
|---|
| 480 | {
|
|---|
| 481 | PRCTHREAD prct;
|
|---|
| 482 | ULONG ulrc = -1;
|
|---|
| 483 | if (prc16QueryThreadInfo(pps, usPID, usTID, &prct))
|
|---|
| 484 | ulrc = prct.ulPriority;
|
|---|
| 485 | return ulrc;
|
|---|
| 486 | }
|
|---|
| 487 |
|
|---|
| 488 | /*
|
|---|
| 489 | *@@category: Helpers\Control program helpers\Process status\32-bit DosQuerySysState
|
|---|
| 490 | */
|
|---|
| 491 |
|
|---|
| 492 | /********************************************************************
|
|---|
| 493 | *
|
|---|
| 494 | * DosQuerySysState (32-bit) interface
|
|---|
| 495 | *
|
|---|
| 496 | ********************************************************************/
|
|---|
| 497 |
|
|---|
| 498 | /*
|
|---|
| 499 | *@@ prc32GetInfo2:
|
|---|
| 500 | * nifty interface to DosQuerySysState, the 32-bit
|
|---|
| 501 | * version of DosQProcStat.
|
|---|
| 502 | *
|
|---|
| 503 | * This returns the head of a newly allocated buffer
|
|---|
| 504 | * which has plenty of pointers for subsequent browing.
|
|---|
| 505 | *
|
|---|
| 506 | * As opposed to prc32GetInfo, with this call you can
|
|---|
| 507 | * specify the information that would like to retrieve.
|
|---|
| 508 | *
|
|---|
| 509 | * Use prc32FreeInfo to free the buffer.
|
|---|
| 510 | *
|
|---|
| 511 | *@@added V1.0.1 (2003-01-10) [umoeller]
|
|---|
| 512 | */
|
|---|
| 513 |
|
|---|
| 514 | PQTOPLEVEL32 prc32GetInfo2(ULONG fl, // in: QS32_* flags
|
|---|
| 515 | APIRET *parc) // out: error, ptr can be NULL
|
|---|
| 516 | {
|
|---|
| 517 | APIRET arc;
|
|---|
| 518 | PQTOPLEVEL32 pReturn = NULL;
|
|---|
| 519 |
|
|---|
| 520 | #define BUFSIZE (1024 * 1024) // 1 meg
|
|---|
| 521 |
|
|---|
| 522 | if (!(arc = DosAllocMem((PVOID*)&pReturn,
|
|---|
| 523 | BUFSIZE,
|
|---|
| 524 | PAG_READ | PAG_WRITE | PAG_COMMIT | OBJ_TILE)))
|
|---|
| 525 | {
|
|---|
| 526 | if (arc = DosQuerySysState(fl,
|
|---|
| 527 | fl, // this was missing V0.9.10 (2001-04-08) [umoeller]
|
|---|
| 528 | 0, 0,
|
|---|
| 529 | (PCHAR)pReturn,
|
|---|
| 530 | BUFSIZE))
|
|---|
| 531 | {
|
|---|
| 532 | DosFreeMem(pReturn);
|
|---|
| 533 | pReturn = NULL;
|
|---|
| 534 | }
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | if (parc)
|
|---|
| 538 | *parc = arc;
|
|---|
| 539 |
|
|---|
| 540 | return pReturn;
|
|---|
| 541 | }
|
|---|
| 542 |
|
|---|
| 543 | /*
|
|---|
| 544 | *@@ prc32GetInfo:
|
|---|
| 545 | * nifty interface to DosQuerySysState, the 32-bit
|
|---|
| 546 | * version of DosQProcStat.
|
|---|
| 547 | *
|
|---|
| 548 | * This returns the head of a newly allocated buffer
|
|---|
| 549 | * which has plenty of pointers for subsequent browing.
|
|---|
| 550 | *
|
|---|
| 551 | * Use prc32FreeInfo to free the buffer.
|
|---|
| 552 | *
|
|---|
| 553 | *@@added V0.9.1 (2000-02-12) [umoeller]
|
|---|
| 554 | *@@changed V0.9.3 (2000-05-01) [umoeller]: now using DosAllocMem
|
|---|
| 555 | *@@changed V0.9.10 (2001-04-08) [umoeller]: fixed second QuerySysState param
|
|---|
| 556 | */
|
|---|
| 557 |
|
|---|
| 558 | PQTOPLEVEL32 prc32GetInfo(APIRET *parc) // out: error, ptr can be NULL
|
|---|
| 559 | {
|
|---|
| 560 | return prc32GetInfo2(QS32_SUPPORTED,
|
|---|
| 561 | parc);
|
|---|
| 562 | }
|
|---|
| 563 |
|
|---|
| 564 | /*
|
|---|
| 565 | *@@ prc32FreeInfo:
|
|---|
| 566 | * frees the memory allocated by prc32GetInfo.
|
|---|
| 567 | *
|
|---|
| 568 | *@@added V0.9.1 (2000-02-12) [umoeller]
|
|---|
| 569 | *@@changed V0.9.3 (2000-05-01) [umoeller]: now using DosFreeMem
|
|---|
| 570 | */
|
|---|
| 571 |
|
|---|
| 572 | VOID prc32FreeInfo(PQTOPLEVEL32 pInfo)
|
|---|
| 573 | {
|
|---|
| 574 | DosFreeMem(pInfo);
|
|---|
| 575 | }
|
|---|
| 576 |
|
|---|
| 577 | /*
|
|---|
| 578 | *@@ prc32FindProcessFromName:
|
|---|
| 579 | *
|
|---|
| 580 | *@@added V0.9.2 (2000-03-05) [umoeller]
|
|---|
| 581 | *@@changed V1.0.0 (2002-08-16) [pr]: optimized
|
|---|
| 582 | */
|
|---|
| 583 |
|
|---|
| 584 | PQPROCESS32 prc32FindProcessFromName(PQTOPLEVEL32 pInfo,
|
|---|
| 585 | const char *pcszName) // in: e.g. "pmshell.exe"
|
|---|
| 586 | {
|
|---|
| 587 | PQPROCESS32 pProcThis = pInfo->pProcessData;
|
|---|
| 588 | while (pProcThis && pProcThis->ulRecType == 1)
|
|---|
| 589 | {
|
|---|
| 590 | PQTHREAD32 t = pProcThis->pThreads;
|
|---|
| 591 | PQMODULE32 pModule;
|
|---|
| 592 |
|
|---|
| 593 | if (pModule = prc32FindModule(pInfo,
|
|---|
| 594 | pProcThis->usHModule))
|
|---|
| 595 | {
|
|---|
| 596 | // the module name is fully qualified, so find the
|
|---|
| 597 | // file name (after the last backslash)
|
|---|
| 598 | if (pModule->pcName)
|
|---|
| 599 | {
|
|---|
| 600 | PSZ pLastBackslash;
|
|---|
| 601 | if (pLastBackslash = strrchr(pModule->pcName, '\\'))
|
|---|
| 602 | // found:
|
|---|
| 603 | if (stricmp(pLastBackslash + 1, pcszName) == 0)
|
|---|
| 604 | // matches:
|
|---|
| 605 | break;
|
|---|
| 606 | }
|
|---|
| 607 | }
|
|---|
| 608 |
|
|---|
| 609 | // for next process, skip the threads info;
|
|---|
| 610 | // the next process block comes after the
|
|---|
| 611 | // threads
|
|---|
| 612 | t += pProcThis->usThreadCount;
|
|---|
| 613 | pProcThis = (PQPROCESS32)t;
|
|---|
| 614 | }
|
|---|
| 615 |
|
|---|
| 616 | if (pProcThis->ulRecType == 1)
|
|---|
| 617 | return pProcThis;
|
|---|
| 618 |
|
|---|
| 619 | return NULL;
|
|---|
| 620 | }
|
|---|
| 621 |
|
|---|
| 622 | /*
|
|---|
| 623 | *@@ prc32FindProcessFromPID:
|
|---|
| 624 | *
|
|---|
| 625 | *@@added V1.0.0 (2002-08-12) [umoeller]
|
|---|
| 626 | *@@changed V1.0.0 (2002-08-16) [pr]: optimized
|
|---|
| 627 | */
|
|---|
| 628 |
|
|---|
| 629 | PQPROCESS32 prc32FindProcessFromPID(PQTOPLEVEL32 pInfo,
|
|---|
| 630 | ULONG pid)
|
|---|
| 631 | {
|
|---|
| 632 | PQPROCESS32 pProcThis = pInfo->pProcessData;
|
|---|
| 633 | while (pProcThis && pProcThis->ulRecType == 1)
|
|---|
| 634 | {
|
|---|
| 635 | PQTHREAD32 t = pProcThis->pThreads;
|
|---|
| 636 |
|
|---|
| 637 | if (pProcThis->usPID == pid)
|
|---|
| 638 | return pProcThis;
|
|---|
| 639 |
|
|---|
| 640 | // for next process, skip the threads info;
|
|---|
| 641 | // the next process block comes after the
|
|---|
| 642 | // threads
|
|---|
| 643 | t += pProcThis->usThreadCount;
|
|---|
| 644 | pProcThis = (PQPROCESS32)t;
|
|---|
| 645 | }
|
|---|
| 646 |
|
|---|
| 647 | return NULL;
|
|---|
| 648 | }
|
|---|
| 649 |
|
|---|
| 650 | /*
|
|---|
| 651 | *@@ prc32FindSem16:
|
|---|
| 652 | * attempts to find the specified 16-bit semaphore
|
|---|
| 653 | * in the specified info buffer.
|
|---|
| 654 | *
|
|---|
| 655 | * The return value points into the pInfo buffer.
|
|---|
| 656 | * Returns NULL if not found.
|
|---|
| 657 | *
|
|---|
| 658 | *@@added V0.9.1 (2000-02-12) [umoeller]
|
|---|
| 659 | */
|
|---|
| 660 |
|
|---|
| 661 | PQS32SEM16 prc32FindSem16(PQTOPLEVEL32 pInfo, // in: as returned by prc32GetInfo
|
|---|
| 662 | USHORT usSemID) // in: as in QPROCESS32.pausSem16
|
|---|
| 663 | {
|
|---|
| 664 | PQS32SEM16HEAD pSemHead = pInfo->pSem16Data;
|
|---|
| 665 | PQS32SEM16 // pSemThis = &pSemData->sema;
|
|---|
| 666 | pSemThis = &pSemHead->Sem16Rec;
|
|---|
| 667 | ULONG i = 0;
|
|---|
| 668 |
|
|---|
| 669 | while (pSemThis)
|
|---|
| 670 | {
|
|---|
| 671 | if (i == usSemID)
|
|---|
| 672 | return pSemThis;
|
|---|
| 673 |
|
|---|
| 674 | i++;
|
|---|
| 675 | pSemThis = pSemThis->pNext;
|
|---|
| 676 | }
|
|---|
| 677 |
|
|---|
| 678 | return NULL;
|
|---|
| 679 | }
|
|---|
| 680 |
|
|---|
| 681 | /*
|
|---|
| 682 | *@@ prc32FindSem32:
|
|---|
| 683 | * attempts to find the specified 32-bit semaphore
|
|---|
| 684 | * in the specified info buffer. This might fail
|
|---|
| 685 | * because the data isn't always complete.
|
|---|
| 686 | *
|
|---|
| 687 | * The return value points into the pInfo buffer.
|
|---|
| 688 | * Returns NULL if not found.
|
|---|
| 689 | *
|
|---|
| 690 | *@@added V0.9.1 (2000-02-12) [umoeller]
|
|---|
| 691 | */
|
|---|
| 692 |
|
|---|
| 693 | PQS32SEM32 prc32FindSem32(PQTOPLEVEL32 pInfo, // in: as returned by prc32GetInfo
|
|---|
| 694 | USHORT usSemID) // in: as in QPROCESS32.pausSem16
|
|---|
| 695 | {
|
|---|
| 696 | // PQSEM32STRUC32 pSemThis = pInfo->pSem32Data;
|
|---|
| 697 |
|
|---|
| 698 | /* while (pSemThis)
|
|---|
| 699 | {
|
|---|
| 700 | if (pSemThis->usIndex == usSemID)
|
|---|
| 701 | return pSemThis;
|
|---|
| 702 |
|
|---|
| 703 | pSemThis = pSemThis->pNext;
|
|---|
| 704 | } */
|
|---|
| 705 |
|
|---|
| 706 | return NULL;
|
|---|
| 707 | }
|
|---|
| 708 |
|
|---|
| 709 | /*
|
|---|
| 710 | *@@ prc32FindShrMem:
|
|---|
| 711 | * attempts to find the specified shared memory
|
|---|
| 712 | * block description.
|
|---|
| 713 | *
|
|---|
| 714 | * The return value points into the pInfo buffer.
|
|---|
| 715 | * Returns NULL if not found.
|
|---|
| 716 | *
|
|---|
| 717 | *@@added V0.9.1 (2000-02-12) [umoeller]
|
|---|
| 718 | */
|
|---|
| 719 |
|
|---|
| 720 | PQSHRMEM32 prc32FindShrMem(PQTOPLEVEL32 pInfo, // in: as returned by prc32GetInfo
|
|---|
| 721 | USHORT usShrMemID) // in: as in QPROCESS32.pausShrMems
|
|---|
| 722 | {
|
|---|
| 723 | PQSHRMEM32 pShrMem = pInfo->pShrMemData;
|
|---|
| 724 | while (pShrMem)
|
|---|
| 725 | {
|
|---|
| 726 | if (pShrMem->usHandle == usShrMemID)
|
|---|
| 727 | return pShrMem;
|
|---|
| 728 | pShrMem = pShrMem->pNext;
|
|---|
| 729 | }
|
|---|
| 730 |
|
|---|
| 731 | return NULL;
|
|---|
| 732 | }
|
|---|
| 733 |
|
|---|
| 734 | /*
|
|---|
| 735 | *@@ prc32FindModule:
|
|---|
| 736 | * attempts to find the specified module description.
|
|---|
| 737 | *
|
|---|
| 738 | * The return value points into the pInfo buffer.
|
|---|
| 739 | * Returns NULL if not found.
|
|---|
| 740 | *
|
|---|
| 741 | *@@added V0.9.1 (2000-02-12) [umoeller]
|
|---|
| 742 | */
|
|---|
| 743 |
|
|---|
| 744 | PQMODULE32 prc32FindModule(PQTOPLEVEL32 pInfo, // in: as returned by prc32GetInfo
|
|---|
| 745 | USHORT usHModule)
|
|---|
| 746 | {
|
|---|
| 747 | PQMODULE32 pModule = pInfo->pModuleData;
|
|---|
| 748 | while (pModule)
|
|---|
| 749 | {
|
|---|
| 750 | if (pModule->usHModule == usHModule)
|
|---|
| 751 | return pModule;
|
|---|
| 752 |
|
|---|
| 753 | pModule = pModule->pNext;
|
|---|
| 754 | }
|
|---|
| 755 |
|
|---|
| 756 | return NULL;
|
|---|
| 757 | }
|
|---|
| 758 |
|
|---|
| 759 | /*
|
|---|
| 760 | *@@ prc32FindFileData:
|
|---|
| 761 | *
|
|---|
| 762 | *
|
|---|
| 763 | *@@added V0.9.1 (2000-02-12) [umoeller]
|
|---|
| 764 | */
|
|---|
| 765 |
|
|---|
| 766 | PQFILEDATA32 prc32FindFileData(PQTOPLEVEL32 pInfo, // in: as returned by prc32GetInfo
|
|---|
| 767 | USHORT usFileID) // in: as in QPROCESS32.pausFds
|
|---|
| 768 | {
|
|---|
| 769 | PQFILEDATA32 pFile = pInfo->pFileData;
|
|---|
| 770 | while ( (pFile)
|
|---|
| 771 | && (pFile->ulRecType == 8) // this is necessary, we'll crash otherwise!!
|
|---|
| 772 | )
|
|---|
| 773 | {
|
|---|
| 774 | ULONG ul;
|
|---|
| 775 | // for some reason, there is an array in the file struct,
|
|---|
| 776 | // so search the array for the SFN
|
|---|
| 777 | for (ul = 0;
|
|---|
| 778 | ul < pFile->ulCFiles;
|
|---|
| 779 | ul++)
|
|---|
| 780 | {
|
|---|
| 781 | if (pFile->paFiles[ul].usSFN == usFileID)
|
|---|
| 782 | return pFile;
|
|---|
| 783 | }
|
|---|
| 784 |
|
|---|
| 785 | pFile = pFile->pNext;
|
|---|
| 786 | }
|
|---|
| 787 |
|
|---|
| 788 | return NULL;
|
|---|
| 789 | }
|
|---|
| 790 |
|
|---|
| 791 |
|
|---|