使用winmm.dll在python中获取输入设备

2024-06-26 01:37:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在我的窗口上列出我的输入设备(如麦克风等)。在

代码在这里:

    from ctypes import * 
    import sys
    #printf = libc.printf
    winmm =  windll.LoadLibrary("winmm.dll")
    widn = winmm.waveInGetDevCapsA #wave in device num
    widn.restype = c_uint

    waveNum = winmm.waveInGetNumDevs 

    class LPWAVEINCAPS(Structure):
        _fields_ = [
            ("wMid",c_ushort),
            ("wPid",c_ushort),
            ("vDriverVersion",c_uint),
            ("szPname",c_wchar_p),
            ("dwFormats",c_uint),
            ("wChannels",c_ushort), 
            ("wReserved1",c_ushort),
            ]

    widn.argtypes = [
        c_uint,
        POINTER(LPWAVEINCAPS),
        c_uint
        ]

    count_devs = waveNum()

    print(count_devs)

    structLP = LPWAVEINCAPS()

    for i in range(count_devs):
        str = widn(c_uint(i),byref(structLP),c_uint(sys.getsizeof(structLP)))
        print(structLP.szPname)

输出是段错误,当我删除byref时,它给了我None作为输出。在

帮帮我谢谢你:)


Tags: inimportcountsysprintuintdevsprintf
1条回答
网友
1楼 · 发布于 2024-06-26 01:37:01

我解决了这个问题:

问题是我的字符串指针,我在结构中使用了s = create_string_buffer(b'\000' * 32),然后使用了("szPname", type(s))

from ctypes import * 
import sys
#printf = libc.printf
winmm =  windll.LoadLibrary("winmm.dll")
widn = winmm.waveInGetDevCapsA #wave in device num
widn.restype = c_uint

waveNum = winmm.waveInGetNumDevs

s = create_string_buffer(b'\000' * 32)

class LPWAVEINCAPS(Structure):
    _fields_ = [
        ("wMid",c_ushort),
        ("wPid",c_ushort),
        ("vDriverVersion",c_uint),
        ("szPname", type(s)),
        ("dwFormats",c_uint),
        ("wChannels",c_ushort), 
        ("wReserved1",c_ushort),
        ]

widn.argtypes = [
    c_uint,
    POINTER(LPWAVEINCAPS),
    c_uint
    ]

count_devs = waveNum()

print(count_devs)

structLP = LPWAVEINCAPS()

for i in range(count_devs):
    print(sizeof(type(structLP)))
    str = widn(c_uint(i),byref(structLP),sizeof(structLP))
    print(structLP.szPname)
#waveCaps = winmm.waveOutGetDevCaps
waveNum.restype = c_uint
#waveCaps.argtypes = []

相关问题 更多 >