Wednesday, November 28, 2007

Calculate the execution time of a function

Calculate the execution time of a function :
we can use timeGetTime() fn available in mmsystem.h .
For using this function,
we have to include the winmm.lib


DWORD dwStartTime = timeGetTime();
StretchBlt(); // which we want to find the execution time
DWORD dwEndTime = timeGetTime();

dwEndTime - dwStartTime is the execution time.

we can also use QueryPerformanceCounter() fn to find the execution time.

Monday, November 26, 2007

While using CString in a DLL, I got an error Because it is a win32 DLL.

While using CString in a DLL, I got an error Because it is a win32 DLL.

Solution :

I modified the project ->settings->General Tab-> Use MFC in a Shared DLL or Use MFC in a Static Library...

Afterwards I included the #include in "stdafx.h" header file, now the problem is solved...

How to Avoid name mangling of a function in a DLL:

How to Avoid name mangling of a function in a DLL:

I opened my DLL using Dependency viewer in visual studio tools.

The Dependency viewer showed the functions in it as follows :

For AAPI_CleanupRecorder() fn, it displays as follows :

?AAPI_CleanupRecorder@@YGHXZ

This is called name mangling.ToSupport function overloading this will happen.
How can we disable name mangling ?...


Solution :
------------

we can avoid name mangling using .def file in a DLL.

I added the AAPI_CleanupRecorder() fn in a .Def file as follows :


LIBRARY AAPIPC2TV.dll

EXPORTS

AAPI_CleanupRecorder @1

I compiled the DLL and then once again opened the DLL in a "Dependency viewer".

Now the AAPI_CleanupRecorder function is available in a Dependency viewer as follows :


AAPI_CleanupRecorder

But the Demerit of this approach is we can't use the function overlaoding.

Combo Box problem in MFC dialog application

Combo Box problem in MFC dialog application :
----------------------------------------------
1.I added the combo box to the dialog and added the string to the combo box using the following code...

CComboBox* combo = (CComboBox*) GetDlgItem( IDC_COMBO1 );
combo->AddString( "one" );
combo->AddString( "two" );
combo->AddString( "three" );

If I checked the count, it returns 3 But it didnt display the text "one,two,three" on screen.


Solution:
---------
Make sure you size the combo box in the resource editor so it is big enough. Click
on the little arrow and then drag the bottom handle square down. If you are doing it in
code, make sure the rect is big enough to fit the drop-down.

I opened the .rc file in a resource editor and modified the
combo box values , Now it displays the data .The problem is combo box height is not big to display
the combo box items in a drop down or drop List

Saturday, November 24, 2007

How to change the audio capture buffer size in DirectSound's CaptureSound application ?...

How to change the audio capture buffer size in DirectSound's CaptureSound application ?...


m_dwNotifySize = 3528;
Set this Value in CreateCaptureBuffer() fn,
before this line:

m_dwNotifySize = 3528;//AudioBufferSize
m_dwCaptureBufferSize = m_dwNotifySize * NUM_REC_NOTIFICATIONS;

 

Notification buffer size is used in this application to retrive data from the Circular buffer .


we can set any size to this notification buffer size and it will be used to retrive buffer size bytes from the circular buffer.

How to change the audio capture buffer size in DirectSound's CaptureSound application ?...

How to change the audio capture buffer size in DirectSound's CaptureSound application ?...


m_dwNotifySize = 3528;
Set this Value in CreateCaptureBuffer() fn,
before this line:

m_dwNotifySize = 3528;//AudioBufferSize
m_dwCaptureBufferSize = m_dwNotifySize * NUM_REC_NOTIFICATIONS;


Notification buffer size is passed to retrieve the data from the Circular buffer.
So we can set any buffer size , this will cause the change in buffer size.

Friday, November 23, 2007

Design time combo box problem in MFC Dialog application

I got the combo box problem in VC++ 6 dialog application as follows :

