Embedding files using C++

Thread Starter

metalcords99

Joined Nov 23, 2005
7
hello
i'm trying to make an .exe file using c++ that can play WAV files... i managed to do that but there's one problem... if the WAV file isn't already on the hard disk, the program can't play it.
so i need to know how can i embed the WAV file into the exe so it can play the audio whenever the file is executed
thanks in advance
my e-mail: metalcords99@hotmail.com
 

Thread Starter

metalcords99

Joined Nov 23, 2005
7
Originally posted by n9352527@Dec 8 2005, 10:02 AM
Insert the wav file into the rc as binary data, load the data (resource) during run-time and play it as usual.
[post=12299]Quoted post[/post]​
i know i have to insert it as a resource file... but i don't know the procedure nor the syntax code for it... so if it's no trouble can you please give me an example code or a link to such code... (BTW, i have borland c++ 5 and visual studio.net 2003) so any procedure would work
thanks for replying to my topic
i really appreciate your help
 

Brandon

Joined Dec 14, 2004
306
Why not just have the .wav and the .exe in the same directory so one can access the other? Zip them up in an self executable archive.

You tell them, run PROB.EXE to install, then run RealProg.exe to run.

Is there reason why u need to embed it?
 

n9352527

Joined Oct 14, 2005
1,198
I don't have a copy of VisualC++ on hand, but the procedure is to open the resource file (resource editor) and then insert a new resource item. The type should be set to binary, you could then either copy and paste the binary data from your wav file (open it first by using binary type and copy the data) or link the resource item to your wav file.

I have no idea how to do it with Borland, the last time I used Borland it was version 3.1 years and years ago. Back then Borland has an excellent stand alone resource editor and compiler (bwrcc if I remember correctly). It could practically hack any resource comprehensively.
 

Thread Starter

metalcords99

Joined Nov 23, 2005
7
Originally posted by Brandon@Dec 9 2005, 03:41 AM
Why not just have the .wav and the .exe in the same directory so one can access the other? Zip them up in an self executable archive.

You tell them, run PROB.EXE to install, then run RealProg.exe to run.

Is there reason why u need to embed it?
[post=12314]Quoted post[/post]​
well when you make an exe and put a wav file with it, people tend to mess with the resources you're using (the wav file in this case) so to prevent this, i want to have the wav embedded inside the exe, so when the user loads the program, it copies the wav into a temporary folder and runs it from there.
i will try what you suggested n9352527... hoping i know how to do that :)
thanks a lot
 

Brandon

Joined Dec 14, 2004
306
Originally posted by metalcords99@Dec 9 2005, 09:00 AM
well when you make an exe and put a wav file with it, people tend to mess with the resources you're using (the wav file in this case) so to prevent this, i want to have the wav embedded inside the exe, so when the user loads the program, it copies the wav into a temporary folder and runs it from there.
i will try what you suggested n9352527... hoping i know how to do that :)
thanks a lot
[post=12332]Quoted post[/post]​
Ahh.. understood.. but its a wav..? You just set your mic to record 'What u hear' run the prog and capture it, but you're going to be giving it to them anyways by copying it to a folder when the program runs, so whats the difference?

Just thinking about time n effort is all.
 

Thread Starter

metalcords99

Joined Nov 23, 2005
7
Originally posted by Brandon@Dec 10 2005, 12:04 AM
Ahh.. understood.. but its a wav..? You just set your mic to record 'What u hear' run the prog and capture it, but you're going to be giving it to them anyways by copying it to a folder when the program runs, so whats the difference?

Just thinking about time n effort is all.
[post=12344]Quoted post[/post]​
you'll have the initial source inside your exe... so everytime you run the program, it copies the wav from the exe to a temp folder, and runs the wav file... then at the end of the program it automatically erases the wav from the temp.
so the initial wav you want is always kept in the exe, so no one can mess with it..
but i'm starting to think that i misunderstood what "embed" means... isn't it that instead of having an exe that runs an external wav, you can have one file (the exe) and inside the exe there's the wav?
 

Thread Starter

metalcords99

Joined Nov 23, 2005
7
btw, i might have forgot to say that the wav file isn't being captured by the user.. i have a certain wav file that i want my program to run
 

