Skip to content

Instantly share code, notes, and snippets.

@papr
Forked from virtuald/devices.py
Created July 5, 2017 13:46
Show Gist options
  • Save papr/208eb04d7b73ef63607f8ff92c3a34ac to your computer and use it in GitHub Desktop.
Save papr/208eb04d7b73ef63607f8ff92c3a34ac to your computer and use it in GitHub Desktop.
Enumerate all Windows audio devices using python+ctypes
import ctypes.wintypes
import ctypes as C
_dsound_dll = C.windll.LoadLibrary("dsound.dll")
_DirectSoundEnumerateW = _dsound_dll.DirectSoundCaptureEnumerateW
_LPDSENUMCALLBACK = C.WINFUNCTYPE(C.wintypes.BOOL,
C.wintypes.LPVOID,
C.wintypes.LPCWSTR,
C.wintypes.LPCWSTR,
C.wintypes.LPCVOID)
_ole32_dll = C.oledll.ole32
_StringFromGUID2 = _ole32_dll.StringFromGUID2
def get_devices():
devices = []
def cb_enum(lpGUID, lpszDesc, lpszDrvName, _unused):
dev = ""
if lpGUID is not None:
buf = C.create_unicode_buffer(500)
if _StringFromGUID2(C.c_int64(lpGUID), C.byref(buf), 00):
dev = buf.value
devices.append((dev, lpszDesc, lpszDrvName))
return True
_DirectSoundEnumerateW(_LPDSENUMCALLBACK(cb_enum), None)
return devices
if __name__ == '__main__':
for devid, desc, name in get_devices():
print('%s: %s | %s' % (devid, desc, name))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment