Skip to content

Instantly share code, notes, and snippets.

@virtuald
Created August 16, 2015 03:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save virtuald/bacc4bafabf267a0831e to your computer and use it in GitHub Desktop.
Save virtuald/bacc4bafabf267a0831e 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.DirectSoundEnumerateW
_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(200)
if _StringFromGUID2(lpGUID, C.byref(buf), 200):
dev = buf.value
devices.append((dev, lpszDesc))
return True
_DirectSoundEnumerateW(_LPDSENUMCALLBACK(cb_enum), None)
return devices
if __name__ == '__main__':
for devid, desc in get_devices():
print '%38s: %s' % (devid, desc)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment