|
-
Aug 25th, 2010, 06:28 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Cant get mciGetErrorString API to work
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
-
Aug 25th, 2010, 08:49 AM
#2
Re: Cant get mciGetErrorString API to work
Preface: I'm not a .Net guy
Which line does the error occur in?
I see you declared LONG parameter types & return value in mciSendString, shouldn't they be Integer?
And the mciGetErrorString API declaration should return Integer not Boolean, correct?
Edited: Another declaration definition
Last edited by LaVolpe; Aug 25th, 2010 at 09:00 AM.
-
Aug 25th, 2010, 09:17 AM
#3
Frenzied Member
Re: Cant get mciGetErrorString API to work
Code:
'In VB6
Private Declare Function mciGetErrorString Lib "winmm.dll" Alias "mciGetErrorStringA" (ByVal ErrorNumber As Long, ByVal ReturnBuffer As String, ByVal ReturnBufferSize As Long) As Long
Private Declare Function mciGetErrorStringW Lib "winmm" (ByVal dwError As Long, ByVal lpstrBuffer As Long, ByVal uLength As Long) As Long
' Get the error that just occured (if one did occur)
Private Function GetLastErr_MCI(Optional ByVal ErrorNumber As Long, _
Optional ByVal LastAPICalled As String = "last", _
Optional ByRef Return_ErrNum As Long, _
Optional ByRef Return_ErrDesc As String, _
Optional ByVal ShowErrors As Boolean = False) As Boolean
Dim intErrorNumber As Integer
' Clear return variables
Return_ErrNum = 0
Return_ErrDesc = ""
' If no error occured, exit
If ErrorNumber = 0 Then
Exit Function
Else
intErrorNumber = LOWORD(ErrorNumber)
End If
' Assign initial values
GetLastErr_MCI = True
Return_ErrNum = CLng(intErrorNumber)
If LastAPICalled = "" Then LastAPICalled = "last"
' Get the error description based on the error number
Return_ErrDesc = String(260, Chr(0))
If m_bUseUnicode Then
If mciGetErrorStringW(Return_ErrNum, StrPtr(Return_ErrDesc), 260) <> 0 Then
Return_ErrDesc = Left(Return_ErrDesc, InStr(Return_ErrDesc, Chr(0)) - 1)
Else
Return_ErrDesc = "Unknown Error"
End If
Else
If mciGetErrorString(Return_ErrNum, Return_ErrDesc, 260) <> 0 Then
Return_ErrDesc = Left(Return_ErrDesc, InStr(Return_ErrDesc, Chr(0)) - 1)
Else
Return_ErrDesc = "Unknown Error"
End If
End If
' If specified to show an error message, do so
If ShowErrors = True Then
MsgBox "The following error occured while calling the " & LastAPICalled _
& " MCI API:" & Chr(13) & Chr(13) & _
"Error Number = " & Return_ErrNum & Chr(13) & _
"Error Description = " & Return_ErrDesc, vbOKOnly + vbExclamation, " MCI ERROR"
End If
End Function
' Function that extracts the "Low Order" (lower 16bits) of a 32bit number
Private Function LOWORD(ByVal dwValue As Long) As Integer
LOWORD = Val("&H" & Right("0000" & Hex(dwValue), 4))
End Function
' Function that extracts the "High Order" (upper 16bits) of a 32bit number
Private Function HIWORD(ByVal dwValue As Long) As Integer
HIWORD = Val("&H" & Left(Right("00000000" & Hex(dwValue), 8), 4))
End Function
Last edited by Jonney; Aug 25th, 2010 at 09:19 AM.
Reason: Codes
-
Aug 26th, 2010, 08:32 AM
#4
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
-
Aug 26th, 2010, 09:14 AM
#5
Thread Starter
Addicted Member
Re: Cant get mciGetErrorString API to work
Thanks everyone for their assistance with this
@Edgemeal
Thanks for your code example, works a treat . Think my code was almost there and i had obviously declared the API incorrectly based on your working code.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|