This function returns an int through the variable sample
for success or if failure returns 0xefffffff.
We use rewind to position the file pointer at the beginning of 'stream'.

Then we begin reading at the sample number times (1+44)
this is because the data in the canonical wave format is held there at this point.

The Canonical Wave Format.

1. The characters "RIFF" indiciate the RIFF header. RIFF stands for Resource Interchange File Format.
2. The sizeof the file being read or recorded.
3. The "WAVE" characters indicate its a .wav file.
4. The "fmt " characters specify that this is the section of the file describing the format specifically.
5. The size of the WAVEFORMATEX data to follow
6. # WAVEFORMATEX (shown below)

a.wFormatTag, only PCM data is supported in this sample
b. nChannels, Number of channels in (1 for mono, 2 for stereo)
c. nSamplesPerSec, Sample rate of the waveform in samples per second
d. nAvgBytesPerSec, Average bytes per second which can be used to determine the
time-wise length of the audio
e. nBlockAlign, Specifies how each audio block must be aligned in bytes
f. wBitsPerSample, How many bits represent a single sample (typically 8 or 16)
7. The "data" characters specify that the audio data is next in the file.
8. The length of the data in bytes.
9. PCM DATA. The actual sound data.

readSample references values in Wav function.
which we will discuss next.

This sets up a Common Dialog to save a .wav file, ofn.flags are set to OFN_EXPLORER | OFN_OVERWRITEPROMPT this asks the user if he/she wants to overwrite the new file if a file by that name already exists.

The above is our code for the record button.
The RECORD button itself is disabled along with the PLAY button the STOP button is enabled.
Memory is allocated for pBuffer1 and pBuffer2.
If you have run out of memory or memory cannot be allocated a messagebox is displayed with the nature of the error.
Next we set up the wave headers pWaveHdr1 and pWaveHdr2 and call the function
waveInPrepareHeader to prepare for waveform audio input.

The above code sets the flag bPlaying to true.
It disables the PLAY button.
Prepares the waveform for playing
if it cant open the audio output device it displays a Messsage Box with the Error.
It then calls the Wav function which loads the temporary wave file "temp.wav".

The flag PLAY is set to true and a refresh of the window is forced by InvalidateRect.

Finally we have the SavWavFile Function this opens a fstream object called myFile
the file is saved as according to the table in the Canonical Wave format section.

First "RIFF" is written, then the chunksize is written this is (36+ Bufferlength*number of channels(in this case 1)) next "WAVE" is written to determine which type of RIFF file it is.
Then "fmt" is written next subchunk1size is written in this case it is 16 for PCM.
Audio format is written next in this case it is 1 for PCM, next numchannels is written in this case it 1 once again.
The sample rate is the next thing to be written in this case it is 11025.
The Byte rate is now written to the file which is the SampleRate * NumChannels * BitsPerSample/8
BitsPerSample can be either 16 or 8 in this case its 8.
The Block alignment is written next which is NumChannels * BitsPerSample/8.
BitsPerSample is now written which is '8' which I havepreviously mentioned.
"data" is now written to the file to mark the start of the data portion of the .wav file.
subchunk2size is now written which is (NumSamples * NumChannels * BitsPerSample/8).
Lastly the actual sample data is written to the .wav file and the file is closed.

For completeness I will now post the projects main files the .cpp the .h and the .rc files.
Then I will go into detail about which Libraries to include.

Replies To: Sound Recorder using the low level Windows API in C++

Nice tutorial, I have been wondering how to do this for a while. You mentioned you had a GCC version available? Would you mind posting it, I am more familiar with GCC than I am with Visual Studio. Thank you very much!

I am getting fatal error LNK1104: cannot open file 'WinMM.Lib;'. I added WinMM.Lib; kernel32.lib; user32.lib; comdlg32.lib as required to the linker. As a test I removed WinMM.Lib and I received the same error regarding kernel32.lib. I've checked my computer and WinMM.Lib exists on my computer as I expected. Any ideas what is going on?

Sounds like the path to where your libraries are is not set correctly.

I don't have VC2008, otherwise I would tell you how to post back the path that the IDE thinks where it should find the library files. I think it'll be something like:
Project Properties / Configuration Properties / VC++ Directories / Library Directories
or
Project Properties / Configuration Properties / Linker / General / Additional Library Directories

I made the change you suggested with --> Project Properties / Configuration Properties / Linker / General / Additional Library Directories to reflect the location of the lib files but still having the same error.