为什么删除println!导致通过FFI调用的Windows API函数失败?

时间:2017-04-06 13:38:40

标签: rust winmm

如果我从以下代码中删除println!,则waveOutPrepareHeader会返回代码MMSYSERR_INVALPARAM

fn write_audio_block(device: &PlaybackDevice, block: &[u8]) {
    use std::mem;
    use std::{thread, time};
    let mut header: mmsystem::WAVEHDR = unsafe { mem::uninitialized() };
    header.dwBufferLength = block.len() as winapi::minwindef::DWORD;
    header.lpData = block.as_ptr() as winapi::winnt::LPSTR;

    println!("ptr: {:?}\nlength: {:?}", // removing this causes error
         header.lpData,
         header.dwBufferLength);

    let header_size: winapi::minwindef::UINT = mem::size_of::<mmsystem::WAVEHDR>() as u32 +
                                           header.dwBufferLength as u32;

    catch_errors(unsafe { winmm::waveOutPrepareHeader(device.handle, &mut header, header_size) });

    catch_errors(unsafe { winmm::waveOutWrite(device.handle, &mut header, header_size) });

    thread::sleep(time::Duration::from_millis(100));

    while unsafe { winmm::waveOutUnprepareHeader(device.handle, &mut header, header_size) } ==
        mmsystem::WAVERR_STILLPLAYING {
        thread::sleep(time::Duration::from_millis(10));
    }
}

即使在println!之后添加其他thread::sleep或删除waveOutWrite,也会导致waveOutPrepareHeader返回MMSYSERR_INVALPARAM

否则它可以正常工作。

0 个答案:

没有答案
相关问题