vineethbs

Joined Nov 14, 2004
56
ok, just an idea, but i think it wud work

if u can use a pointer to access ur data, then i think this is possible.
take the binary data and put it in an assembly file with the label made global.
also maybe make a global function in that assembly code that returns a pointer to this data location.
Compile the actual program with this function defined as extern.
assemble <prog1> to obj code
compile <prog2> to obj code
link them together to one exe

i guess this would work.

------------------------------
:D well another solution is like this

hexdump the wav file
add this to ur source file like this

unsigned char wav_file[] = "\x33\x24...";
// note that \x is needed
then use maybe
unsigned long dataptr = (unsigned long *) wave_file;
 

Thread Starter

metalcords99

Joined Nov 23, 2005
7
hello

i managed to achieve my goal, but not through the method i wanted.
see a .wav file is recognized by packs... each packs contains five decimal numbers: the first two, and the fifth decimals are five digit numbers each.. the thrid and fourth are negative 8 digit numbers each...
so what i managed to do is make a program that retrieves the packs... and believe me the number of packs is HUUUUUUUUGGE... so anyways, it gets each pack at a time and writes it in another .cpp file... (let's say final.cpp).
when the program finishes depacking the wav file into the final.cpp, i make the final.exe write the packs in another .wav and run the wav
of course, this works for small .wav files... cause the problem now is if the .wav was too big, then the compiler won't be able to process all the lines in final.cpp and generate an error "out of memory".
so how can i prevent the compiler from running out of memory?

oh and one more thing... :) i'm not very advanced in C language, so when you guys explain how to embed, i don't understand everything you say.. so i still don't know how to embed :(

thanks for replying
i know i've been annoying :D
 

Brandon

Joined Dec 14, 2004
306
You can use an MP3 or resample the wav in a program to a lower bit rate and sampling freq OR don't put the wav in the program.

btw, i might have forgot to say that the wav file isn't being captured by the user.. i have a certain wav file that i want my program to run
I believe you said that. I was just pointing out that any user can set up their PC to record any sound their PC makes. Also, if your writing it to the HD, its there, erasing it or not, its still there. They user will have access to the wav regardless of whats done.

You can always encrypt the wav and have 2 files and u wouldn't have the memory issue or anyone being able to mess with the wav directly.
 

n9352527

Joined Oct 14, 2005
1,198
Originally posted by metalcords99@Dec 10 2005, 11:26 AM
you'll have the initial source inside your exe... so everytime you run the program, it copies the wav from the exe to a temp folder, and runs the wav file... then at the end of the program it automatically erases the wav from the temp.
so the initial wav you want is always kept in the exe, so no one can mess with it..
but i'm starting to think that i misunderstood what "embed" means... isn't it that instead of having an exe that runs an external wav, you can have one file (the exe) and inside the exe there's the wav?
[post=12351]Quoted post[/post]​
Yes, the wav is inside the exe and you don't need to write the wav to an external file to play it. You can 'load' the wav internally and play it. It might need some hack but it is perfectly possible. Are you using MFC? It should be fun trying to hack MFC classes to load a wav from resource and play it.

In VS .NET, switch to Resource View. Menu: Project - Add Resource. In the Add Resource dialog box click Custom, give a name to the custom resource e.g. wav. It will open a new item of type wav in an edit window. This will default to binary. You could either copy a binary wav data to the edit window or link the wav file to the resource item. To link, click the wav item (e.g. IDR_WAV1) and on the properties box set the Filename property to your wav file. Done.
 

Thread Starter

metalcords99

Joined Nov 23, 2005
7
i'll try to do that... (encrypting or adding resource in VS.net)

in the mean time.. i have another question :)
when you want to play a .wav sound you use the function "playsound" or "sndplaysound". so what if i want to play a MIDI file, what function should i use?
 

schutnik

Joined Dec 19, 2005
4
Originally posted by metalcords99@Dec 11 2005, 06:16 AM
so what if i want to play a MIDI file, what function should i use?
[post=12371]Quoted post[/post]​
Google for "Using the MCIWnd window class". That should give you a decent example.

edit: also, see this for a better example.
 
Top