Last active
November 12, 2021 13:58
-
-
Save ayancey/b3399177f9072db88d8a58559f1e3503 to your computer and use it in GitHub Desktop.
Gets the type of RAM in Windows, with raw SMBIOS data. Works even if Win32_PhysicalMemory.MemoryType is Unknown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import wmi | |
import struct | |
# RAM memory types described in 7.18.2 in SMBIOS Specification | |
MEMORY_TYPES = {1: 'Other', 2: 'Unknown', 3: 'DRAM', 4: 'EDRAM', 5: 'VRAM', 6: 'SRAM', 7: 'RAM', 8: 'ROM', 9: 'FLASH', 10: 'EEPROM', 11: 'FEPROM', 12: 'EPROM', 13: 'CDRAM', 14: '3DRAM', 15: 'SDRAM', 16: 'SGRAM', 17: 'RDRAM', 18: 'DDR', 19: 'DDR2', 20: 'DDR2 FB-DIMM', 21: 'Reserved', 22: 'Reserved', 23: 'Reserved', 24: 'DDR3', 25: 'FBD2'} | |
def Get_SMBIOS(w): | |
SMBIOSTableData = "" | |
for i in w.query("SELECT * FROM MSSmBios_RawSMBiosTables")[0].SMBiosData: | |
SMBIOSTableData += chr(i) | |
return SMBIOSTableData | |
# Thanks to MyNameIsMeerkat on GitHub for this method | |
def Parse_SMBIOS(smb): | |
table_data = {} | |
while 1: | |
try: | |
formatted_len = struct.unpack("<B", smb[1])[0] | |
except IndexError: | |
# Reached the end of the structure | |
break | |
handle = struct.unpack("<H", smb[2:4])[0] | |
unformatted_len = smb[formatted_len:].find(struct.pack("<H", 0)) + 2 | |
table_data[handle] = smb[: formatted_len + unformatted_len] | |
smb = smb[formatted_len + unformatted_len:] | |
return table_data | |
def Get_MemoryType(sm): | |
mem_type = '' | |
for i in sm.values(): | |
if ord(i[0]) == 17: | |
mem_type = MEMORY_TYPES[ord(i[18])] | |
return mem_type | |
smbios_raw = Get_SMBIOS(wmi.WMI(namespace="WMI")) | |
smbios_parsed = Parse_SMBIOS(smbios_raw) | |
print Get_MemoryType(smbios_parsed) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment