File Embedder Project in C

Some times it is necessary for our compiled project to have it’s supporting files embedded within the EXE module itself so that the supporting files may not be put into a seperate folder and carried along with the project.So here i am presenting you with the source code of the FILE EMBEDDER UTILITY project.

This utility can be used to embed one file with in the other.ie:Suppose we need to embed a .bat file(or any other file *.exe,*bmp,.txt…..) into our final project so that the batch file is present with in the compiled module and is hidden from the users avoiding tthe need to carry the .bat file every time with the project.

Both the Embedding and extraction process has been presented in seperate functions for your convenience.here’s the code…..

#include
#include
#include
#include
#include
#include
#include

void embed(void);
void extract(void);

char buff[1],sname[128],tname[128],dname[128],choice;
unsigned long int size=0;long int psize=0;int outh,bytes=0;
FILE *source,*target,*data;

void main()
{
while(1)
{
clrscr();
puts(”\n\n\t\t\tFILE EMBEDDING UTILITY BY SRIKANTH\n\n\n”);
puts(”1.Embed A File 2. Extract A File 3.Exit\n”);
choice=getch();
switch(choice)
{
case ‘1′:embed();
getch();
break;
case ‘2′:
extract();
getch();
break;
default:
exit(0);
}
}
}

void embed()
{
puts(”\nEnter The Source Filename\n”);
scanf(”%s”,sname);
source=fopen(sname,”rb+”);
if(source==NULL)
{
puts(”\nCannot Open The Source File\n”);
return;
}
puts(”\nEnter The Target Filename\n”);
scanf(”%s”,tname);
outh=open(tname,O_RDONLYO_BINARY);
if(outh==-1)
{
puts(”\nCannot Open The Target File\n”);
return;
}
printf(”\nReading The Source File Please Wait…\n”);
while((bytes=read(outh,buff,1))>0)
size+=bytes;
data=fopen(”Data.cfg”,”w”);
if(data==NULL)
{
puts(”\nCannot Create Configuration The File\n”);
return;
}
fprintf(data,”%lu”,size);
close(outh);
fclose(data);
target=fopen(tname,”rb”);
if(target==NULL)
{
puts(”Cannot Open Target File\n”);
return;
}
printf(”\nEmbedding Please Wait…\n”);
fseek(source,0,SEEK_END);
while(fread(buff,1,1,target)>0)
fwrite(buff,1,1,source);
fcloseall();
printf(”\nEmbedding Completed Successfully\n”);
}

void extract()
{
printf(”\nEnter The Source Filename\n”);
scanf(”%s”,sname);
source=fopen(sname,”rb”);
if(source==NULL)
{
printf(”\nCannot Open The Source File\n”);
return;
}
printf(”\nEnter The Target Filename(eg: abc.exe)\n”);
scanf(”%s”,tname);
printf(”\nEnter The Configuration Filename(eg: DATA.cfg)\n”);
scanf(”%s”,dname);
data=fopen(dname,”r”);
if(data==NULL)
{
printf(”\nConfiguration File Not Found\n”);
return;
}
fscanf(data,”%ld”,&psize);
target=fopen(tname,”wb”);
if(target==NULL)
{
puts(”\nCannot Open The Target File\n”);
return;
}
printf(”\nExtracting Please Wait…\n”);
fseek(source,-psize,SEEK_END);
while((fread(buff,1,1,source))>0)
fwrite(buff,1,1,target);
printf(”\nFile Extraction Completed Successfully\n”);
fcloseall();
}

Post a Comment

Previous Post Next Post