Embedding files using C++

Discussion in 'Programmer's Corner' started by metalcords99, Dec 8, 2005.

  1. metalcords99

    Thread Starter New Member

    Nov 23, 2005
    7
    0
    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
     
  2. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    Insert the wav file into the rc as binary data, load the data (resource) during run-time and play it as usual.
     
  3. metalcords99

    Thread Starter New Member

    Nov 23, 2005
    7
    0
    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
     
  4. Brandon

    Senior Member

    Dec 14, 2004
    306
    0
    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?
     
  5. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    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.
     
  6. metalcords99

    Thread Starter New Member

    Nov 23, 2005
    7
    0
    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
     
  7. Brandon

    Senior Member

    Dec 14, 2004
    306
    0
    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.
     
  8. metalcords99

    Thread Starter New Member

    Nov 23, 2005
    7
    0
    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?
     
  9. metalcords99

    Thread Starter New Member

    Nov 23, 2005
    7
    0
    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
     
  10. vineethbs

    Well-Known Member

    Nov 14, 2004
    56
    0
    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;
     
  11. metalcords99

    Thread Starter New Member

    Nov 23, 2005
    7
    0
    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
     
  12. Brandon

    Senior Member

    Dec 14, 2004
    306
    0
    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.

    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.
     
  13. n9352527

    AAC Fanatic!

    Oct 14, 2005
    1,198
    4
    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.
     
  14. metalcords99

    Thread Starter New Member

    Nov 23, 2005
    7
    0
    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?
     
  15. schutnik

    New Member

    Dec 19, 2005
    4
    0
    Google for "Using the MCIWnd window class". That should give you a decent example.

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