Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
4.50/5 (2 votes)
See more:
Hello, I am doing an audio recording application. I am writing the audio data to a file. I am trying to read the data in the file and add it to a buffer. During debugging, I find the error is pointing to:
ASSERT(AfxIsValidAddress(lpBuf, nCount));
of the
CFile::Read
function. Here is the code:
C#
void CWaveIn::WriteWaveFile()
{
    CString* databuf;   //buffer to hold the data from the file.
    databuf = NULL;
    databuf = new CString;
    CFile my_file,f1;
    if(my_file.Open(m_strFile,CFile::modeRead|CFile::shareDenyNone,NULL))
    {   
       unsigned int x =  ;
       int iRead = my_file.Read((void*)databuf, my_file.GetLength());//error here
       .....

If you think, I have problem with my recording, I am pasting that code as well:
(I am using a callback thread, for recording. The thread's function is written below)
DWORD WINAPI CWaveIn::ThreadProc(LPVOID lpParameter)
{
    CWaveIn* pWaveIn     = (CWaveIn*)lpParameter;
    MSG msg;
    DWORD dwWait      = 0;
    DWORD dwUser      = 0;
    WAVEHDR *pWaveHdr = NULL;
    if (pWaveIn == NULL)
    {
        return -1;
    }
    while ((dwWait = WaitForSingleObject(pWaveIn->m_hEventKill, 5)) != 0)
    {
        // Handle MMAPI wave capturing thread messages
        while (PeekMessage(&msg, 0, 0, 0, PM_REMOVE))
        {
            switch (msg.message)
            {
            case MM_WIM_DATA:
                EnterCriticalSection(&pWaveIn->m_critSecRtp);
                pWaveHdr = (WAVEHDR*)msg.lParam;      
                // Unprepare header, initialize with the next buffer position, prepare the header
                //  again and add it to the wave capture device.
                if (pWaveHdr)
                {
                    waveInUnprepareHeader(pWaveIn->m_hWaveIn, pWaveHdr, sizeof(WAVEHDR));
                    dwUser = pWaveHdr->dwUser;
                    //m_fRecord is a FILE* in CWaveIn class                       
                    fwrite(pWaveHdr->lpData, 1, pWaveHdr->dwBytesRecorded, pWaveIn->m_fRecord);
                    ZeroMemory(pWaveHdr, sizeof(WAVEHDR));                       
                    pWaveHdr->dwBufferLength = BYTES_PER_HDR;
                    pWaveHdr->lpData = (char*)pWaveIn->m_HdrBuffer[dwUser];
                    pWaveHdr->dwUser = dwUser;
                    waveInPrepareHeader(pWaveIn->m_hWaveIn, pWaveHdr, sizeof(WAVEHDR));
                    waveInAddBuffer(pWaveIn->m_hWaveIn, pWaveHdr, sizeof(WAVEHDR));
                }
                LeaveCriticalSection(&pWaveIn->m_critSecRtp);
                break;
            default:
                break;
            }
        }
    }
    CloseHandle(pWaveIn->m_thread);
    pWaveIn->m_thread = NULL;
    return 0;
}
Posted
Updated 3-Jul-12 18:56pm
v3

Why are you assigning a new, empty CString to the buffer and then reading into it?
C#
CString* databuf;   //buffer to hold the data from the file.
databuf = NULL;
databuf = new CString;
...
int iRead = my_file.Read((void*)databuf, my_file.GetLength());//error here
Surely, assigning a buffer that was big enough to hold the data in the file would be a better idea? An empty CString is not going to "magically" expand itself to cope with the file data just because you have assigned it and cast it to a pointer-to-void! Have a look at the CFile.Read function, and allocate a buffer instead: http://msdn.microsoft.com/en-us/library/ctka0kks(v=vs.80).aspx[^]
 
Share this answer
 
Comments
stib_markc 4-Jul-12 1:23am    
Thank you, I think my problem is solved. I will check and accept the solution in a minute or I have any problem(not sure I will), I will update. anyway thank you very much for the solution.
you're using CString incorrectly

It does not represent an object with a buffer that you can scribble into ...

change it to

char *databuf=new char[my_file.GetLength()];

inside the .Open clause

and make sure you delete it on the way out
 
Share this answer
 
Comments
stib_markc 4-Jul-12 1:23am    
Thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900