Results 1 to 2 of 2
  1. #1
    Join Date
    Sep 2005
    Posts
    5

    Unhappy Unanswered: Bug GlobalAlloc() in VB6

    Hello,

    There is a bug in my application when i call GlobalAlloc() Function, but i don't have error message.

    I call the Function StartInput() the first time and it's ok. When I record I call all the minute (in a timer with interval 1) the GetVolume() Function. When i finish my record I call the StopInput() Function. It's always OK, no bug. The bug appears when I want to restart Record.

    I call StartInput() Function => There is an bug on the second GlobalAlloc() (hmem(0)=GlobalAlloc(...) is OK and hmem(1)=GlobalAlloc(...) cause an error without message (just killing application).

    In my code : BUFFER_SIZE=700, NUM_BUFFERS=2 and buffaddress is global.

    StartInput() :
    Code:
    Public Function StartInput() As Boolean
    On Error GoTo err
        format.wFormatTag = 1
        format.nChannels = 1
        format.wBitsPerSample = 8
        format.nSamplesPerSec = 12000   '8000
        format.nBlockAlign = format.nChannels * format.wBitsPerSample / 8
        format.nAvgBytesPerSec = format.nSamplesPerSec *   format.nBlockAlign
        format.cbSize = 0
        For i = 0 To NUM_BUFFERS - 1
            hmem(i) = GlobalAlloc(&H40, BUFFER_SIZE) 'Error here
            inHdr(i).lpData = GlobalLock(hmem(i))
            inHdr(i).dwBufferLength = BUFFER_SIZE
            inHdr(i).dwFlags = 0
            inHdr(i).dwLoops = 0
        Next
        rc = waveInOpen(hWaveIn, DEVICEID, format, 0, 0, 0)
        If rc <> 0 Then
            waveInGetErrorText rc, msg, Len(msg)
            MsgBox msg
            StartInput = False
            Exit Function
        End If
        For i = 0 To NUM_BUFFERS - 1
            rc = waveInPrepareHeader(hWaveIn, inHdr(i), Len(inHdr(i)))
            If (rc <> 0) Then
                waveInGetErrorText rc, msg, Len(msg)
                MsgBox msg
            End If
        Next
        For i = 0 To NUM_BUFFERS - 1
            rc = waveInAddBuffer(hWaveIn, inHdr(i), Len(inHdr(i)))
            If (rc <> 0) Then
                waveInGetErrorText rc, msg, Len(msg)
                MsgBox msg
            End If
        Next
        fRecording = True
        rc = waveInStart(hWaveIn)
        StartInput = True
        Exit Function
    err:
        StartInput = False
    End Function
    StopInput() :
    Code:
    Public Function StopInput() As Integer
        On Error GoTo err
        fRecording = False
        waveInReset hWaveIn
        waveInStop hWaveIn
        For i = 0 To NUM_BUFFERS - 1
            waveInUnprepareHeader hWaveIn, inHdr(i), Len(inHdr(i))
            GlobalFree hmem(i)
        Next
        waveInClose hWaveIn
        GlobalFree volHmem
        StopInput = 0
        Exit Function
    err:
        StopInput = 1
    End Function
    getVolume() :
    Code:
    Public Function getVolume(pbuff As Long) As Integer
    Dim n As Integer
       On Error Resume Next
             Do While Not inHdr(0).dwFlags And WHDR_DONE
             ' perhaps I ought to put a time limit on this bit!
             Loop
                iValue.Caption = CStr(0)
                iValue.Refresh
                CopyStructFromPtr audbytearray, inHdr(0).lpData, inHdr(0).dwBufferLength
                rc = waveInAddBuffer(hWaveIn, inHdr(0), Len(inHdr(0)))
        tempval = 0
        posval = 0
        For n = 0 To BUFFER_SIZE - 1
            posval = audbytearray.bytes(n) - 128
            If posval < 0 Then posval = 0 - posval
            If posval > tempval Then tempval = posval
        Next n
            getVolume = tempval
            pbuff = inHdr(0).lpData
    End Function
    Call of StartInput() :
    Code:
             SoundMeter.StartInput
            Timer1.Enabled = True
    Call of StopInput() :
    Code:
        Dim i As Long
        Timer1.Enabled = False
        For i = 1 To 500000
        Next
        SoundMeter.StopInput
        buffaddress = 0
    Call of getVolume() :
    Code:
    Private Sub Timer1_Timer()
        Dim vValeur As Long
        vValeur = SoundMeter.getVolume(buffaddress)
        Select Case vValeur
         ...
        End Select

  2. #2
    Join Date
    Jun 2004
    Location
    Arizona, USA
    Posts
    1,848
    how and where are BUFFER_SIZE and hmem declared?
    Lou
    使大吃一惊
    "Lisa, in this house, we obey the laws of thermodynamics!" - Homer Simpson
    "I have my standards. They may be low, but I have them!" - Bette Middler
    "It's a book about a Spanish guy named Manual. You should read it." - Dilbert


Posting Permissions

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