Oh where, oh where has my ToolHelp gone . . .

Dear Dr GUI,

I’ve lost my ToolHelp—do you think you could help me find it? My ToolHelp32 code works just fine in Windows 95 but causes all sorts of GPFs in Windows NT 4.0. I’ve searched without success through the whole MSDN and all my hundreds of books and I can’t find NT 4’s version of the Toolhelp32 functions. They must be there somewhere or Task Manager could not work.

Yours in hope,
Nigel [Ed: No, he’s not Nigel Thompson]

Dr. GUI replies:

It seems like everyone is losing things nowadays. Dr. GUI, for instance, sometimes feels as though he’s losing his mind, especially when listening to Scott MacNealy. (Or is it that the good doctor feels he’s losing his temper?)

Windows NT does not implement TOOLHELP32. Instead you can use the psapi.dll that is shipped with the Win32 SDK to accomplish nearly everything that TOOLHELP32 accomplishes on Windows 95. You can, for example, enumerate the list of processes in the system, as well as enumerate the list of DLLs in each process. There are also APIs to get loaded device driver information as well as process memory usage information, working set information, and memory-mapped file information.

The PSAPI DLL can be found in the BIN directory of the Win32 SDK; psapi.h and psapi.lib can be found as a part of a Win32 SDK sample in the samples\sdktools\image\winnt\pfmon directory.

There are two Microsoft Systems Journal articles in the “Under the Hood” column by Matt Pietrek (August 1996 and November 1996) that cover pretty much everything that is in PSAPI.

If you want to do this in Java, either use a native interface or, better yet, write wrappers for the functions you need as COM objects and call the COM objects from Java. Dr. GUI prefers using COM objects despite the fact that they’re not even 99 44/100% pure Java, let alone 100% pure Java. Why? Well, they’re no harder to write than the wrapper DLLs you’d need to use a native interface, they allow the Microsoft Java VM to deal with multithreading and garbage collection issues for you, and they’re reusable in other languages (such as Visual Basic) on your Windows system. Remember, you’re not going to see psapi.dll on Solaris anyway.

Also, if you’re doing Windows development in Java, be sure to check out the classes provided in the Microsoft SDK for Java 2.0—they include many classes that provide access to the Windows and DirectX APIs.