dcsimg
September 18, 2020
Hot Topics:

Play Wav resource

  • By Anthony Petruso
  • Send Email »
  • More Articles »

Well, this doesn't really deal with MFC, but its useful knowledge if you don't know how..

Parts of this code I ripped from VC++ 4.1 help, but it was outdated and didn't come close to working.

First of all you need to add the wave files to the .rc file manually like so:


<NameOfSound> WAVE <Location of WAVE.>
Example being

Cool WAVE C:\projects\sounds\cool.wav

Then you need to add this function declarion to the class you plan on using..


BOOL PlayResource(LPSTR lpName)
{
    BOOL bRtn;
    LPSTR lpRes;
    HANDLE hRes;
    HRSRC hResInfo;
    HINSTANCE Nl=AfxGetInstanceHandle();

    /* Find the WAVE resource. */
    hResInfo= FindResource(Nl,lpName,"WAVE");
    if(hResInfo == NULL)
       return FALSE;
    /* Load the WAVE resource. */

    hRes = LoadResource(Nl,hResInfo);
    if (hRes == NULL)
      return FALSE;

    /* Lock the WAVE resource and play it. */
    lpRes=(LPSTR)LockResource(hRes);
    if(lpRes==NULL)
      return FALSE;

    bRtn = sndPlaySound(lpRes, SND_MEMORY | SND_SYNC);
    if(bRtn == NULL)
      return FALSE;

    /* Free the WAVE resource and return success or failure. */
    FreeResource(hRes);
    return TRUE;
}

Then to play the sound you simply use:


PlayResource("<soundname>");
Example being

PlayResource("Cool");






This article was originally published on November 14, 1998

Enterprise Development Update

Don't miss an article. Subscribe to our newsletter below.


Thanks for your registration, follow us on our social networks to keep up-to-date