locked
pIXMLHTTPRequest to get a audio file RRS feed

  • Question

  • I am creating a window mobile application in win32
    I made a pIXMLHTTPRequest to get a audio file as a response. Here after getting pIStream i take this stream to a byte buffer
    then i get the an empty buffer which is not written as a file. How to convert pIStream to a writable buffer to save this as a mp3 file ?

    LRESULT WEBREQUEST_AUDIOFILE(HWND hWnd,TCHAR *PageURL)
    {


            TCHAR szFileName[MAX_PATH] = {0};
          
            PBYTE pbBuffer = NULL;
            DWORD writeLen;
            DWORD dwBufferLen;
            dwBufferLen=1024*1024;
               if ((pbBuffer = (PBYTE)malloc (dwBufferLen)) == NULL)
              {
                MessageBox( NULL, L"Out of memory!", L"File Error", MB_OK );
              }
    ////////////////////////////////////////////////////////////////////

                   MSXML::IXMLHttpRequestPtr pIXMLHTTPRequest = NULL;
                BSTR bstrString = NULL;
                HRESULT hr;
            try
            {
           
                CoInitializeEx(NULL, 0);
                hr=pIXMLHTTPRequest.CreateInstance("Microsoft.XMLHTTP");
                 VARIANT vAsync;
                vAsync.vt = VT_BOOL;
                vAsync.boolVal = FALSE;
           
                VARIANT vUser;
                vUser.vt = VT_BSTR;
                vUser.bstrVal = NULL;

                VARIANT vPassword;
                vPassword.vt = VT_BSTR;
                vPassword.bstrVal = NULL;
       
                /*IPicture* p;*/
                IStream *pIStream = NULL;
                VARIANT varValue;

                BSTR bstrSym = NULL;
                bstrSym = SysAllocString(PageURL);

                hr=pIXMLHTTPRequest->open("GET", bstrSym, vAsync, vUser, vPassword);
                SUCCEEDED(hr) ? 0 : throw hr;
                SysFreeString(bstrSym);
                bstrSym=NULL;

     
                hr=pIXMLHTTPRequest ->setRequestHeader((bstr_t)"Content-Type", (bstr_t)"audio/mpeg3");
               SUCCEEDED(hr) ? 0 : throw hr;

     
                hr=pIXMLHTTPRequest->send();
                SUCCEEDED(hr) ? 0 : throw hr;


                hr = pIXMLHTTPRequest->get_responseStream(&varValue);
                SUCCEEDED(hr) ? 0 : throw hr;
                pIStream = (IStream*)varValue.punkVal;

                ULONG ulBytesRead;
                if(pIStream->Read(pbBuffer, dwBufferLen, &ulBytesRead) != S_OK)
                {
                pIStream->Release();
                }

                writeLen=(DWORD)ulBytesRead;


                FILE *hDestFile = NULL;
                if ((hDestFile = _wfopen (szFileName, TEXT("wb"))) == NULL)
              {
                /*MessageBox( NULL, L"Error in opening file", L"File Error", MB_OK );*/
              }
                // Write the data to the destination file.

                fwrite (pbBuffer, 1, writeLen, hDestFile);//sizeof(PBYTE)

                if (ferror (hDestFile))
                {
                 MessageBox( NULL, L"Error in writting file", L"File Error", MB_OK );
                }

              if (hDestFile) fclose (hDestFile);
               if (pbBuffer) free (pbBuffer);


            }
            catch(...)
            {
              MessageBox( NULL, L"Internet Connection is not available", NULL, MB_OK );
            }


            if (pIXMLHTTPRequest)
                 pIXMLHTTPRequest.Release();
                pIXMLHTTPRequest = NULL;
               
                CoUninitialize();
        return( 0 );
    }
    Friday, August 28, 2009 11:08 AM