Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to insert a .wav in the resource of an exe file and play it using Playsound()?

Posted on 2008-06-10
7
985 Views
Last Modified: 2013-11-17
How to insert a .wav in the resource of an exe file and play it using Playsound()?
0
Comment
Question by:Xyptilon2
  • 3
  • 2
7 Comments
 
LVL 16

Accepted Solution

by:
George Tokas earned 175 total points
ID: 21767821
Not a hard task...
In fact I wrote an article about resource files linked to an executable or inside a resource dll on http://bcbjournal.com.
So... Create a text file in notepad.
First line:
THEWAVFILE     WAV   TheWaveFile.wav

Save the file with extension .rc
Copy Brcc32.exe from bin directory to the directory of the rc file and of course the wav file....
Both files has to be present (rc and wav) or the content of the text file as file names.

Open a command prompt on that directory to use brcc to create a .res file as follows:
brcc32 -32 TheFileName.rc TheFileName.res
A .res file will be created with a size a bit greater than all the files included inside.
Add the res file to the executable using Project|Add To Project
The next time you will compile and link the wav file or whatever else will be included in the executable.

The hard part is to play the sound...
Usually I'm using DirectSound for that so all I ever needed is a buffer...
ANYWAY:
You can load the sound to a TMemoryStream as follows:
TMemoryStream   *Stream;
       HRSRC   ResHandle;
        HGLOBAL MemHandle;
        void *ResPtr;
        int   ResSize;
        char  *lpName;

        ResHandle = FindResource(HInstance, "THEWAVFILE", "WAV);
        MemHandle = LoadResource(HInstance, ResHandle);

Using the TMemoryStream->SaveToFile function you can save to a temporary file before use the playsound function if there isn't any other way.


George Tokas.
        ResPtr = LockResource(MemHandle);
        ResSize = SizeofResource(HInstance, ResHan-dle);
        Stream = new TMemoryStream;
        Stream->SetSize(ResSize);
        Stream->Seek(0,0);
        Stream->Write(ResPtr, ResSize);
        Stream->Seek(0,0);
       
0
 
LVL 25

Assisted Solution

by:kode99
kode99 earned 75 total points
ID: 21773162
PlaySound works fine you just have to tell it the 'file' is a resource as follows:  

PlaySound("THEWAVEFILE",NULL, SND_RESOURCE);

or

sndPlaySound("THEWAVEFILE", SND_RESOURCE);

You do need to include the mmsystem.h header file.

I believe rc file needs to use WAVE not just WAV.  Depending on the file path/name you might have to use "" around the file name as well,  for example if the name has any spaces you need the quotes.

So first line:
THEWAVFILE WAVE TheWaveFile.wav

You can actually just add the .rc file to the project and the CBuilder will take care of building the .res file automatically.  Either way it gets the job done.

0
 
LVL 16

Expert Comment

by:George Tokas
ID: 21777218
>>THEWAVFILE WAVE TheWaveFile.wav
Whatever can be used as long as the middle description in this line and the last variable on FindResource() is the same...
I tried that and verify it...:-)
Sometimes one needs to confuse others, like a kind of cryptography....

George Tokas.
0
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 25

Expert Comment

by:kode99
ID: 21781119
When doing the PlaySound/sndPlaySound methods to directly play the audio  from the resource only 'WAVE' works (using the SND_RESOURCE flag).  I would guess those MS SDK calls have hardcoded thier resource lookups.

If you load it into memory manually you are right,  anything will do as long as you  match the call to what you put in the resource file.

0
 
LVL 16

Expert Comment

by:George Tokas
ID: 21781606
>>When doing the PlaySound/sndPlaySound methods to directly play the audio  from the resource only 'WAVE' works

YES!
What I mean is to insert to and extract from resource...
I'm using that long time now, have all wav files inside a dll but I'm using DirectSound to play them...
DirectSound plays the sounds from a buffer (DirectSoundBuffer)...

George Tokas.
0
 
LVL 13

Author Closing Comment

by:Xyptilon2
ID: 31465906
Thanks guys! I got it working!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to synchronize PHP projects with a remote server in NetBeans IDE 8.0 for Windows.

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question