in reply to Win32::API Help !

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?

Replies are listed 'Best First'.
Re^2: Win32::API Help !
by shonorio (Hermit) on Apr 09, 2005 at 21:30 UTC
    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?