首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

写了个捆绑和分解的验证性代码,希望大家帮小弟我看看

2012-02-16 
写了个捆绑和分解的验证性代码,希望大家帮我看看//exebind.cpp:定义控制台应用程序的入口点。//还需要另外

写了个捆绑和分解的验证性代码,希望大家帮我看看
//   exebind.cpp   :   定义控制台应用程序的入口点。
//还需要另外一个附属exe文件,用于把捆绑的可执行文件分解出来.

#include   "stdafx.h "
#include   "windows.h "
#include   "stdio.h "
#include   "atlstr.h "
#include   <sys/stat.h>

typedef   struct   tagMyBINDSTRUCT
{
char   cFilenameHost[40];         //   宿主文件名.
char   cFilenameClient[40];     //   被捆绑文件名.
char   cFilenameUnbind[40];//解压缩程序名
DWORD   nFileSizeUnbind;//   解压缩程序大小
        DWORD   nFileSizeHost;                 //   宿主文件长.
        DWORD   nFileSizeClient;             //   被捆绑文件长.
        DWORD   nFileSizeBind;                 //   已捆绑文件长.
}   MYBINDINFO;

int   main(int   argc,char*   argv[])

{

MYBINDINFO   bind;
memset(&bind,0,sizeof(MYBINDINFO));//IT   IS   IMPORTANT;
if(argc!=4)
{
printf( "the   argument   is   error! ");
return   0;
}
else
printf( "the   argument   is   right\n\r ");
//printf( "%s ",argv[2]);
CString   strFilenameClient=argv[2];
strcpy(bind.cFilenameClient,strFilenameClient);
//bind.cFilenameClient[40]=strFilenameClient;




printf( "%s,there   is   aaa\n\r ",bind.cFilenameClient);


//bind.cFilenameHost[40]=*argv[1];//the   next,i   should   print   th   strFilenameHost   var.verify   the   kind   of   strFinenameHost.
CString   strFilenameHost=argv[1];//bind.cFilenameHost;
strcpy(bind.cFilenameHost,strFilenameHost);
//bind.cFilenameUnbind[40]=*argv[3];
CString   strFilenameUnbind=argv[3];
strcpy(bind.cFilenameUnbind,strFilenameUnbind);

CString   strFileTemname= "tempfile.exe ";
struct   _stat   ST;
int   error;
//DWORD   dwError;
FILE*   fpreadHost,*fpreadClient,*fpreadUnbind,*fpwriteTem;
error=_stat(strFilenameClient,&ST);
if(error!=0)
{
printf( "prg   fail   ,the   error   is   %d\n ",GetLastError());
}

bind.nFileSizeClient=ST.st_size;
printf( "the   file   size   is   %d\n\r ",bind.nFileSizeClient);
_stat(strFilenameHost,&ST);
bind.nFileSizeHost=ST.st_size;
printf( "the   file   size   is   %d\n\r ",bind.nFileSizeHost);
_stat(strFilenameUnbind,&ST);
bind.nFileSizeUnbind=ST.st_size;
printf( "the   file   size   is   %d\n\r ",bind.nFileSizeUnbind);
bind.nFileSizeBind=bind.nFileSizeClient+bind.nFileSizeHost+bind.nFileSizeUnbind+sizeof(MYBINDINFO);
printf( "the   finial   size   is   %d\n\r ",bind.nFileSizeBind);
        if((fpreadHost=fopen(strFilenameHost, "rb "))==NULL)
{
printf( "read   host   file   fail! ");
return   0;
}

if((fpreadClient=fopen(strFilenameClient, "rb "))==NULL)
{
printf( "read   client   file   fail ");
return   0;
}

if((fpreadUnbind=fopen(strFilenameUnbind, "rb "))==NULL)
{
printf( "read   unbind   file   fail ");


return   0;
}

if((fpwriteTem=fopen(strFileTemname, "wb "))==NULL)
{
printf( "write   tem   file   fail ");
return   0;
}

fseek(fpreadHost,0,SEEK_SET);
fseek(fpreadClient,0,SEEK_SET);
fseek(fpreadUnbind,0,SEEK_SET);
fseek(fpwriteTem,0,SEEK_SET);

int   c;//nCount;
while((c=fgetc(fpreadUnbind))!=EOF)
{

fputc(c,fpwriteTem);
}

while((c=fgetc(fpreadHost))!=EOF)
{
//c=fgetc(fpreadHost);
fputc(c,fpwriteTem);
}
while((c=fgetc(fpreadClient))!=EOF)
{
//c=fgetc(fpreadClient);
fputc(c,fpwriteTem);
}
//while((c=fgetc(fpreadClient))!=EOF)
//{

//fputc(c,fpwriteTem);
//}
fwrite(&bind,1,sizeof(bind),fpwriteTem);
fclose(fpreadUnbind);
fclose(fpreadHost);
fclose(fpreadClient);
fclose(fpwriteTem);
_stat(strFileTemname,&ST);
printf( "the   size   of   bind 's   file   is   %d ",ST.st_size);
return   0;

//读文件信息,三个文件信息,
//建立一个临时文件,往这个临时文件里增加unbind.exe,host.exe,client.exe,还有三个文件信息
}


