Play Wav resource | CodeGuru

Play Wav resource

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: WAVE […]

Written By
CodeGuru Staff
CodeGuru Staff
Nov 14, 1998
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

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:projectssoundscool.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");
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.