Sign in to follow this  
Megamike55

Loading .wav from resource (crashes)

Recommended Posts

I'm a bit of a novice when it comes to loading things from resources, and still more a novice when it comes to Direct sound. Therefore I'm using the CSoundManager and CSound classes, but I am still having trouble. The following is my code to load my sound from memory, but it does not work (program crashes when it tries to load). Could someone give me a few tips? Thanks -Mike

CSoundManager* g_pSoundManager = NULL;
CSound*        g_pSound[gCGNumSounds];


bool loadSoundFromRes(int s_i, char *pResourceName, char *pResourceType) {
	// from resources
	HRSRC hRes;
	hRes = FindResource(NULL, pResourceName, pResourceType);

	DWORD	dwDataSize	= SizeofResource(NULL,hRes);
	HGLOBAL	hGlob		= LoadResource(NULL,hRes);
	LPVOID	pData		= LockResource(hGlob);

	// allocate some global memory and copy the resource data into it..
	HGLOBAL hGlobal = GlobalAlloc(GMEM_MOVEABLE, dwDataSize);
	void* pData2 = GlobalLock(hGlobal);
	memcpy(pData2, pData, dwDataSize);

	// unlock and free the resource
	GlobalUnlock(hGlobal);
	UnlockResource(hGlob);
	FreeResource(hGlob);

	bool result = true;
	HRESULT hr;
	WAVEFORMATEX wf;
	if (FAILED(hr = g_pSoundManager->CreateFromMemory(&g_pSound[s_i], (BYTE*)pData2, dwDataSize, &wf))) 
	{
		MessageBox( hWnd, "Could not load .wav file from resources!", "Error", MB_OK | MB_ICONERROR );
		result = false;
	}
	GlobalFree(hGlobal);
	return result;
}


I'm calling this function with:
if (loadSoundFromRes(s_i, MAKEINTRESOURCE(IDR_ARCSWAP), "WAV") == false) failed = true;


Share this post


Link to post
Share on other sites
Well, you've told us virtually nothing about the nature of the crash, i.e. where it crashes in that function, values you are passing that function, or indeed if the objects involved are valid. I would start with running the program with your debugger and see exactly where it crashes. Then you can work from that point backwards through the callstack as necessary.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this