//   unbind.cpp   :   定义控制台应用程序的入口点。
//

#include   "stdafx.h "

#include   "stdio.h "
#include   <sys/stat.h>
#include   <windows.h>
#include   "atlstr.h "
//#define   MAX_PATH   40
typedef   struct   tagMyBINDSTRUCT
{
TCHAR   cFilenameHost[40];         //   宿主文件名.
TCHAR   cFilenameClient[40];     //   被捆绑文件名.
TCHAR   cFilenameUnbind[40];//解压缩程序名
DWORD   nFileSizeUnbind;//   解压缩程序大小
        DWORD   nFileSizeHost;                 //   宿主文件长.
        DWORD   nFileSizeClient;             //   被捆绑文件长.
        DWORD   nFileSizeBind;                 //   已捆绑文件长.
}   MYBINDINFO;
int   main(int   argc,   char*   argv[])
{
CString   strFilenameTem=argv[0];
MYBINDINFO   bind;
//char   szModule[MAX_PATH];
memset(&bind,0,sizeof(MYBINDINFO));
//if(GetModuleFileName(0,szModule,MAX_PATH)==0)
//{
//printf( "get   module   file   name   error\n\r ");
//return   0;
//}
//printf( "the   path   is   %s ",szModule);
struct   _stat   ST;
if(0!=_stat(strFilenameTem,&ST))
{
printf( "there   is   error ");
return   0;
}
DWORD   nFilesize=ST.st_size;
printf( "the   size   of   the   file   is   %d ",nFilesize);
DWORD   offset=nFilesize-sizeof(MYBINDINFO);
if(offset <0)   return   0;
FILE   *fpreadbind,*fpwrite;
if((fpreadbind=fopen(strFilenameTem, "rb "))==NULL)
{
printf( "open   the   self   fail\n\r ");
return   0;
}
fseek(fpreadbind,offset,SEEK_SET);
fread(&bind,1,sizeof(bind),fpreadbind);
if(bind.nFileSizeBind!=nFilesize)
{
fclose(fpreadbind);
printf( "has 't   bind ");
return   0;
}
DWORD   nPos=bind.nFileSizeUnbind;
CString   srFilenameHost=bind.cFilenameHost;


printf( "the   host   file   is   %s\n\r ",srFilenameHost);
printf( "the   size   of   file   is   %d   \n\r ",bind.nFileSizeHost);
//fseek(fpwrite,0,SEEK_SET);

fseek(fpreadbind,nPos,SEEK_SET);
if((fpwrite=fopen(srFilenameHost, "wb "))==NULL)
{
fclose(fpreadbind);
printf( "create   the   host   file   fail ");
return   0;
}
fseek(fpwrite,0,SEEK_SET);
int   c;
DWORD   nCount=0;
while(1)
{
c=fgetc(fpreadbind);
fputc(c,fpwrite);
nCount++;
if(nCount==bind.nFileSizeHost)   break;

}
//printf( "the   size   of   host   file   is   %d ",nCount);
int   address;
if(0!=(address=ferror(fpreadbind)))
{
printf( "there   is   error   when   create   host   file   the   address   is   %0x ",address);
return   0;
}

printf( "cover   the   host   file   sucess!\n\r ");
if(0!=_stat(srFilenameHost,&ST))
{
printf( "there   is   a   error   about   host   file ");
return   0;
}

printf( "the   size   of   host   is   %d ",ST.st_size);

//nPos+=bind.nFileSizeHost;
//fseek(fpreadbind,nPos,SEEK_SET);
CString   srFilenameCilent=bind.cFilenameClient;
printf( "the   client   file   is   %s\n\r ",srFilenameCilent);
printf( "the   size   of   file   is   %d   \n\r ",bind.nFileSizeClient);

if((fpwrite=fopen(srFilenameCilent, "wb "))=NULL)
{
fclose(fpreadbind);
printf( "create   the   client   file   fail ");
return   0;
}
fseek(fpwrite,0,SEEK_SET);

while((c=fgetc(fpwrite))!=EOF)
{

fputc(c,fpwrite);
nCount++;
if(nCount==bind.nFileSizeClient)   break;


}
printf( "cover   the   client   file   sucess!\n\r ");
return   0;
}



