Windows PSAPI

Sergey Gromov snake.scaly at gmail.com
Sun Sep 7 09:20:22 PDT 2008


sleek <cslush at gmail.com> wrote:
> Hi, I'm trying to get a list of all processes running on my machine and the 
> dlls that are in use by those processes. Windows provides the PSAPI to 
> perform these tasks. However, the code I'm trying to run isn't working as 
> expected. I can get the PIDs of the processes, but beyond that, weird things 
> occur.
> 
> Can anyone offer some assistance? I have attached the code in question.

Firstly, there are some obvious bugs:

    DWORD count;
    if (!EnumProcessModules(hProcess, hMods.ptr,
        hMods.sizeof, &count))
    {
        return;
    }

    hMods.length = count / DWORD.sizeof;

must be

    DWORD count;
    if (!EnumProcessModules(hProcess, hMods.ptr,
        hMods.length * HMODULE.sizeof, &count))
    {
        return;
    }

    hMods.length = count / HMODULE.sizeof;

But there is one more important and crucial.

The call to GetModuleFileNameExA() messes up stack.  As far as I can 
tell this happens because PSAPI functions are declared extern(C) in 
psapi.d bindings, but in fact are __stdcall.  The problem is, it's not 
possible to replace extern(C) with extern(Windows) because the psapi.dll 
functions have unmangled names, while extern(Windows) mangles them with 
argument stack size.  I don't have a slightest idea of how to specify 
mangling convention and calling convention separately in D. In C 
declaration looks like:

extern "C"
DWORD
WINAPI
GetModuleFileNameExA(
    HANDLE hProcess,
    HMODULE hModule,
    LPSTR lpFilename,
    DWORD nSize
    );


More information about the Digitalmars-d-learn mailing list