shonorio has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
I trying to work with the APIs Process32First and Process32Next using Windows API Guide: Process32First Function as a sample.

For some reason that I don't figure out yeat, my code are not returning the expected value. I've look at my place, MSDN included, to check where is problem without success.

Because this, I'm asking for help here.

Thanks.

use Win32::API; Win32::API::Struct->typedef( PROCESSENTRY32 => qw{ LONG dwSize; LONG cntUsage; LONG th32ProcessID; LONG th32DefaultHeapID; LONG th32ModuleID; LONG cntThreads; LONG th32ParentProcessID; LONG pcPriClassBase; LONG dwFlags; CHAR szExeFile[260]; }); Win32::API->Import('kernel32.dll', "HANDLE CreateToolhelp32Snapshot(DW +ORD dwFlags,DWORD th32ProcessID)"); Win32::API->Import('kernel32.dll', "BOOL Process32First( HANDLE hSnaps +hot, LPPROCESSENTRY32 lppe )" ); Win32::API->Import('kernel32.dll', "BOOL Process32Next( HANDLE hSnapsh +ot, LPPROCESSENTRY32 lppe )" ); my $TH32CS_SNAPPROCESS = 0x2; my $Snapshot = undef; my $ProcessInfo = Win32::API::Struct->new('PROCESSENTRY32'); $Snapshot = CreateToolhelp32Snapshot($TH32CS_SNAPPROCESS, 0); $ProcessInfo->{dwSize} = Win32::API::Struct::sizeof ($ProcessInfo); $Sucess = Process32First($Snapshot, $ProcessInfo); print "Fist Process : $ProcessInfo->{szExeFile}\n"; #are returning not +hing
Solli Moreira Honorio
Sao Paulo - Brazil

Replies are listed 'Best First'.
Re: Win32::API Help !
by BrowserUk (Patriarch) on Apr 09, 2005 at 20:09 UTC

    It seems that Win32::API::Struct::Sizeof is returning the wrong size. Setting it to 296 and your code works:

    #! perl -slw use strict; use Data::Dumper; use Win32::API; Win32::API::Struct->typedef( PROCESSENTRY32 => qw{ LONG dwSize; LONG cntUsage; LONG th32ProcessID; LONG th32DefaultHeapID; LONG th32ModuleID; LONG cntThreads; LONG th32ParentProcessID; LONG pcPriClassBase; LONG dwFlags; CHAR szExeFile[260]; }) ; Win32::API->Import('kernel32.dll', "HANDLE CreateToolhelp32Snapshot(DW +ORD dwFlags,DWORD th32ProcessID)"); Win32::API->Import('kernel32.dll', "BOOL Process32First( HANDLE hSnaps +hot, LPPROCESSENTRY32 lppe )" ); Win32::API->Import('kernel32.dll', "BOOL Process32Next( HANDLE hSnapsh +ot, LPPROCESSENTRY32 lppe )" ); my $TH32CS_SNAPPROCESS = 0x2; $^W=0; my $Snapshot = undef; my $ProcessInfo = Win32::API::Struct->new('PROCESSENTRY32') or die $^E +; $Snapshot = CreateToolhelp32Snapshot($TH32CS_SNAPPROCESS, 0) or die $^ +E; $ProcessInfo->{dwSize} = 296; #Win32::API::Struct::sizeof ($ProcessInf +o); my $Sucess = Process32First( $Snapshot, $ProcessInfo) or die $^E; print "First Process : $ProcessInfo->{szExeFile}\n"; #are returning no +thing #print Dumper $ProcessInfo; while( Process32Next( $Snapshot, $ProcessInfo ) ) { print $ProcessInfo->{szExeFile}; # print Dumper $ProcessInfo; } __END__ P:\test>446283 First Process : [System Process] System SMSS.EXE CSRSS.EXE WINLOGON.EXE SERVICES.EXE LSASS.EXE SVCHOST.EXE SVCHOST.EXE SVCHOST.EXE SVCHOST.EXE vsmon.exe EXPLORER.EXE zlclient.exe TASKMGR.EXE mdm.exe CMD.EXE CMD.EXE CMD.EXE CMD.EXE CMD.EXE CMD.EXE opera.exe perl.exe

    I worked out what was wrong by adding some error checking which produced the error text: "The program issued a command but the command length is incorrect" from the call to Process32First().


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco.
    Rule 1 has a caveat! -- Who broke the cabal?
      Thanks for your help, and my apologize for I didn't check the errors. But i'd like know how did you get que correct size ?

      Solli Moreira Honorio
      Sao Paulo - Brazil
        But i'd like know how did you get que correct size ?

        LONG dwSize; # 4 LONG cntUsage; # 4 LONG th32ProcessID; # 4 LONG th32DefaultHeapID; # 4 LONG th32ModuleID; # 4 LONG cntThreads; # 4 LONG th32ParentProcessID; # 4 LONG pcPriClassBase; # 4 LONG dwFlags; # 4 CHAR szExeFile[260]; # 260 # = 296

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco.
        Rule 1 has a caveat! -- Who broke the cabal?