How to get Windows physical RAM using python?

Branimir Petrovic BranimirPetrovic at yahoo.com
Wed Jul 30 20:01:59 EDT 2003


"Martin v. Löwis" <martin at v.loewis.de> wrote in message news:<bg920a$9pt$05$1 at news.t-online.com>...
> Python can't get you Windows physical RAM. You have to order RAM
> from a manufacturer or reseller if Windows needs more RAM :-)
> 
> Regards,
> Martin

"Easy" way to get to physical RAM on Windows is via WMI objects.

Before I've discovered (why/how use) Python this example below is
how I used to do it from JScript (did not "re-tool" and converted 
all my stuff to Python yet), but should be good enough to give 
you an idea of what to look for:


function getSysInfo(sHost) {
    //  Returns instance of Object object with these expando
properties:
    //
    //      oObj.Computer       NetBIOS computer name
    //      oObj.OSname         "Microsoft Windows 2000 Professional"
    //      oObj.OSver          4.0.1398 (for WinNT4), 5.0.2195 (for
Win2K), 5.1.??? (for WinXP)
    //      oObj.SPver          2 (for SP2), ...
    //      oObj.OStype         "W" (for Workstation), "S" (for
Server)
    //      oObj.IPaddr         "123.45.67.89"
    //      oObj.WinDir         "C:\Winnt"
    //      oObj.SysDir         "C:\Winnt\system32"
    //      oObj.RAM            In MB
    //      oObj.BootDev        "\Device\Harddisk0\Partition1"
    //      oObj.SysDev         "\Device\Harddisk0\Partition1"
    //      oObj.ERRMSG         will be "" or hold error message
string
    //      oObj.ERRNO          will be:
    //                              0 if there are no errors,
    //                              1 if system does not exist or is
unavailable,
    //                              2 if WMI is not installed on
target system,
    //                              3 other reason(s)...

    var oObj = new Object();

    if (!isIP(sHost)) {
        var oP = new PingerCLS();
        oObj.IPaddr = oP.getIP(sHost);
    } else oObj.IPaddr = sHost;

    try {
        var oOSset =
GetObject("winmgmts:{impersonationLevel=impersonate}!//"+sHost).InstancesOf("Win32_OperatingSystem")
        oObj.ERRNO = 0;
        oObj.ERRMSG = "NO ERRORS";
    } catch(e) {
        switch (e.number) {
            case 462 :
            case -2146827826 :
                //  The remote server machine does not exist or is
unavailable
                oObj.ERRNO = 1;
                oObj.ERRMSG = "Host does not exist or is not
available.";
                return oObj;
            case 429 :
            case -2146827859 :
                //  ActiveX component can't create object
                oObj.ERRNO = 2;
                oObj.ERRMSG = "Host is missing WMI capabilities.";
                return oObj;
            case -2147217405 :
                //  Lacking admim permissions to connect to host
                oObj.ERRNO = 3;
                oObj.ERRMSG = "Missing permissions to connect to this
host.";
                return oObj;
            case -2146828218 :
                //  "Permission denied" - DCOM not enabled error
message
                oObj.ERRNO = 4;
                oObj.ERRMSG = "DCOM not enabled on host: " + sHost;
                return oObj;
            case -2147418111 :
                //  Wierd shit error (only number, no message)
                //  Happens on 2 Win2K Prof with CheckPoint's
client...
                oObj.ERRNO = 5;
                oObj.ERRMSG = "Wierd one (ERROR# -2147418111)...";
                return oObj;
            default :
                //  Whatever else the cause may be...
                oObj.ERRNO = e.number;
                oObj.ERRMSG = e.description;
                return oObj;
        }
    }

    var oEnm_1 = new Enumerator(oOSset);
    var oEnm_2;

    for (; !oEnm_1.atEnd(); oEnm_1.moveNext()) {
        oEnm_2 = new Enumerator(oEnm_1.item().Properties_);

        for (; !oEnm_2.atEnd(); oEnm_2.moveNext()) {
            //  Pick only some of available properties (and ignores
the rest):
            switch (oEnm_2.item().Name) {
                case "CSName" :
                    oObj.Computer = oEnm_2.item().Value;
                    continue;
                case "Caption" :
                    oObj.OSname = oEnm_2.item().Value;
                    oObj.OStype =
(/Server/i).test(oEnm_2.item().Value)? "S":"W"
                    continue;
                case "Version" :
                    oObj.OSver = oEnm_2.item().Value;
                    continue;
                case "ServicePackMajorVersion" :
                    oObj.SPver = oEnm_2.item().Value;
                    continue;
                case "WindowsDirectory" :
                    oObj.WinDir = oEnm_2.item().Value;
                    continue;
                case "SystemDirectory" :
                    oObj.SysDir = oEnm_2.item().Value;
                    continue;
                case "TotalVisibleMemorySize" :
                    oObj.RAM = Math.round(oEnm_2.item().Value/1024);
                    continue;
                case "BootDevice" :
                    oObj.BootDev = oEnm_2.item().Value;
                    continue;
                case "SystemDevice" :
                    oObj.SysDev = oEnm_2.item().Value;
                    continue;
                default :
                    continue;
            }
        }
    }

    return oObj;                                        //  >>>

}

Branimir




More information about the Python-list mailing list