www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Windows PSAPI

reply "sleek" <cslush gmail.com> writes:
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.

P.S.
I built psapi.lib by doing:
implib /noi /system psapi.lib c:\windows\system32\psapi.dll

Also, I'm using the "bindings" project for the psapi.d module. 


begin 666 dependencies.d
M;6]D=6QE(&1E<&5N9&5N8VEE<SL-" T*:6UP;W)T('1A;F=O+FEO+E-T9&]U
M=#L-" T*+R\ 1G)O;2!T:&4 8FEN9&EN9W, <')O:F5C= T*:6UP;W)T('=I
M;C,R+G!S87!I.PT*:6UP;W)T('=I;C,R+G=I;F1E9CL-" T*97AT97)N("A7
M:6YD;W=S*0T*>PT*("  ($A!3D1,12!/<&5N4')O8V5S<RA$5T]21"!D=T1E
M<VER961!8V-E<W,L($)/3TP 8DEN:&5R:71(86YD;&4L($173U)$(&1W4')O
M8V5S<TED*3L-"B  ("!"3T],($-L;W-E2&%N9&QE*$A!3D1,12!H3V)J96-T
M*3L-"B  ("!$5T]21"!'971,87-T17)R;W(H*3L-"GT-" T*=F]I9"!S:&]W


M;W5N=#L-"B  ("!I9B H(45N=6U0<F]C97-S36]D=6QE<RAH4')O8V5S<RP 





M(#T 1V5T36]D=6QE1FEL94YA;65%>$$H:%!R;V-E<W,L(&A-;V0L(&9I;&5.
M86UE+G!T<BP 9FEL94YA;64N;&5N9W1H*3L-"B  ("  ("  :68 *"%L96YG

M17)R;W(Z('M](BP 1V5T3&%S=$5R<F]R*"DI.PT*("  ("  ("  ("  <F5T

M;F=T:#L-

M("  2$%.1$Q%(&A0<F]C97-S(#T 3W!E;E!R;V-E<W,H4%)/0T534U]15452
M65])3D9/4DU!5$E/3B!\(%!23T-%4U-?5DU?4D5!1"!\(%!23T-%4U-?5$52
M34E.051%+"!F86QS92P <&ED*3L-"B  ("!I9B H(6A0<F]C97-S*0T*("  
M('L-"B  ("  ("  <F5T=7)N.PT*("  ('T-" T*("  ('-H;W=-;V1U;&5S
M*&A0<F]C97-S*3L-" T*("  ($-L;W-E2&%N9&QE*&A0<F]C97-S*3L-"GT-
M" T*=F]I9"!S:&]W4')O8V5S<V5S*"D-"GL-"B  ("!$5T]21%M=('!R;V-E
M<W-E<SL-"B  ("!P<F]C97-S97,N;&5N9W1H(#T ,C4V.PT*("  ($173U)$

M97,N<'1R+"!P<F]C97-S97,N;&5N9W1H("H 1%=/4D0N<VEZ96]F+" F8GET


M<F]C97-S97,N;&5N9W1H(#T 8GET94-O=6YT("\ 1%=/4D0N<VEZ96]F.PT*

M("  ("!I9B H<&ED(#T
M("  ("  ("!3=&1O=70N9F]R;6%T;&XH(E!)1#H >WTB+"!P:60I.PT*("  

`
end

begin 666 main.d


`
end
Sep 06 2008
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
sleek <cslush 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 );
Sep 07 2008
next sibling parent reply "sleek" <cslush gmail.com> writes:
Sergey,

Thanks for the response. Lucky for me, I actually fixed those first couple 
bugs you mentioned after looking at the code a bit more. Unluckily for me, 
it still sounds like I'm somewhat screwed. Does anyone else out there have 
any info as to how I can use PSAPI from D?

"Sergey Gromov" <snake.scaly gmail.com> wrote in message 
news:MPG.232e39d36d5254929896bb news.digitalmars.com...
 sleek <cslush 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 );
Sep 07 2008
parent reply Sergey Gromov <snake.scaly gmail.com> writes:
sleek <cslush gmail.com> wrote:
 Sergey,
 
 Thanks for the response. Lucky for me, I actually fixed those first couple 
 bugs you mentioned after looking at the code a bit more. Unluckily for me, 
 it still sounds like I'm somewhat screwed. Does anyone else out there have 
 any info as to how I can use PSAPI from D?
See attached. These are the correct import library and fixed bindings. It wasn't easy to create the library though. First I've got Psapi.h from Microsoft Platform SDK and modified it as suggested in this article: http://support.microsoft.com/kb/131313 Then I went the hard way which is of no interest. The right way is to compile the header into an obj file and then dump its symbols. The compiler to use depends on what tools you've got handy. I've got the complete Microsoft SDK so I used cl -c psapi.c dumpbin /symbols psapi.obj >psapi.def Remove everything except the names, add 'LIBRARY "psapi.dll"' and 'EXPORTS' at the top, and you're almost done with the .def file. "Almost" because functions in actual DLL are not mangled, so you need to specify an internal name. Use any regular expression tool to convert "_Anything digits" into "_Anything digits = Anything". That's it. Now use implib psapi psapi.def to produce the correct import library. I wonder if there is an easier way.
Sep 07 2008
next sibling parent reply wyverex <wyverex.cypher gmail.com> writes:
Sergey Gromov wrote:
 sleek <cslush gmail.com> wrote:
 Sergey,

 Thanks for the response. Lucky for me, I actually fixed those first couple 
 bugs you mentioned after looking at the code a bit more. Unluckily for me, 
 it still sounds like I'm somewhat screwed. Does anyone else out there have 
 any info as to how I can use PSAPI from D?
See attached. These are the correct import library and fixed bindings. It wasn't easy to create the library though. First I've got Psapi.h from Microsoft Platform SDK and modified it as suggested in this article: http://support.microsoft.com/kb/131313 Then I went the hard way which is of no interest. The right way is to compile the header into an obj file and then dump its symbols. The compiler to use depends on what tools you've got handy. I've got the complete Microsoft SDK so I used cl -c psapi.c dumpbin /symbols psapi.obj >psapi.def Remove everything except the names, add 'LIBRARY "psapi.dll"' and 'EXPORTS' at the top, and you're almost done with the .def file. "Almost" because functions in actual DLL are not mangled, so you need to specify an internal name. Use any regular expression tool to convert "_Anything digits" into "_Anything digits = Anything". That's it. Now use implib psapi psapi.def to produce the correct import library. I wonder if there is an easier way.
Always try using linkdef http://www.dprogramming.com/linkdef.php
Sep 07 2008
next sibling parent "sleek" <cslush gmail.com> writes:
Another great tip. Thanks, I'll look into this one next time I need to 
create a custom lib file.

"wyverex" <wyverex.cypher gmail.com> wrote in message 
news:ga18jq$1h5m$1 digitalmars.com...
 Sergey Gromov wrote:
 sleek <cslush gmail.com> wrote:
 Sergey,

 Thanks for the response. Lucky for me, I actually fixed those first 
 couple bugs you mentioned after looking at the code a bit more. 
 Unluckily for me, it still sounds like I'm somewhat screwed. Does anyone 
 else out there have any info as to how I can use PSAPI from D?
See attached. These are the correct import library and fixed bindings. It wasn't easy to create the library though. First I've got Psapi.h from Microsoft Platform SDK and modified it as suggested in this article: http://support.microsoft.com/kb/131313 Then I went the hard way which is of no interest. The right way is to compile the header into an obj file and then dump its symbols. The compiler to use depends on what tools you've got handy. I've got the complete Microsoft SDK so I used cl -c psapi.c dumpbin /symbols psapi.obj >psapi.def Remove everything except the names, add 'LIBRARY "psapi.dll"' and 'EXPORTS' at the top, and you're almost done with the .def file. "Almost" because functions in actual DLL are not mangled, so you need to specify an internal name. Use any regular expression tool to convert "_Anything digits" into "_Anything digits = Anything". That's it. Now use implib psapi psapi.def to produce the correct import library. I wonder if there is an easier way.
Always try using linkdef http://www.dprogramming.com/linkdef.php
Sep 07 2008
prev sibling parent Sergey Gromov <snake.scaly gmail.com> writes:
wyverex <wyverex.cypher gmail.com> wrote:
 Always try using linkdef
 http://www.dprogramming.com/linkdef.php
What a nice tool. I wish it were mentioned somewhere in D docs concerning the DLL usage.
Sep 07 2008
prev sibling parent "sleek" <cslush gmail.com> writes:
Sergey,

Right now you are my friggin hero! I greatly appreciate your contribution 
here. I just replaced the necessary pieces in my code and it now works 
exactly as expected! Thanks so much for the help!

"Sergey Gromov" <snake.scaly gmail.com> wrote in message 
news:MPG.232e5cf6426b533d9896bc news.digitalmars.com...
 sleek <cslush gmail.com> wrote:
 Sergey,

 Thanks for the response. Lucky for me, I actually fixed those first 
 couple
 bugs you mentioned after looking at the code a bit more. Unluckily for 
 me,
 it still sounds like I'm somewhat screwed. Does anyone else out there 
 have
 any info as to how I can use PSAPI from D?
See attached. These are the correct import library and fixed bindings. It wasn't easy to create the library though. First I've got Psapi.h from Microsoft Platform SDK and modified it as suggested in this article: http://support.microsoft.com/kb/131313 Then I went the hard way which is of no interest. The right way is to compile the header into an obj file and then dump its symbols. The compiler to use depends on what tools you've got handy. I've got the complete Microsoft SDK so I used cl -c psapi.c dumpbin /symbols psapi.obj >psapi.def Remove everything except the names, add 'LIBRARY "psapi.dll"' and 'EXPORTS' at the top, and you're almost done with the .def file. "Almost" because functions in actual DLL are not mangled, so you need to specify an internal name. Use any regular expression tool to convert "_Anything digits" into "_Anything digits = Anything". That's it. Now use implib psapi psapi.def to produce the correct import library. I wonder if there is an easier way.
Sep 07 2008
prev sibling parent reply torhu <no spam.invalid> writes:
Sergey Gromov wrote:
 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:
Using coffimplib with a psapi.lib taken from the M$ platform SDK will probably work. The stdcall mangling is present in the .lib file, but not the .dll. I didn't test the resulting OMF lib file, but the names are correctly mangled, "_GetModuleFileNameExA 16" and so on. http://ftp.digitalmars.com/coffimplib.zip
Sep 07 2008
parent Sergey Gromov <snake.scaly gmail.com> writes:
torhu <no spam.invalid> wrote:
 Sergey Gromov wrote:
 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:
Using coffimplib with a psapi.lib taken from the M$ platform SDK will probably work. The stdcall mangling is present in the .lib file, but not the .dll. I didn't test the resulting OMF lib file, but the names are correctly mangled, "_GetModuleFileNameExA 16" and so on. http://ftp.digitalmars.com/coffimplib.zip
Thank you, this is another fine tool that works.
Sep 08 2008