Before closing the application you must shut down the capture system. This is a simple matter of releasing all the objects. You must release the IDirectSoundNotify interface before releasing the capture buffer.
void CleanupDSoundCapture(void)
{
if (lpdsNotify)
IDirectSoundNotify_Release(lpdsNotify);
if (lpdscb)
IDirectSoundCaptureBuffer_Release(lpdscb);
if (lpdsc)
IDirectSoundCapture_Release(lpdsc);
}