in reply to How do I code GlobalMemoryStatusEx in perl

Win32::API::Struct->typedef(MEMORYSTATUSEX { DWORD dwLength; ...

For one, you probably want some Perl-compatible syntax :)

Win32::API::Struct->typedef(MEMORYSTATUSEX => qw{ DWORD dwLength; ... });

From the docs:

typedef NAME, TYPE, MEMBER, TYPE, MEMBER, ...

This method defines a structure named NAME. The definition consists of types and member names, just like in C. In fact, most of the times you can cut the C definition for a structure and paste it verbatim to your script, enclosing it in a qw() block. The function takes care of removing the semicolon after the member name.

Replies are listed 'Best First'.
Re^2: How do I code GlobalMemoryStatusEx in perl
by azaragoza (Acolyte) on Mar 12, 2009 at 02:06 UTC
    Thanks for all the help because of the help I was able to code the perl script, but it does not return any values. Does anyone now why?
    use warnings; use Win32::API; use Win32::API::Struct; use Data::Dumper; use Win32::SystemInfo; use strict; typedef Win32::API::Struct MEMORYSTATUSEX => qw{ DWORD dwLength; DWORD dwMemoryLoad; DWORDLONG ullTotalPhys; DWORDLONG ullAvailPhys; DWORDLONG ullTotalPageFile; DWORDLONG ullAvailPageFile; DWORDLONG ullTotalVirtual; DWORDLONG ullAvailVirtual; DWORDLONG ullAvailExtendedVirtual; }; Win32::API->Import( 'kernel32', 'BOOL GlobalMemoryStatusEx( LPMEMORYSTATUSEX lpBuffer +)' ); my $stat = Win32::API::Struct->new( 'MEMORYSTATUSEX' ); GlobalMemoryStatusEx($stat); my $dwLength = FormatBytes( $stat->{dwLength} ); print "\nSize of memory status structure: $dwLength\n"; my $ullTotalPhys = FormatBytes( $stat->{ullTotalPhys} ); print "Total Available physical memory --> $ullTotalPhys\n\n"; printf "Memory in use: %ld%%\n", $stat->{dwMemoryLoad}; my $ullAvailPhys = FormatBytes( $stat->{ullAvailPhys} ); print "Free physical memory ---> $ullAvailPhys \n"; my $ullTotalVirtual = FormatBytes( $stat->{ullTotalVirtual} ); print "Total virtual memory ---> $ullTotalVirtual \n"; my $ullAvailVirtual = FormatBytes( $stat->{ullAvailVirtual} ); print "Free virtual memory ----> $ullAvailVirtual\n"; my $ullTotalPageFile = FormatBytes( $stat->{ullTotalPageFile} ); print "Total paging file ------> $ullTotalPageFile\n"; my $ullAvailPageFile = FormatBytes( $stat->{ullAvailPageFile} ); print "Free paging file -------> $ullAvailPageFile\n\n"; #--------------------------------------------------------------------- +------ # sub FormatBytes { my( $Number ) = @_; my( $Suffix ) = ""; my $K = 1024; my $M = 1024 * $K; my $G = 1024 * $M; if( $G <= $Number ) { $Suffix = "(G)"; $Number /= $G; } elsif( $M <= $Number ) { $Suffix = "(M)"; $Number /= $M; } elsif( $K <= $Number ) { $Suffix = "(K)"; $Number /= $K; } $Number =~ s/(\.\d{0,2})\d*$/$1/; {} while ($Number =~ s/^(-?\d+)(\d{3})/$1,$2/); return( $Number . $Suffix ); } #--------------------------------------------------------------------- +------
    As I looked for answers in the web I found a lot of questions on how to do this with perl, unfortunately I have not been successful.