[解决办法]
// unbind.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h "

#include "stdio.h "
#include "stdlib.h "
#include <sys/stat.h>
#include <windows.h>
#include "atlstr.h "
//#define MAX_PATH 40
typedef struct tagMyBINDSTRUCT
{
TCHAR cFilenameHost[40]; // 宿主文件名.
TCHAR cFilenameClient[40]; // 被捆绑文件名.
TCHAR cFilenameUnbind[40];//解压缩程序名
DWORD nFileSizeUnbind;// 解压缩程序大小
DWORD nFileSizeHost; // 宿主文件长.
DWORD nFileSizeClient; // 被捆绑文件长.
DWORD nFileSizeBind; // 已捆绑文件长.
} MYBINDINFO;
int main(int argc, char* argv[])
{
CString strFilenameTem= "tempfile.exe ";//只用语测试目的
MYBINDINFO bind;
//char szModule[MAX_PATH];
memset(&bind,0,sizeof(MYBINDINFO));
//if(GetModuleFileName(0,szModule,MAX_PATH)==0)
//{
//printf( "get module file name error\n\r ");
//return 0;
//}
//printf( "the path is %s ",szModule);
struct _stat ST;
if(0!=_stat(strFilenameTem,&ST))
{
printf( "there is error ");
return 0;
}
DWORD nFilesize=ST.st_size;


printf( "the size of the file is %d\n\r ",nFilesize);//
DWORD offset=nFilesize-sizeof(MYBINDINFO);
if(offset <0) return 0;
FILE *fpreadbind,*fpwrite,*fpwrite2;
if((fpreadbind=fopen(strFilenameTem, "rb "))==NULL)
{
printf( "open the self fail\n\r ");
return 0;
}
fseek(fpreadbind,offset,SEEK_SET);
fread(&bind,1,sizeof(bind),fpreadbind);
if(bind.nFileSizeBind!=nFilesize)
{
fclose(fpreadbind);
printf( "has 't bind ");
return 0;
}
DWORD nPos=bind.nFileSizeUnbind;
CString srFilenameHost=bind.cFilenameHost;
printf( "the host file is %s\n\r ",srFilenameHost);
printf( "the size of file is %d \n\r ",bind.nFileSizeHost);
//fseek(fpwrite,0,SEEK_SET);

fseek(fpreadbind,nPos,SEEK_SET);
if((fpwrite=fopen(srFilenameHost, "wb "))==NULL)
{
fclose(fpreadbind);
printf( "create the host file fail ");
return 0;
}
fseek(fpwrite,0,SEEK_SET);
//int c;
//DWORD nCount=0;
byte* buf;
DWORD bufsize;
if(bind.nFileSizeHost> bind.nFileSizeClient)
bufsize=bind.nFileSizeHost;
else
bufsize=bind.nFileSizeClient;

buf=(BYTE*)malloc(bufsize);
int byteread,bytewrite=0;
while(byteread=fread(buf,1,bind.nFileSizeHost,fpreadbind))//这里是整个程序最重要的地方,我的理解是,下面的代//码主要是为里处理碰到1a字符的处理
{
if(bytewrite+byteread> bind.nFileSizeHost)//从新的指针开始的时候,是不是还是读bind.nfilesizehost大小的//数据。
byteread=bind.nFileSizeHost-bytewrite;
bytewrite+=fwrite(buf,1,byteread,fpwrite);
}
//if(0!=_stat(srFilenameHost,&ST))
//{
//printf( "there is a error about host file ");
//return 0;
//}

//printf( "the size of host is %d\n\r ",ST.st_size);
printf( "the size of host is %d\n\r ",bind.nFileSizeHost);
fclose(fpwrite);


//while(1)
//{
//c=fgetc(fpreadbind);
//fputc(c,fpwrite);
//nCount++;
//if(nCount==bind.nFileSizeHost) break;

//}
//printf( "the size of host file is %d ",nCount);
//int address;
//if(0!=(address=ferror(fpreadbind)))
//{
//printf( "there is error when create host file the address is
%0x ",address);
//return 0;
//}

//printf( "cover the host file sucess!\n\r ");


nPos+=bind.nFileSizeHost;
fseek(fpreadbind,nPos,SEEK_SET);
CString srFilenameCilent=bind.cFilenameClient;
printf( "the client file is %s\n\r ",srFilenameCilent);
//printf( "the size of file is %d \n\r ",bind.nFileSizeClient);
printf( "open the file!\n\r ");

if((fpwrite2=fopen(srFilenameCilent, "wb "))==NULL)//在原来的程序中,我只写了一个=
{
fclose(fpreadbind);
printf( "create the client file fail ");
return 0;
}
if(0!=fseek(fpwrite2,0,SEEK_SET))
{
printf( "seek the address fail ");
return 0;
}
bytewrite=0;
printf( "begin write client file\n\r ");
while(byteread=fread(buf,1,bind.nFileSizeClient,fpreadbind))
{
if(bytewrite+byteread> bind.nFileSizeClient)//从新的指针开始的时候,是不是还是读bind.nfilesizehost大小//的数据。
byteread=bind.nFileSizeClient-bytewrite;
bytewrite+=fwrite(buf,1,byteread,fpwrite2);
}
fclose(fpwrite2);
fclose(fpreadbind);
free(buf);
if(0!=_stat(srFilenameCilent,&ST))
{
printf( "there is a error about client file ");
return 0;


}

printf( "the size of client is %d\n\r ",ST.st_size);
printf( "the size of client is %d\n\r ",bind.nFileSizeClient);
//while((c=fgetc(fpwrite))!=EOF)
//{

//fputc(c,fpwrite);
//nCount++;
//if(nCount==bind.nFileSizeClient) break;


//}
printf( "cover the client file sucess!\n\r ");
return 0;
}



[解决办法]
合并原理,exebind 需要捆绑三个软件,unbind+程序1+程序2+被捆绑文件信息.
unbind的作用是运行后会重新把程序1,程序2重新读取出来.关于eof标志,以及fget反回值的类型是int ,而不是char,大家可以用google搜索eof,能找到相应答案.
碰到最主要的问题是对这些函数结束符的处理,但是有个问题我还没想通,就是执行合并的时候不会出现问题.还有fread的指针是怎么移动的,也可以通过google搜索下.
内事不绝问老婆,外事不绝问google
注名:后来改用fwrite/fread函数是在老迈的指点下修改的,那段循环读写也是从网上得到的.

热点排行