Advertisement
3_2004-2005 Graphics/ Sound #145620

How to compile sound files directly into an exe.

Shows a technique for having wav files compiled directly into an executable.

AI

AI Summary: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Source Code
original-source
/*This is an example of how to have .wav files compiled as resources directly into your 
<br>
executable file. In vc++ 6.0 I could not find an option to insert these types of
<br>
 resources directly. I did, however, find a bit of info on how to do this in 
<br>
 directly. First, copy the wav file into your project folder. Then open the 
<br>
 .rc file using notepad. Add the following lines, changing them to match your wav file:
<br>
<br>
/////////////////////////////////////////////////////////////////////////////
<br>
//
<br>
// WAVE
<br>
//
<br>
<br>
IDR_WAVE_INFO      WAVE  MOVEABLE PURE  "info.wav"
<br>
<br>
IDR_WAVE_INFO is the id for the wave .WAVE MOVEABLE PURE has to do with the type of 
<br>
resource it is. "info.wav" is, of course, the path of the resource...in this case 
<br>
that happens to 
<br>
be in our project folder. Calling this will be different then calling a .wav 
<br>
external to your .exe.The following function illustartes how to do this:*/
<br>
<br>
void playres()
<br>
{
<br>
      	HGLOBAL hGlobMem;//handle to a global memory lock
<br>
      	HRSRC hWaveRes;//handle to a resource
<br>
    if (hWaveRes = FindResource(NULL,"IDR_WAVE_INFO","WAVE"))//using findresource to get a handle to the resurce
<br>
     {  // Resource intact; load into GLOBAL MEMORY/GMEM_SHARED memory
<br>
      if (hGlobMem = LoadResource(NULL,hWaveRes))//using load resource to load it into a global mem block
<br>
        { // Load resource into global memory and play.
<br>
         // Play sound resource via sndPlaySound() using
<br>
         // SND_MEMORY flag.
<br>
         // Application waits until sndPlaySound completes
<br>
         // given SND_SYNC.
<br>
         // SND_MEMORY (first parameter is ptr to memory image
<br>
         // vs. filename).
<br>
         sndPlaySound((LPSTR)LockResource(hGlobMem),SND_SYNC | SND_MEMORY);
<br>
         FreeResource(hGlobMem);
<br>
        } 
<br>
        else MessageBox(NULL,"No resource!","Multimedia Sampler!",MB_ICONHAND);
<br>
      GlobalFree(hGlobMem);//be nice and free the global mem block
<br>
     } 
<br>
     else MessageBox(NULL,"Lost resource!","Multimedia Sampler!",MB_ICONHAND);
<br>
}<br>
<br>
P.S....I received a feedback that mentioned the 
<br>
following point I forgot to bring up about making <br>
a reference to winmm.lib:<br>
thejackal0101@hotmail.com<br>
Comment: maybe this one is little bit more easy <br>
make sure you you put the lib in your project <br>
winmm.lib and put the wave file in the resource
this just example <br>
this was just an idea nothing big <br>
#include<windows.h><br>
#define IDI_FACE 101<br>
#define IDR_WAVE1 102<br>
LRESULT CALLBACK WndProc(HWND hWnd,UINT iMsg,WPARAM wParam,LPARAM lParam)<br>
{<br>
  switch(iMsg)<br>
  {<br>  
  case WM_CREATE:<br>
//playing sound  <br>
PlaySound(MAKEINTRESOURCE(IDR_WAVE1),NULL, SND_RESOURCE | SND_ASYNC );<br>
<br>
  break;<br>
  
  case WM_CLOSE:<br>
    PostQuitMessage(0);<br>
    break;<br>
  case WM_DESTROY:<br>
    DestroyWindow(hWnd);<br>
    break;<br>
  default:<br>
    return DefWindowProc(hWnd,iMsg,wParam,lParam);<br>
  }<br>
  return 0;<br>
}<br>
<br>
..he's right...same idea but a bit shorter code....mad props<br>
<br>

Original Comments (3)
Recovered from Wayback Machine