Using: Visual Studio 2008 SP1
Language: VB.NET

I am using the API 'mciSendString' to record audio to a WAV file, this works fine. As i want to monitor 'mciSendString' API for errors i am using the 'mciGetErrorString' API. I cannot get the 'mciGetErrorString' API to work as it keeps failing with errors when run.

I have googled the code many times and modified it to use different types of buffers to stores its values and it still fails to work or displays a different type of error message. Please can you inform me what i may be doing wrong or give a working code example.

Below is one example of my code:

Displays the message "Arithmetic operation resulted in an overflow" when run

Code:
    

    Dim errorcode As Long

    Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Long, ByVal hwndCallback As Long) As Long

    Declare Auto Function mciGetErrorString Lib "winmm.dll" (ByVal fdwError As Integer, ByVal lpszErrorText As String, ByVal cchErrorText As Integer) As Boolean

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        errorcode = mciSendString("open new type waveaudio alias RecWavFile", 0&, 0, 0)
        errorcode = mciSendString("set RecWavFile channels 2", 0, 0, 0) ' Stereo Or Mono Sound
        errorcode = mciSendString("set RecWavFile samplespersec " & CBKhz.SelectedItem, 0, 0, 0) ' Khz Rate
        errorcode = mciSendString("set RecWavFile bitspersample " & CBBit.SelectedItem, 0, 0, 0) ' Sound Sample Rate (Bits)
        errorcode = mciSendString("record RecWavFile", 0&, 0, 0)
        ' Start recording in the highest quality format for .WAV PCM Audio
        ' Recording will only work if the sound card supports the chosen KHZ / BIT rate

        If errorcode <> 0 Then
            MessageBox.Show(GetMCIErrorString(errorcode))
        End If

    End Sub

Private Function GetMCIErrorString(ByVal errorcode As Long) As String
        'create a buffer
        GetMCIErrorString = Space$(128)
        'retrieve the error string
        mciGetErrorString(errorcode, GetMCIErrorString, Len(GetMCIErrorString))
        ''strip off the trailing spaces
        GetMCIErrorString = Trim$(GetMCIErrorString)
 End Function