in reply to How do I code GlobalMemoryStatusEx in perl

Hi azaragoza,
Here's an Inline::C solution:
use warnings; use Inline C => Config => BUILD_NOISY => 1; use Inline C => <<'EOC'; #define _WIN32_WINNT 0x0500 #include <windows.h> #define DIV 1024 char *divisor = "K"; #define WIDTH 7 void foo() { MEMORYSTATUSEX statex; statex.dwLength = sizeof (statex); GlobalMemoryStatusEx (&statex); printf ("%ld percent of memory is in use.\n", statex.dwMemoryLoad); printf ("There are %*I64d total %sbytes of physical memory.\n", WIDTH, statex.ullTotalPhys/DIV, divisor); printf ("There are %*I64d free %sbytes of physical memory.\n", WIDTH, statex.ullAvailPhys/DIV, divisor); printf ("There are %*I64d total %sbytes of paging file.\n", WIDTH, statex.ullTotalPageFile/DIV, divisor); printf ("There are %*I64d free %sbytes of paging file.\n", WIDTH, statex.ullAvailPageFile/DIV, divisor); printf ("There are %*I64d total %sbytes of virtual memory.\n", WIDTH, statex.ullTotalVirtual/DIV, divisor); printf ("There are %*I64d free %sbytes of virtual memory.\n", WIDTH, statex.ullAvailVirtual/DIV, divisor); // Show the amount of extended memory available. printf ("There are %*I64x free %sbytes of extended memory.\n", WIDTH, statex.ullAvailExtendedVirtual/DIV, divisor); } void bar() { Inline_Stack_Vars; MEMORYSTATUSEX statex; char buffer [30]; statex.dwLength = sizeof (statex); GlobalMemoryStatusEx (&statex); Inline_Stack_Reset; Inline_Stack_Push(sv_2mortal(newSVuv(statex.dwMemoryLoad))); _ui64toa(statex.ullTotalPhys/DIV, buffer, 10); Inline_Stack_Push(sv_2mortal(newSVpv(buffer, 0))); _ui64toa(statex.ullAvailPhys/DIV, buffer, 10); Inline_Stack_Push(sv_2mortal(newSVpv(buffer, 0))); _ui64toa(statex.ullTotalPageFile/DIV, buffer, 10); Inline_Stack_Push(sv_2mortal(newSVpv(buffer, 0))); _ui64toa(statex.ullAvailPageFile/DIV, buffer, 10); Inline_Stack_Push(sv_2mortal(newSVpv(buffer, 0))); _ui64toa(statex.ullTotalVirtual/DIV, buffer, 10); Inline_Stack_Push(sv_2mortal(newSVpv(buffer, 0))); _ui64toa(statex.ullAvailVirtual/DIV, buffer, 10); Inline_Stack_Push(sv_2mortal(newSVpv(buffer, 0))); _ui64toa(statex.ullAvailExtendedVirtual/DIV, buffer, 10); Inline_Stack_Push(sv_2mortal(newSVpv(buffer, 0))); Inline_Stack_Done; Inline_Stack_Return(8); } EOC foo(); @info = bar(); print "\n"; print "$_\n" for @info;
The foo function simply prints the values to stdout.
The bar function returns the same values to an array.

Unfortunately, it works only with Microsoft compilers, but not MinGW. I don't know why GlobalMemoryStatusEx is inaccessible to MinGW - working out why should help fill in my day :-)

Cheers,
Rob

Replies are listed 'Best First'.
Re^2: How do I code GlobalMemoryStatusEx in perl
by syphilis (Archbishop) on Mar 12, 2009 at 02:24 UTC
    Unfortunately, it works only with Microsoft compilers, but not MinGW

    I think the problem with MinGW is that _WIN32_WINNT does not get defined to 0x0500 early enough. A solution is to insert the following code into the script (just above the foo function):
    #ifndef _MSC_VER typedef struct _MEMORYSTATUSEX { DWORD dwLength; DWORD dwMemoryLoad; DWORDLONG ullTotalPhys; DWORDLONG ullAvailPhys; DWORDLONG ullTotalPageFile; DWORDLONG ullAvailPageFile; DWORDLONG ullTotalVirtual; DWORDLONG ullAvailVirtual; DWORDLONG ullAvailExtendedVirtual; } MEMORYSTATUSEX,*LPMEMORYSTATUSEX; WINBASEAPI BOOL WINAPI GlobalMemoryStatusEx(LPMEMORYSTATUSEX); #endif
    Works for me, anyway :-)

    Cheers,
    Rob
Re^2: How do I code GlobalMemoryStatusEx in perl
by azaragoza (Acolyte) on Mar 12, 2009 at 02:24 UTC
    Thanks Rob I'll try an find a C compiler and use this until I can get the perl script working.
      I'll try an find a C compiler

      If you're using ActivePerl:
      ppm install MinGW
      Cheers,
      Rob