Skip to content

Instantly share code, notes, and snippets.

@clalancette
Created April 20, 2021 02:03
Show Gist options
  • Save clalancette/43d79a7357da355e33b0c4f2bb012e22 to your computer and use it in GitHub Desktop.
Save clalancette/43d79a7357da355e33b0c4f2bb012e22 to your computer and use it in GitHub Desktop.
Get CD information from Windows WMI without wmi library
import win32com.client
def get_raw_windows_device_size():
obj = win32com.client.GetObject("winmgmts:")
typecomp = obj._oleobj_.GetTypeInfo().GetContainingTypeLib()[0].GetTypeComp()
result = typecomp.Bind('wbemFlagReturnImmediately')
if not result[0]:
raise AttributeError(name)
wbemFlagReturnImmediately = result[1].value
result = typecomp.Bind('wbemFlagForwardOnly')
if not result[0]:
raise AttributeError(name)
wbemFlagForwardOnly = result[1].value
query = "SELECT * FROM Win32_CDROMDrive WHERE Drive = 'D:'"
wql = query.replace("\\", "\\\\")
flags = wbemFlagReturnImmediately | wbemFlagForwardOnly
ole_obj = obj.ExecQuery(strQuery=wql, iFlags=flags)
size = None
for o in ole_obj:
if size is not None:
raise ValueError('Only expected one size')
size = o.Properties_('size').Value
return size
print(get_raw_windows_device_size())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment