How to compile sound files directly into an exe.

Submitted on: 1/4/2015 4:48:00 AM
By: John Bell (from psc cd)  
Level: Intermediate
User Rating: By 4 Users
Compatibility: Microsoft Visual C++
Views: 1000
     Shows a technique for having wav files compiled directly into an executable.

This article has accompanying files
				/*This is an example of how to have .wav files compiled as resources directly into your 

executable file. In vc++ 6.0 I could not find an option to insert these types of
resources directly. I did, however, find a bit of info on how to do this in
directly. First, copy the wav file into your project folder. Then open the
.rc file using notepad. Add the following lines, changing them to match your wav file:



IDR_WAVE_INFO is the id for the wave .WAVE MOVEABLE PURE has to do with the type of
resource it is. "info.wav" is, of course, the path of the this case
that happens to
be in our project folder. Calling this will be different then calling a .wav
external to your .exe.The following function illustartes how to do this:*/

void playres()
HGLOBAL hGlobMem;//handle to a global memory lock
HRSRC hWaveRes;//handle to a resource
if (hWaveRes = FindResource(NULL,"IDR_WAVE_INFO","WAVE"))//using findresource to get a handle to the resurce
{// Resource intact; load into GLOBAL MEMORY/GMEM_SHARED memory
if (hGlobMem = LoadResource(NULL,hWaveRes))//using load resource to load it into a global mem block
{ // Load resource into global memory and play.
// Play sound resource via sndPlaySound() using
// SND_MEMORY flag.
// Application waits until sndPlaySound completes
// given SND_SYNC.
// SND_MEMORY (first parameter is ptr to memory image
// vs. filename).
sndPlaySound((LPSTR)LockResource(hGlobMem),SND_SYNC | SND_MEMORY);
else MessageBox(NULL,"No resource!","Multimedia Sampler!",MB_ICONHAND);
GlobalFree(hGlobMem);//be nice and free the global mem block
else MessageBox(NULL,"Lost resource!","Multimedia Sampler!",MB_ICONHAND);

P.S....I received a feedback that mentioned the
following point I forgot to bring up about making
a reference to winmm.lib:
Comment: maybe this one is little bit more easy
make sure you you put the lib in your project
winmm.lib and put the wave file in the resource this just example
this was just an idea nothing big
#define IDI_FACE 101
#define IDR_WAVE1 102
//playing sound

case WM_CLOSE:
return DefWindowProc(hWnd,iMsg,wParam,lParam);
return 0;

..he's right...same idea but a bit shorter code....mad props

winzip iconDownload article

Note: Due to the size or complexity of this submission, the author has submitted it as a .zip file to shorten your download time. Afterdownloading it, you will need a program like Winzip to decompress it.Virus note:All files are scanned once-a-day by Planet Source Code for viruses, but new viruses come out every day, so no prevention program can catch 100% of them. For your own safety, please:
  1. Re-scan downloaded files using your personal virus checker before using it.
  2. NEVER, EVER run compiled files (.exe's, .ocx's, .dll's etc.)--only run source code.

If you don't have a virus scanner, you can get one at many places on the net

Other 3 submission(s) by this author


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this article (in the Intermediate category)?
(The article with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)

Other User Comments

 There are no comments on this submission.

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular article, please click here instead.)

To post feedback, first please login.