Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

RE: (Guildenstern: TMTOWTDI): Win32::MemoryInfo

by Guildenstern (Deacon)
on Sep 01, 2000 at 00:48 UTC ( #30631=note: print w/ replies, xml ) Need Help??


in reply to RE: RE: RE: Win32::MemoryInfo
in thread Win32::MemoryInfo

Here's what I came up with after some reworking. I decided to go with a hash reference as the main parameter instead of taking words. This also means that it must now be called like this:

my %hMem = (TotalPhys =>0, AvailPhys =>0); Win32::MemoryInfo::MemoryStatus(%hMem,"MB");

I don't know what the consensus is on putting values into a hash like that, but if someone doesn't check the return value at least they won't get bit with undef values.
Here's the reworked function:
sub MemoryStatus (\%;$) { my $return = shift; my $ret_type = shift; my %fmt_types = ( B => 1, KB => 1024, MB => 1024*1024, GB => 1024*1024*1024); my @params = qw(MemLoad TotalPhys AvailPhys TotalPage AvailPage TotalVirtual AvailVirtual); my %results; my $MemFormat; my $dwMSLength; $MemFormat = ($ret_type =~ /^[BKMG]B?$/) ? $fmt_types{$ret_type} : $fmt_types{B} +; my $GlobalMemoryStatus ||= new Win32::API("kernel32", "GlobalMemoryStatus", ["P"], "V") or ret +urn undef; my $MEMORYSTATUS = pack "L8",(0, 0, 0, 0, 0, 0, 0, 0); $GlobalMemoryStatus->Call($MEMORYSTATUS); ($dwMSLength, @results{@params}) = unpack "L8", $MEMORYSTATUS; return undef if ($dwMSLength == 0); if (keys(%$return) == 0) { foreach (@params) { $return->{$_} = ($_ eq "MemLoad") ? $results{$_} : $results{$_}/$M +emFormat; } } else { foreach (@params){ $return->{$_} = $results{$_}/$MemFormat unless (!defined($return- +>{$_})); } } 1; }


Guildenstern
Negaterd character class uber alles!


Comment on RE: (Guildenstern: TMTOWTDI): Win32::MemoryInfo
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://30631]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (14)
As of 2015-07-06 17:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (77 votes), past polls