PDA

View Full Version : Storing images or files inside executable?



Nutshell
03-28-2002, 11:38 PM
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

Fordy
03-29-2002, 02:32 AM
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;



#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.

Nutshell
03-29-2002, 04:21 AM
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

Nutshell
03-29-2002, 05:10 AM
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

Fordy
03-29-2002, 06:25 AM
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......


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

I guess....but I never tried


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

Nutshell
03-29-2002, 06:43 AM
wats wrong wif my nonsense method?

GaPe
03-29-2002, 01:04 PM
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.

Nutshell
03-29-2002, 04:12 PM
ah....... i'll try that

Nutshell
03-29-2002, 04:18 PM
nope had the same funny characters in the output.

GaPe
03-30-2002, 03:31 AM
Post the code Nutshell. If you copy file in binary mode it should be working.

Nutshell
03-30-2002, 03:46 AM
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.



#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;
}

GaPe
03-30-2002, 05:27 AM
You should use fscanf function.

Here's the 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;
}

Nutshell
03-30-2002, 06:12 AM
tried ur code, when i execute the outputed exe it says "Program too big to fit in memory".

how?

GaPe
03-30-2002, 06:39 AM
That's very strange. When I execute the outputed exe it's working fine. I compiled in VC++ 6.0.

:rolleyes:

Nutshell
03-30-2002, 06:45 AM
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.

jinx
03-30-2002, 08:31 PM
<poping in>btw...mysterious people in the past have used the same method of storing files within exec's to evade e-mail atachment snarfing from the feds then opening on the other end as "resources" and exporting the data...<leaving>