Step 8: Stopping Capture

When it's time to stop recording, call the following function:

BOOL StopWrite()
{
    IDirectSoundCaptureBuffer_Stop(lpdscb);
    StreamToFile();  
    WaveCloseWriteFile(&hmmio, &mmckinfoData, 
            &mmckinfoParent, &mmioinfo, 
            dwTotalBytesWritten / (wfx.wBitsPerSample / 8));
    return TRUE;
}
 

This function stops the capture buffer, calls the StreamToFile function one more time in order to save all the data up to the current read position, and closes the file. The WaveCloseWriteFile function in Wave.c also updates the header of the data chunk by writing the total number of samples.