Thread: Storing images or files inside executable?

  1. #1
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020

    Storing images or files inside executable?

    Hi,

    In many programs, the images or other files are packed into an executable so they can be extracted later, for example, winzip's self extracting ability.

    BUt i'm not talking about compression utilities. I want to know hwo to store an image in an executable so i can display it later without any external images files. Much like 'resources' in visual basic. Is it possible to do that?

    If you still don't get what i mean say so and i'll give more details.

    thnx in advance

  2. #2
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Why post this here????? You should post it on the Windows board....

    Anyway, you should be able to import custom resources........you can with VC++.......

    For example;

    [list=1][*]Insert->Resource->Custom[*]Call it "FILE"[*]Insert->Resource->Import[*]Select a file and select "FILE" as its type[*]The IDE gives the resource a name like "IDR_FILE2" (in my case)[/list=1]

    Then type the following code;

    Code:
    #include <windows.h>
    #include "resource.h"
    
    
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst,
                       LPSTR lpszArgs, int nWinMode)
    {
    
    	HRSRC hRes;
    	HGLOBAL hGlobRes;
    	LPBYTE lpData = 0;
    	HANDLE hFile;
    	DWORD dwSize,dwWritten;
    
    	hRes = FindResource(NULL,MAKEINTRESOURCE(IDR_FILE2),"FILE");
    
    	if(hRes == NULL){
    		MessageBox(HWND_DESKTOP,"Could not find resource","Error",
    			MB_OK | MB_ICONEXCLAMATION);
    		return 1;
    	}
    
    	hGlobRes = LoadResource(NULL,hRes);
    
    	if(hGlobRes == NULL){
    		MessageBox(HWND_DESKTOP,"Could not load resource","Error",
    			MB_OK | MB_ICONEXCLAMATION);
    		return 1;
    	}
    
    	lpData = (LPBYTE)LockResource(hGlobRes);
    
    	if(lpData == NULL){
    		MessageBox(HWND_DESKTOP,"Could not lock resource","Error",
    			MB_OK | MB_ICONEXCLAMATION);
    		return 1;
    	}
    
    	dwSize = SizeofResource(NULL,hRes);
    
    	hFile = CreateFile("MyFile.zip",GENERIC_WRITE,0,NULL,
    		CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,NULL);
    
    	if(hFile == INVALID_HANDLE_VALUE){
    		MessageBox(HWND_DESKTOP,"Could not create file","Error",
    			MB_OK | MB_ICONEXCLAMATION);
    		return 1;
    	}
    
    	if(!WriteFile(hFile,lpData,dwSize,&dwWritten,NULL)){
    		MessageBox(HWND_DESKTOP,"Could not write to file","Error",
    			MB_OK | MB_ICONEXCLAMATION);
    		return 1;
    	}
    
    	MessageBox(HWND_DESKTOP,"File Extracted!","Success",MB_OK);
    
    return 0;
    
    }
    That will extract your file (a zipfile in my case).......

    Would a kind Moderator please move this to the proper forum.

  3. #3
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Is this only windows-specific? Can't do this on other platforms?

    also, is is able to achieve the same thing without the use of MSVC?

    thnx

  4. #4
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Here is what i did:

    I made a small hello world program and compiled it to exe. Then i opened the exe file using notepad. I saved it as another .txt file. Then i made another program that reads this text file one charcter at a time and output it to a new .exe file which the program creates using 'fopen()'. However, when i run the .exe which the program creates its output is incorrect, involving funny characters. Am i doin something wrong along the steps or this is jsut plain nonsense?

    thnx and pls also answer my qs in my post above thnx a lot

  5. #5
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Code:
    I made a small hello world program and compiled it to exe.
     Then i opened the exe file using notepad. 
    I saved it as another .txt file. 
    Then i made another program that reads this text file one charcter at a time and output it to a
     new .exe file which the program creates using 'fopen()'. 
    However, when i run the .exe which the program creates its output is incorrect,
     involving funny characters.
     Am i doin something wrong along the steps or this is jsut plain nonsense?
    No....You need to do it as I said......

    Code:
    also, is is able to achieve the same thing without the use of MSVC?
    I guess....but I never tried

    Code:
    Is this only windows-specific? Can't do this on other platforms?
    My example is windows specific....as you mentioned Visual Basic in your question I assumed you wanted it for windows.....

    You will be able to do this on other platforms....but as I dont bother with *nix unless I have to....I cant help
    Last edited by Fordy; 03-29-2002 at 12:38 PM.

  6. #6
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    wats wrong wif my nonsense method?

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Cool

    I made a small hello world program and compiled it to exe. Then i opened the exe file using notepad. I saved it as another .txt file. Then i made another program that reads this text file one charcter at a time and output it to a new .exe file which the program creates using 'fopen()'. However, when i run the .exe which the program creates its output is incorrect, involving funny characters. Am i doin something wrong along the steps or this is jsut plain nonsense?
    You probably opened your file in text mode. This is wrong. If you want to copy a file into other file you have to open file in binary mode.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  8. #8
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    ah....... i'll try that

  9. #9
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    nope had the same funny characters in the output.

  10. #10
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Wink

    Post the code Nutshell. If you copy file in binary mode it should be working.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  11. #11
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Someone told me in another thread to do it using hex numbers, it worked. BUt here is the code anyway and pls tell me what's wrong with it. the child.txt is created by opening the binary file using notepad and save it as .txt. Below i opened the file using "r" mode and ALSO tried "rb" mode.

    Code:
    #include <stdio.h>
    
    int main()
    {
       FILE *fpParent, *fpChild;
       char c;
    
       if ( ( fpParent = fopen( "hello.txt", "r" ) ) != NULL ) {
          if ( ( fpChild = fopen( "child.exe", "wb" ) ) != NULL ) {
             while ( ( c = getc( fpParent ) ) != EOF ) {
                putc( c, fpChild );
             }
    
          }
    
       }
    
    
       system("PAUSE");
       return 0;
    }

  12. #12
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    You should use fscanf function.

    Here's the code:

    Code:
    # include <stdio.h>
    
    int main()
    {
       FILE *fpParent, *fpChild;
       char c;
    
       if ( ( fpParent = fopen( "hello.txt", "rb" ) ) != NULL ) 
       {
          if ( ( fpChild = fopen( "child.exe", "wb" ) ) != NULL ) 
    	  {
             while ( fscanf(fpParent, "%c", &c) != EOF ) 
    		 {
                putc( c, fpChild );
             }
          }
    
       }
       system("PAUSE");
       return 0;
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  13. #13
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    tried ur code, when i execute the outputed exe it says "Program too big to fit in memory".

    how?

  14. #14
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    That's very strange. When I execute the outputed exe it's working fine. I compiled in VC++ 6.0.

    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  15. #15
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    I dun mean errors in the compiling process.; I mean when i execute the program the output is " Program too big to fit in memory". i am using DevC++ on Winxp Pro.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Executable files in C++ Builder from Borland
    By ma55acre in forum C++ Programming
    Replies: 1
    Last Post: 04-09-2002, 10:26 AM
  2. reinserting htm files into chm help files
    By verb in forum Windows Programming
    Replies: 0
    Last Post: 02-15-2002, 09:35 AM
  3. displaying text files, wierd thing :(
    By Gades in forum C Programming
    Replies: 2
    Last Post: 11-20-2001, 05:18 PM
  4. inputting of files and storing to variables
    By curtner in forum C++ Programming
    Replies: 2
    Last Post: 10-11-2001, 03:10 PM
  5. storing string objects to binary files
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 10-06-2001, 11:33 PM