Problem creating DirectSound primary buffer

I'm trying to learn how to use DirectSound so that I can use it in a game engine I am working on. I'm using MSVC++ 2008 (Without MFC) and the DXSDK March 2008.

The problem I am having is that when I attempt to create the primary buffer using the IDirectSound8::CreateSoundBuffer function the returned HRESULT is always E_INVALIDARG. Since I am relatively inexperienced with C++ chances are it's a quick-fix solution, but I've searched MSDN, Google and Experts-Exchange and I've found nothing to help me.

Thanks in advance.
//Globals
LPDIRECTSOUND8 pDSound = NULL;
LPDIRECTSOUNDBUFFER pDsndPrimary = NULL;
LPDIRECTSOUNDBUFFER8 pDsndSecondary = NULL;
 
int InitDSound(HWND hWnd)
{
	HRESULT hr = CoInitialize(NULL);
 
	hr = DirectSoundCreate8(NULL,&pDSound,NULL);
	if(FAILED(hr))
		return 1;
 
	hr = pDSound->SetCooperativeLevel(hWnd, DSSCL_NORMAL);
	if(FAILED(hr))
		return 1;
 
//The 'problem' portion of my DSound function
	//Set up the wave format
	WAVEFORMATEX wvf;
	ZeroMemory(&wvf, sizeof(WAVEFORMATEX));
 
	wvf.nChannels = 1;
	wvf.wFormatTag = WAVE_FORMAT_PCM;
	wvf.nSamplesPerSec = 11025;
	wvf.wBitsPerSample = 16;
	wvf.nBlockAlign = ((wvf.nChannels*wvf.wBitsPerSample)/8);
	wvf.nAvgBytesPerSec = (wvf.nBlockAlign*wvf.nSamplesPerSec);
	wvf.cbSize = 0;
	
	//Create the buffer description
	DSBUFFERDESC dsDesc;
	ZeroMemory(&dsDesc, sizeof(DSBUFFERDESC));
 
	dsDesc.dwSize = sizeof(dsDesc);
	dsDesc.dwFlags = DSBCAPS_PRIMARYBUFFER;
	dsDesc.guid3DAlgorithm = GUID_NULL;
	dsDesc.lpwfxFormat = &wvf;
	dsDesc.dwBufferBytes = 0;
	dsDesc.dwReserved = NULL;
	
	//Create the buffer
	hr = pDSound->CreateSoundBuffer(&dsDesc, &pDsndPrimary, NULL);
	if(FAILED(hr))
		return 1; //Here hr is equal to E_INVALIDARG and the program quits
 
	return 0;
}

Open in new window

LVL 13
Glen A.IT Project ManagerAsked:
Who is Participating?
 
ee_autoCommented:
Question PAQ'd, 500 points refunded, and stored in the solution database.
0
 
Glen A.IT Project ManagerAuthor Commented:
This problem has been solved:

It seems that the lpwfxFormat member can't be specified in the DSBUFFERDESC structure when you are creating a buffer with the DSBCAPS_PRIMARYBUFFER flag.

This question can be closed.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.