1.I added the Combo box with DropList property in a dialog application
2.I added the "control" member variable from the classwizard.
3.Within OnInitDialog() fn, I added the string to the combo box.
( m_cbo.AddString("Sundar");m_cbo.AddString('Sundar");)
But the added string is not displayed at runtime.

if I set the selected index for the combo box,

m_cbo.SetCurSel(0) , the selected index string "sundar" alone displayed in a combo box at
runtime.


4. To identify the problem, I created the Dynamic combo box by code ...

and Added the string , all the strings are displayed in a combo box.

But For Design time combo box, it didnt display the text.

unsupported operation was attempted error

In an MFC Dialog based application,
I didnt have a linker error but I got an error at runtime as follows;

"An unsupported operation was attempted."

Then the application is closed...


Reason for the Error :

I have added the Combo box in a MFC dialog application and added the control variable for it.

wizard will add the code as follows : DDX_Control(pDX, IDC_cboDeviceList, m_cboDevice);

later for some reason, I deleted the combo box from the dialog.

But the following code is still in my dialog application

DDX_Control(pDX, IDC_cboDeviceList, m_cboDevice);

and causes the error "the unsupported operation was attempted"

Vista supported multimedia technologies

Windows vista supports video and audio based development technology as follows:


1.Core Audio APIs
2.Directshow
3.Microsoft Media Foundation SDK
4.Windows portable devices
5.windows Audio and Video Codec and DSP APis
6.windows media device manager11 SDk
7.Windows movie maker 6.0 and windows DVD maker 1.0 SDK
8.Windows media Format 11 SDK
9.Windows media player 11 SDK
10.windows media Rights manager 10.1.2 SDK
11.Windows media Services 9 series

Sound card capturer in Vista

Today we tested the Directsound Audio capturer in vista ...
It is not working and we checked the waveIn Multimedia APis in vista..
That is also not working.

GraphBuilder way to convert RGB to YUY2

u need the RGB to YUY2 conversion as source Filter ?

we can do RGB to YUY2 in another way also .

Build the graph with Source Filter and add the color conversion filter to it.
and render it in screen.

Note : we have to set the input and output pin of the color conversion filter as
YUY2 .

RGB to YUY2 conversion

RGB to YUY2 :
1.Convert the RGB to YUY2 in my way and store it in a file
2.Using Color space converter convert it to YUY2 and dump it in memory

Compare these two things...

Other way is to

1.Initialize DirectDraw
2.Create the RGB Surface and copy the data to RGB surface
3.Create the YUY2 Surface and copy the RGB Surface to the YUY2 Surface
4.Lock the YUY2 Surface and get the buffer data...

RGB to YUV :

RGB to YUV conversion some formulas are available...
Bytes are arranged in this manner for YUY2
For the First 3 RGB Bytes we have to find the YU
YU
For the next 3 RGB bytes, we have to find the YV.
YV
This will be continued repeatedly.
YUYVYUYV

Capturing Data from Soundcard doesn't works

Previously I got an error in capturing data from Soundcard...

Solution :
1.Right click the speaker icon appeared in the right side of the Task bar
2.The "Master Volume" titled dialog box appear.
3.Select the menu properties-> Options
4.Look at the Mixer Device...

5.There Audio and video devices are appeared...
6.Select the Audio Input device.

7.Ensure the "Checked" state of RecordingControl, MiC Volume, LineVolume and Stereo Mix of an input Device

CWaveFile Usage

void MyFunc()
{
CWaveFile* g_pWaveFile = NULL;


g_pWaveFile = new CWaveFile;
HRESULT hr = S_OK;
hr = g_pWaveFile->Open(strFileName, &wfxCaptureWaveFormat, WAVEFILE_WRITE ) ;
if(FAILED(hr)
{
OutputDebugString("Error in writing WaveFileHeader");
return;
}

DWORD dwCaptureLength;
UINT dwDataWrote;
VOID* pbCaptureData = NULL;


// Write the data into the wav file
if( FAILED( hr = g_pWaveFile->Write( dwCaptureLength,
(BYTE*)pbCaptureData,
&dwDataWrote ) ) )

return DXTRACE_ERR_MSGBOX( TEXT("Write data to wavFile"), hr );


SAFE_DELETE(g_pWaveFile);

}

CWaveFile error in Directsound

I got this error while using CWaveFile class :


SoundTest.obj : error LNK2001: unresolved external symbol "public: __thiscall CWaveFile::CWaveFile(void)" (??0CWaveFile@@$$FQAE@XZ)
SoundTest.obj : error LNK2001: unresolved external symbol "public: __thiscall CWaveFile::~CWaveFile(void)" (??1CWaveFile@@$$FQAE@XZ)


Solution :
I added the dsutil.h and dsutil.cpp in my application.
I included the library files dxerr9.lib and winmm.lib



Next I got an error like this :

Creating library QuarticsAudioCaptureDLL___Win32_Debug_unicode/AAPIPC2TV.lib and object QuarticsAudioCaptureDLL___Win32_Debug_unicode/AAPIPC2TV.exp
dsutil.obj : error LNK2001: unresolved external symbol _IID_IDirectSound3DListener
dsutil.obj : error LNK2001: unresolved external symbol _IID_IDirectSoundNotify
dsutil.obj : error LNK2001: unresolved external symbol _IID_IDirectSound3DBuffer
QuarticsAudioCaptureDLL___Win32_Debug_unicode/AAPIPC2TV.dll : fatal error LNK1120: 3 unresolved externals
Error executing link.exe.

Solution :

include the library dxguid.lib in ur application

CWaveFile available in DirectSound

class CWaveFile
{
public:
WAVEFORMATEX* m_pwfx; // Pointer to WAVEFORMATEX structure
HMMIO m_hmmio; // MM I/O handle for the WAVE
MMCKINFO m_ck; // Multimedia RIFF chunk
MMCKINFO m_ckRiff; // Use in opening a WAVE file
DWORD m_dwSize; // The size of the wave file
MMIOINFO m_mmioinfoOut;
DWORD m_dwFlags;
BOOL m_bIsReadingFromMemory;
BYTE* m_pbData;
BYTE* m_pbDataCur;
ULONG m_ulDataSize;
CHAR* m_pResourceBuffer;

protected:
HRESULT ReadMMIO();
HRESULT WriteMMIO( WAVEFORMATEX *pwfxDest );

public:
CWaveFile();
~CWaveFile();

HRESULT Open( LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags );
HRESULT OpenFromMemory( BYTE* pbData, ULONG ulDataSize, WAVEFORMATEX* pwfx, DWORD dwFlags );
HRESULT Close();

HRESULT Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead );
HRESULT Write( UINT nSizeToWrite, BYTE* pbData, UINT* pnSizeWrote );

DWORD GetSize();
HRESULT ResetFile();
WAVEFORMATEX* GetFormat() { return m_pwfx; };
};





//-----------------------------------------------------------------------------
// Name: CWaveFile::CWaveFile()
// Desc: Constructs the class. Call Open() to open a wave file for reading.
// Then call Read() as needed. Calling the destructor or Close()
// will close the file.
//-----------------------------------------------------------------------------
CWaveFile::CWaveFile()
{
m_pwfx = NULL;
m_hmmio = NULL;
m_pResourceBuffer = NULL;
m_dwSize = 0;
m_bIsReadingFromMemory = FALSE;
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::~CWaveFile()
// Desc: Destructs the class
//-----------------------------------------------------------------------------
CWaveFile::~CWaveFile()
{
Close();

if( !m_bIsReadingFromMemory )
SAFE_DELETE_ARRAY( m_pwfx );
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::Open()
// Desc: Opens a wave file for reading
//-----------------------------------------------------------------------------
HRESULT CWaveFile::Open( LPTSTR strFileName, WAVEFORMATEX* pwfx, DWORD dwFlags )
{
HRESULT hr;

m_dwFlags = dwFlags;
m_bIsReadingFromMemory = FALSE;

if( m_dwFlags == WAVEFILE_READ )
{
if( strFileName == NULL )
return E_INVALIDARG;
SAFE_DELETE_ARRAY( m_pwfx );

m_hmmio = mmioOpen( strFileName, NULL, MMIO_ALLOCBUF | MMIO_READ );

if( NULL == m_hmmio )
{
HRSRC hResInfo;
HGLOBAL hResData;
DWORD dwSize;
VOID* pvRes;

// Loading it as a file failed, so try it as a resource
if( NULL == ( hResInfo = FindResource( NULL, strFileName, TEXT("WAVE") ) ) )
{
if( NULL == ( hResInfo = FindResource( NULL, strFileName, TEXT("WAV") ) ) )
return DXTRACE_ERR( TEXT("FindResource"), E_FAIL );
}

if( NULL == ( hResData = LoadResource( NULL, hResInfo ) ) )
return DXTRACE_ERR( TEXT("LoadResource"), E_FAIL );

if( 0 == ( dwSize = SizeofResource( NULL, hResInfo ) ) )
return DXTRACE_ERR( TEXT("SizeofResource"), E_FAIL );

if( NULL == ( pvRes = LockResource( hResData ) ) )
return DXTRACE_ERR( TEXT("LockResource"), E_FAIL );

m_pResourceBuffer = new CHAR[ dwSize ];
memcpy( m_pResourceBuffer, pvRes, dwSize );

MMIOINFO mmioInfo;
ZeroMemory( &mmioInfo, sizeof(mmioInfo) );
mmioInfo.fccIOProc = FOURCC_MEM;
mmioInfo.cchBuffer = dwSize;
mmioInfo.pchBuffer = (CHAR*) m_pResourceBuffer;

m_hmmio = mmioOpen( NULL, &mmioInfo, MMIO_ALLOCBUF | MMIO_READ );
}

if( FAILED( hr = ReadMMIO() ) )
{
// ReadMMIO will fail if its an not a wave file
mmioClose( m_hmmio, 0 );
return DXTRACE_ERR( TEXT("ReadMMIO"), hr );
}

if( FAILED( hr = ResetFile() ) )
return DXTRACE_ERR( TEXT("ResetFile"), hr );

// After the reset, the size of the wav file is m_ck.cksize so store it now
m_dwSize = m_ck.cksize;
}
else
{
m_hmmio = mmioOpen( strFileName, NULL, MMIO_ALLOCBUF |
MMIO_READWRITE |
MMIO_CREATE );
if( NULL == m_hmmio )
return DXTRACE_ERR( TEXT("mmioOpen"), E_FAIL );

if( FAILED( hr = WriteMMIO( pwfx ) ) )
{
mmioClose( m_hmmio, 0 );
return DXTRACE_ERR( TEXT("WriteMMIO"), hr );
}

if( FAILED( hr = ResetFile() ) )
return DXTRACE_ERR( TEXT("ResetFile"), hr );
}

return hr;
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::OpenFromMemory()
// Desc: copy data to CWaveFile member variable from memory
//-----------------------------------------------------------------------------
HRESULT CWaveFile::OpenFromMemory( BYTE* pbData, ULONG ulDataSize,
WAVEFORMATEX* pwfx, DWORD dwFlags )
{
m_pwfx = pwfx;
m_ulDataSize = ulDataSize;
m_pbData = pbData;
m_pbDataCur = m_pbData;
m_bIsReadingFromMemory = TRUE;

if( dwFlags != WAVEFILE_READ )
return E_NOTIMPL;

return S_OK;
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::ReadMMIO()
// Desc: Support function for reading from a multimedia I/O stream.
// m_hmmio must be valid before calling. This function uses it to
// update m_ckRiff, and m_pwfx.
//-----------------------------------------------------------------------------
HRESULT CWaveFile::ReadMMIO()
{
MMCKINFO ckIn; // chunk info. for general use.
PCMWAVEFORMAT pcmWaveFormat; // Temp PCM structure to load in.

m_pwfx = NULL;

if( ( 0 != mmioDescend( m_hmmio, &m_ckRiff, NULL, 0 ) ) )
return DXTRACE_ERR( TEXT("mmioDescend"), E_FAIL );

// Check to make sure this is a valid wave file
if( (m_ckRiff.ckid != FOURCC_RIFF) ||
(m_ckRiff.fccType != mmioFOURCC('W', 'A', 'V', 'E') ) )
return DXTRACE_ERR( TEXT("mmioFOURCC"), E_FAIL );

// Search the input file for for the 'fmt ' chunk.
ckIn.ckid = mmioFOURCC('f', 'm', 't', ' ');
if( 0 != mmioDescend( m_hmmio, &ckIn, &m_ckRiff, MMIO_FINDCHUNK ) )
return DXTRACE_ERR( TEXT("mmioDescend"), E_FAIL );

// Expect the 'fmt' chunk to be at least as large as ;
// if there are extra parameters at the end, we'll ignore them
if( ckIn.cksize < (LONG) sizeof(PCMWAVEFORMAT) )
return DXTRACE_ERR( TEXT("sizeof(PCMWAVEFORMAT)"), E_FAIL );

// Read the 'fmt ' chunk into .
if( mmioRead( m_hmmio, (HPSTR) &pcmWaveFormat,
sizeof(pcmWaveFormat)) != sizeof(pcmWaveFormat) )
return DXTRACE_ERR( TEXT("mmioRead"), E_FAIL );

// Allocate the waveformatex, but if its not pcm format, read the next
// word, and thats how many extra bytes to allocate.
if( pcmWaveFormat.wf.wFormatTag == WAVE_FORMAT_PCM )
{
m_pwfx = (WAVEFORMATEX*)new CHAR[ sizeof(WAVEFORMATEX) ];
if( NULL == m_pwfx )
return DXTRACE_ERR( TEXT("m_pwfx"), E_FAIL );

// Copy the bytes from the pcm structure to the waveformatex structure
memcpy( m_pwfx, &pcmWaveFormat, sizeof(pcmWaveFormat) );
m_pwfx->cbSize = 0;
}
else
{
// Read in length of extra bytes.
WORD cbExtraBytes = 0L;
if( mmioRead( m_hmmio, (CHAR*)&cbExtraBytes, sizeof(WORD)) != sizeof(WORD) )
return DXTRACE_ERR( TEXT("mmioRead"), E_FAIL );

m_pwfx = (WAVEFORMATEX*)new CHAR[ sizeof(WAVEFORMATEX) + cbExtraBytes ];
if( NULL == m_pwfx )
return DXTRACE_ERR( TEXT("new"), E_FAIL );

// Copy the bytes from the pcm structure to the waveformatex structure
memcpy( m_pwfx, &pcmWaveFormat, sizeof(pcmWaveFormat) );
m_pwfx->cbSize = cbExtraBytes;

// Now, read those extra bytes into the structure, if cbExtraAlloc != 0.
if( mmioRead( m_hmmio, (CHAR*)(((BYTE*)&(m_pwfx->cbSize))+sizeof(WORD)),
cbExtraBytes ) != cbExtraBytes )
{
SAFE_DELETE( m_pwfx );
return DXTRACE_ERR( TEXT("mmioRead"), E_FAIL );
}
}

// Ascend the input file out of the 'fmt ' chunk.
if( 0 != mmioAscend( m_hmmio, &ckIn, 0 ) )
{
SAFE_DELETE( m_pwfx );
return DXTRACE_ERR( TEXT("mmioAscend"), E_FAIL );
}

return S_OK;
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::GetSize()
// Desc: Retuns the size of the read access wave file
//-----------------------------------------------------------------------------
DWORD CWaveFile::GetSize()
{
return m_dwSize;
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::ResetFile()
// Desc: Resets the internal m_ck pointer so reading starts from the
// beginning of the file again
//-----------------------------------------------------------------------------
HRESULT CWaveFile::ResetFile()
{
if( m_bIsReadingFromMemory )
{
m_pbDataCur = m_pbData;
}
else
{
if( m_hmmio == NULL )
return CO_E_NOTINITIALIZED;

if( m_dwFlags == WAVEFILE_READ )
{
// Seek to the data
if( -1 == mmioSeek( m_hmmio, m_ckRiff.dwDataOffset + sizeof(FOURCC),
SEEK_SET ) )
return DXTRACE_ERR( TEXT("mmioSeek"), E_FAIL );

// Search the input file for the 'data' chunk.
m_ck.ckid = mmioFOURCC('d', 'a', 't', 'a');
if( 0 != mmioDescend( m_hmmio, &m_ck, &m_ckRiff, MMIO_FINDCHUNK ) )
return DXTRACE_ERR( TEXT("mmioDescend"), E_FAIL );
}
else
{
// Create the 'data' chunk that holds the waveform samples.
m_ck.ckid = mmioFOURCC('d', 'a', 't', 'a');
m_ck.cksize = 0;

if( 0 != mmioCreateChunk( m_hmmio, &m_ck, 0 ) )
return DXTRACE_ERR( TEXT("mmioCreateChunk"), E_FAIL );

if( 0 != mmioGetInfo( m_hmmio, &m_mmioinfoOut, 0 ) )
return DXTRACE_ERR( TEXT("mmioGetInfo"), E_FAIL );
}
}

return S_OK;
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::Read()
// Desc: Reads section of data from a wave file into pBuffer and returns
// how much read in pdwSizeRead, reading not more than dwSizeToRead.
// This uses m_ck to determine where to start reading from. So
// subsequent calls will be continue where the last left off unless
// Reset() is called.
//-----------------------------------------------------------------------------
HRESULT CWaveFile::Read( BYTE* pBuffer, DWORD dwSizeToRead, DWORD* pdwSizeRead )
{
if( m_bIsReadingFromMemory )
{
if( m_pbDataCur == NULL )
return CO_E_NOTINITIALIZED;
if( pdwSizeRead != NULL )
*pdwSizeRead = 0;

if( (BYTE*)(m_pbDataCur + dwSizeToRead) >
(BYTE*)(m_pbData + m_ulDataSize) )
{
dwSizeToRead = m_ulDataSize - (DWORD)(m_pbDataCur - m_pbData);
}

CopyMemory( pBuffer, m_pbDataCur, dwSizeToRead );

if( pdwSizeRead != NULL )
*pdwSizeRead = dwSizeToRead;

return S_OK;
}
else
{
MMIOINFO mmioinfoIn; // current status of m_hmmio

if( m_hmmio == NULL )
return CO_E_NOTINITIALIZED;
if( pBuffer == NULL || pdwSizeRead == NULL )
return E_INVALIDARG;

if( pdwSizeRead != NULL )
*pdwSizeRead = 0;

if( 0 != mmioGetInfo( m_hmmio, &mmioinfoIn, 0 ) )
return DXTRACE_ERR( TEXT("mmioGetInfo"), E_FAIL );

UINT cbDataIn = dwSizeToRead;
if( cbDataIn > m_ck.cksize )
cbDataIn = m_ck.cksize;

m_ck.cksize -= cbDataIn;

for( DWORD cT = 0; cT < cbDataIn; cT++ )
{
// Copy the bytes from the io to the buffer.
if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
{
if( 0 != mmioAdvance( m_hmmio, &mmioinfoIn, MMIO_READ ) )
return DXTRACE_ERR( TEXT("mmioAdvance"), E_FAIL );

if( mmioinfoIn.pchNext == mmioinfoIn.pchEndRead )
return DXTRACE_ERR( TEXT("mmioinfoIn.pchNext"), E_FAIL );
}

// Actual copy.
*((BYTE*)pBuffer+cT) = *((BYTE*)mmioinfoIn.pchNext);
mmioinfoIn.pchNext++;
}

if( 0 != mmioSetInfo( m_hmmio, &mmioinfoIn, 0 ) )
return DXTRACE_ERR( TEXT("mmioSetInfo"), E_FAIL );

if( pdwSizeRead != NULL )
*pdwSizeRead = cbDataIn;

return S_OK;
}
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::Close()
// Desc: Closes the wave file
//-----------------------------------------------------------------------------
HRESULT CWaveFile::Close()
{
if( m_dwFlags == WAVEFILE_READ )
{
mmioClose( m_hmmio, 0 );
m_hmmio = NULL;
SAFE_DELETE_ARRAY( m_pResourceBuffer );
}
else
{
m_mmioinfoOut.dwFlags |= MMIO_DIRTY;

if( m_hmmio == NULL )
return CO_E_NOTINITIALIZED;

if( 0 != mmioSetInfo( m_hmmio, &m_mmioinfoOut, 0 ) )
return DXTRACE_ERR( TEXT("mmioSetInfo"), E_FAIL );

// Ascend the output file out of the 'data' chunk -- this will cause
// the chunk size of the 'data' chunk to be written.
if( 0 != mmioAscend( m_hmmio, &m_ck, 0 ) )
return DXTRACE_ERR( TEXT("mmioAscend"), E_FAIL );

// Do this here instead...
if( 0 != mmioAscend( m_hmmio, &m_ckRiff, 0 ) )
return DXTRACE_ERR( TEXT("mmioAscend"), E_FAIL );

mmioSeek( m_hmmio, 0, SEEK_SET );

if( 0 != (INT)mmioDescend( m_hmmio, &m_ckRiff, NULL, 0 ) )
return DXTRACE_ERR( TEXT("mmioDescend"), E_FAIL );

m_ck.ckid = mmioFOURCC('f', 'a', 'c', 't');

if( 0 == mmioDescend( m_hmmio, &m_ck, &m_ckRiff, MMIO_FINDCHUNK ) )
{
DWORD dwSamples = 0;
mmioWrite( m_hmmio, (HPSTR)&dwSamples, sizeof(DWORD) );
mmioAscend( m_hmmio, &m_ck, 0 );
}

// Ascend the output file out of the 'RIFF' chunk -- this will cause
// the chunk size of the 'RIFF' chunk to be written.
if( 0 != mmioAscend( m_hmmio, &m_ckRiff, 0 ) )
return DXTRACE_ERR( TEXT("mmioAscend"), E_FAIL );

mmioClose( m_hmmio, 0 );
m_hmmio = NULL;
}

return S_OK;
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::WriteMMIO()
// Desc: Support function for reading from a multimedia I/O stream
// pwfxDest is the WAVEFORMATEX for this new wave file.
// m_hmmio must be valid before calling. This function uses it to
// update m_ckRiff, and m_ck.
//-----------------------------------------------------------------------------
HRESULT CWaveFile::WriteMMIO( WAVEFORMATEX *pwfxDest )
{
DWORD dwFactChunk; // Contains the actual fact chunk. Garbage until WaveCloseWriteFile.
MMCKINFO ckOut1;

dwFactChunk = (DWORD)-1;

// Create the output file RIFF chunk of form type 'WAVE'.
m_ckRiff.fccType = mmioFOURCC('W', 'A', 'V', 'E');
m_ckRiff.cksize = 0;

if( 0 != mmioCreateChunk( m_hmmio, &m_ckRiff, MMIO_CREATERIFF ) )
return DXTRACE_ERR( TEXT("mmioCreateChunk"), E_FAIL );

// We are now descended into the 'RIFF' chunk we just created.
// Now create the 'fmt ' chunk. Since we know the size of this chunk,
// specify it in the MMCKINFO structure so MMIO doesn't have to seek
// back and set the chunk size after ascending from the chunk.
m_ck.ckid = mmioFOURCC('f', 'm', 't', ' ');
m_ck.cksize = sizeof(PCMWAVEFORMAT);

if( 0 != mmioCreateChunk( m_hmmio, &m_ck, 0 ) )
return DXTRACE_ERR( TEXT("mmioCreateChunk"), E_FAIL );

// Write the PCMWAVEFORMAT structure to the 'fmt ' chunk if its that type.
if( pwfxDest->wFormatTag == WAVE_FORMAT_PCM )
{
if( mmioWrite( m_hmmio, (HPSTR) pwfxDest,
sizeof(PCMWAVEFORMAT)) != sizeof(PCMWAVEFORMAT))
return DXTRACE_ERR( TEXT("mmioWrite"), E_FAIL );
}
else
{
// Write the variable length size.
if( (UINT)mmioWrite( m_hmmio, (HPSTR) pwfxDest,
sizeof(*pwfxDest) + pwfxDest->cbSize ) !=
( sizeof(*pwfxDest) + pwfxDest->cbSize ) )
return DXTRACE_ERR( TEXT("mmioWrite"), E_FAIL );
}

// Ascend out of the 'fmt ' chunk, back into the 'RIFF' chunk.
if( 0 != mmioAscend( m_hmmio, &m_ck, 0 ) )
return DXTRACE_ERR( TEXT("mmioAscend"), E_FAIL );

// Now create the fact chunk, not required for PCM but nice to have. This is filled
// in when the close routine is called.
ckOut1.ckid = mmioFOURCC('f', 'a', 'c', 't');
ckOut1.cksize = 0;

if( 0 != mmioCreateChunk( m_hmmio, &ckOut1, 0 ) )
return DXTRACE_ERR( TEXT("mmioCreateChunk"), E_FAIL );

if( mmioWrite( m_hmmio, (HPSTR)&dwFactChunk, sizeof(dwFactChunk)) !=
sizeof(dwFactChunk) )
return DXTRACE_ERR( TEXT("mmioWrite"), E_FAIL );

// Now ascend out of the fact chunk...
if( 0 != mmioAscend( m_hmmio, &ckOut1, 0 ) )
return DXTRACE_ERR( TEXT("mmioAscend"), E_FAIL );

return S_OK;
}




//-----------------------------------------------------------------------------
// Name: CWaveFile::Write()
// Desc: Writes data to the open wave file
//-----------------------------------------------------------------------------
HRESULT CWaveFile::Write( UINT nSizeToWrite, BYTE* pbSrcData, UINT* pnSizeWrote )
{
UINT cT;

if( m_bIsReadingFromMemory )
return E_NOTIMPL;
if( m_hmmio == NULL )
return CO_E_NOTINITIALIZED;
if( pnSizeWrote == NULL || pbSrcData == NULL )
return E_INVALIDARG;

*pnSizeWrote = 0;

for( cT = 0; cT < nSizeToWrite; cT++ )
{
if( m_mmioinfoOut.pchNext == m_mmioinfoOut.pchEndWrite )
{
m_mmioinfoOut.dwFlags |= MMIO_DIRTY;
if( 0 != mmioAdvance( m_hmmio, &m_mmioinfoOut, MMIO_WRITE ) )
return DXTRACE_ERR( TEXT("mmioAdvance"), E_FAIL );
}

*((BYTE*)m_mmioinfoOut.pchNext) = *((BYTE*)pbSrcData+cT);
(BYTE*)m_mmioinfoOut.pchNext++;

(*pnSizeWrote)++;
}

return S_OK;
}

How to call the DLL from DLL client

For DLL client application :
--------------------------------

1.we have to copy the header file from the DLL application
2.include the library path where the library file resides and copy the library and DLL fucntions
to the application path.
3.we have to include the DLL application's header file in DLL client application and
Call the Function in DLL client application before that

Deleting STL list pointers

list TestList;
list:: iterator Iter;

// Adding data to the
Iter = TestList.begin();
TestList.insert(new Test(),Iter);
Iter++;
TestList.insert(new Test(),Iter);


Test* pT = NULL;




//Removing All the pointers from the STL list as follows :


while( TestList.size() > 0)
{
Iter = TestList.begin();
pT = *Iter;
TestList.remove(pT); //Remove the item from the list
SAFE_DELETE(pT); //Free the resource
}

Thursday, November 01, 2007

C# File Access Problem

 
 I faced the problem in C# file access that is as follows :
 
 
C# file accessing Sample :
-------------------------------------


// Specify file, instructions, and privelegdes
FileStream file = new FileStream("test.txt", FileMode.Open);

// Create a new stream to read from a file
StreamReader sr = new StreamReader(file);

// Read contents of file into a string
string s = sr.ReadToEnd();

// Close StreamReader
sr.Close();

// Close file
file.Close();

  Everywhere people used the sample like this...

I also followed this sample during the development.
 I read the char by char from the streamReader object.

test.txt file is less than 1024 bytes size then the problem will not arise.

But if  file size is greater than 1024 bytes, I got the problem.


 I read the char by char from the StreamReader object.

 char[] szChar;
 szChar = new char[1];
                   strmReader.Read(szChar,0,1);

  
 After reaching the 1024 th character, I got the first character from the file...

So the StreamReader doesnt Read characters from file after 1024 characters.


Reason :

 By default StreamReader buffer size is 1024 bytes. we can also allocate the StreamReader buffer size.

if we read characters after 1024th character, StreamReader's Read() fn will returns the first character from the stream.

This will be repeated...


To avoid this problem, I allocated the StreamReader's size as File Size.
Now  we can traverse from beginning of the file to the end of the file.The code is as follows :

 

 FileStream myFile = new FileStream("test.txt",FileMode.Open);
 FileInfo fileInfo  = new FileInfo("test.txt");
 int FileLength = (int)fileInfo.Length; 
 StreamReader strmReader = new StreamReader( myFile, System.Text.Encoding.UTF8,false, FileLength);