objectPicker.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. # A demo for the IDsObjectPicker interface.
  2. import win32clipboard
  3. import pythoncom
  4. from win32com.adsi import adsi
  5. from win32com.adsi.adsicon import *
  6. cf_objectpicker = win32clipboard.RegisterClipboardFormat(CFSTR_DSOP_DS_SELECTION_LIST)
  7. def main():
  8. hwnd = 0
  9. # Create an instance of the object picker.
  10. picker = pythoncom.CoCreateInstance(adsi.CLSID_DsObjectPicker,
  11. None,
  12. pythoncom.CLSCTX_INPROC_SERVER,
  13. adsi.IID_IDsObjectPicker)
  14. # Create our scope init info.
  15. siis = adsi.DSOP_SCOPE_INIT_INFOs(1)
  16. sii = siis[0]
  17. # Combine multiple scope types in a single array entry.
  18. sii.type = DSOP_SCOPE_TYPE_UPLEVEL_JOINED_DOMAIN | \
  19. DSOP_SCOPE_TYPE_DOWNLEVEL_JOINED_DOMAIN
  20. # Set uplevel and downlevel filters to include only computer objects.
  21. # Uplevel filters apply to both mixed and native modes.
  22. # Notice that the uplevel and downlevel flags are different.
  23. sii.filterFlags.uplevel.bothModes = DSOP_FILTER_COMPUTERS
  24. sii.filterFlags.downlevel = DSOP_DOWNLEVEL_FILTER_COMPUTERS
  25. # Initialize the interface.
  26. picker.Initialize(
  27. None, # Target is the local computer.
  28. siis, # scope infos
  29. DSOP_FLAG_MULTISELECT, # options
  30. ('objectGUID','displayName') ) # attributes to fetch
  31. do = picker.InvokeDialog(hwnd)
  32. # Extract the data from the IDataObject.
  33. format_etc = (cf_objectpicker, None,
  34. pythoncom.DVASPECT_CONTENT, -1,
  35. pythoncom.TYMED_HGLOBAL)
  36. medium = do.GetData(format_etc)
  37. data = adsi.StringAsDS_SELECTION_LIST(medium.data)
  38. for item in data:
  39. name, klass, adspath, upn, attrs, flags = item
  40. print("Item", name)
  41. print(" Class:", klass)
  42. print(" AdsPath:", adspath)
  43. print(" UPN:", upn)
  44. print(" Attrs:", attrs)
  45. print(" Flags:", flags)
  46. if __name__=='__main__':
  47. main()