Advertisement
linuxlizard

Win32 Python SetupDI Printers

Mar 18th, 2012
187
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.73 KB | None | 0 0
  1. def printers(available_only=True):
  2.     """This generator scans the device registry for com ports and yields port, desc, hwid.
  3.       If available_only is true only return currently existing ports."""
  4.  
  5.     flags = DIGCF_DEVICEINTERFACE
  6.  
  7.     if available_only:
  8.         flags |= DIGCF_PRESENT
  9.  
  10.     g_hdi = SetupDiGetClassDevs(ctypes.byref(GUID_CLASS_USBPRINT), None, NULL, flags);
  11.  
  12.     if g_hdi==INVALID_HANDLE_VALUE :
  13.         raise Exception( "SetupDiGetCLassDevs() failed" )
  14.  
  15.     #~ for i in range(256):
  16.     for dwIndex in range(256):
  17.         did = SP_DEVICE_INTERFACE_DATA()
  18.         did.cbSize = ctypes.sizeof(did)
  19.  
  20.         if not SetupDiEnumDeviceInterfaces(
  21.             g_hdi,
  22.             None,
  23.             ctypes.byref(GUID_CLASS_USBPRINT),
  24. #            ctypes.byref(GUID_CLASS_COMPORT),
  25.             dwIndex,
  26.             ctypes.byref(did) ):
  27.             if ctypes.GetLastError() != ERROR_NO_MORE_ITEMS:
  28.                 raise ctypes.WinError()
  29.             break
  30.        
  31.         dwNeeded = DWORD()
  32.  
  33.         # get the size
  34.         if not SetupDiGetDeviceInterfaceDetail(
  35.             g_hdi,
  36.             ctypes.byref(did),
  37.             None, 0, ctypes.byref(dwNeeded),
  38.             None ):
  39.             # Ignore ERROR_INSUFFICIENT_BUFFER
  40.             if ctypes.GetLastError() != ERROR_INSUFFICIENT_BUFFER:
  41.                 raise ctypes.WinError()
  42.  
  43.         # allocate buffer
  44.         class SP_DEVICE_INTERFACE_DETAIL_DATA_A(ctypes.Structure):
  45.             _fields_ = [
  46.                 ('cbSize', DWORD),
  47.                 ('DevicePath', CHAR*(dwNeeded.value - ctypes.sizeof(DWORD))),
  48.             ]
  49.             def __str__(self):
  50.                 return "DevicePath:%s" % (self.DevicePath,)
  51.  
  52.         idd = SP_DEVICE_INTERFACE_DETAIL_DATA_A()
  53.         idd.cbSize = 5
  54.         devinfo = SP_DEVINFO_DATA()
  55.         devinfo.cbSize = ctypes.sizeof(devinfo)
  56.  
  57.         if not SetupDiGetDeviceInterfaceDetail(
  58.             g_hdi,
  59.             ctypes.byref(did),
  60.             ctypes.byref(idd), dwNeeded, None,
  61.             ctypes.byref(devinfo) ):
  62.             raise ctypes.WinError()
  63.  
  64.         # hardware ID
  65.         szHardwareID = ctypes.create_string_buffer('\0' * 250)
  66.         if not SetupDiGetDeviceRegistryProperty(
  67.             g_hdi,
  68.             ctypes.byref(devinfo),
  69.             SPDRP_HARDWAREID,
  70.             None,
  71.             ctypes.byref(szHardwareID), ctypes.sizeof(szHardwareID) - 1,
  72.             None ):
  73.             # Ignore ERROR_INSUFFICIENT_BUFFER
  74.             if ctypes.GetLastError() != ERROR_INSUFFICIENT_BUFFER:
  75.                 raise ctypes.WinError()
  76.  
  77.         # davep ; stop here because printers don't seem to have a friendly name
  78.         yield idd.DevicePath, "", szHardwareID.value
  79.    
  80.     SetupDiDestroyDeviceInfoList(g_hdi)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement