Results 1 to 5 of 5

Thread: [RESOLVED] Cant get mciGetErrorString API to work

Threaded View

  1. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: Cant get mciGetErrorString API to work

    These seems to be catching error and working in VB10 / .Net4 for me.

    Code:
    Public Class Form1
        Private Declare Function mciSendString Lib "winmm.dll" Alias "mciSendStringA" (ByVal lpstrCommand As String, ByVal lpstrReturnString As String, ByVal uReturnLength As Integer, ByVal hwndCallback As Integer) As Integer
        Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal dwError As Integer, ByVal lpstrBuffer As String, ByVal uLength As Integer) As Integer
        Dim ErrCode As Integer
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim RetStr As String = Space(1024)
            ErrCode = mciSendString("open new type waveaudio alias RecWavFile", RetStr, 1024, 0)
            If ErrCode = 0 Then
                ErrCode = mciSendString("set RecWavFile channels 2", RetStr, 1024, 0) ' Stereo Or Mono Sound
                If ErrCode = 0 Then
                    ErrCode = mciSendString("set RecWavFile samplespersec 44100", RetStr, 1024, 0) ' Khz Rate
                    If ErrCode = 0 Then ' Make it error...... bitspersample = 6 <<< (should be 8 or 16?) 
                        ErrCode = mciSendString("set RecWavFile bitspersample 6", RetStr, 1024, 0) ' Sound Sample Rate (Bits)
                        If ErrCode = 0 Then
                            ErrCode = mciSendString("record RecWavFile", RetStr, 1024, 0)
                        End If
                    End If
                End If
            End If
    
            If ErrCode <> 0 Then MessageBox.Show(GetMCIErrorString(ErrCode))
    
        End Sub
    
        Private Function GetMCIErrorString(ByVal ErrorCode As Integer) As String
            'create a buffer
            GetMCIErrorString = Space(1024)
            'retrieve the error string
            mciGetErrorString(ErrorCode, GetMCIErrorString, Len(GetMCIErrorString))
            'strip off the trailing spaces
            GetMCIErrorString = Trim(GetMCIErrorString)
        End Function
    
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
            ' // stop  and save to file //
            Dim RetStr As String = Space(1024) 'Will show error here if path/folder doesn't exist
            ErrCode = mciSendString("save RecWavFile D:\TEST\RecWavFile.wav", RetStr, 1024, 0)
            If ErrCode <> 0 Then MessageBox.Show(GetMCIErrorString(ErrCode))
            ErrCode = mciSendString("close RecWavFile", RetStr, 1024, 0)
            If ErrCode <> 0 Then MessageBox.Show(GetMCIErrorString(ErrCode))
        End Sub
    End Class
    Last edited by Edgemeal; Aug 26th, 2010 at 09:04 AM. Reason: revised

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width