This article describes how to add your own sound resources to the VC resource file so that your application can play the sound.
1, first open the resource file (. rc file) with a text editor (such as Notepad)
In the end add your own sound resources, as follows
IDW WAVE "c:\\kav\\sound\\virus.wav"
Save the resource file.
2, to start another thread in the application where you want to play the sound, to play the sound
AfxBeginThread((AFX_THREADPROC)sound,NULL,THREAD_PRIORITY_NORMAL);
3, add the thread callback function
Here's how to load a resource. First, the handle of the instance is obtained by the function AfxGetInstanceHandle (), and then the sound resource is found by using the function FindResource.
HRSRC FindResource(
HMODULE hModule, // module handle
LPCTSTR lpName, // resource name
LPCTSTR lpType // resource type
)
After you find the sound resource, use LoadResource to join the resource
HGLOBAL LoadResource(
HMODULE hModule, // module handle
HRSRC hResInfo // resource handle
);
Finally, the resource memory block is locked and the virtual memory address of the memory block that is calibrated is returned. If the resource is successfully locked, the return value points to the first byte at the beginning of the resource:
LPVOID pv=LockResource()
Note: If the problem occurs in any of the four steps above, return and release the corresponding memory. The next thing to do is to load data based on the file data type.
UINT CPlaySoundView::sound(LPVOID pParam)
{
HINSTANCE h=AfxGetInstanceHandle();
HRSRC hr=FindResource(h,"IDW","WAVE");
HGLOBAL hg=LoadResource(h,hr);
LPSTR lp=(LPSTR)LockResource(hg);
sndPlaySound(lp,SND_MEMORY|SND_SYNC);
FreeResource(hg);
return 0;
}