WaitForSingleObject(hProcess,INFINITE);
DeleteFile(FileName);
+++++++++++++++++++++++++++++++++
第一次调用 Create_Process后主进程便被阻塞,只能等第一个被创建的进程结束并删除第一个文件后,才会第二次调用 Create_Process
DeleteFile(FileName);
+++++++++++++++++++++++++++++++++
第一次调用 Create_Process后主进程便被阻塞,只能等第一个被创建的进程结束并删除第一个文件后,才会第二次调用 Create_Process
#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;HANDLE Create_Process(LPCTSTR FileName)
{PROCESS_INFORMATION PI;
STARTUPINFO SI;
BOOL bRet;
memset(&SI,0,sizeof(SI));
SI.cb=sizeof(SI);
bRet=CreateProcess(FileName,NULL,NULL,NULL,FALSE,NORMAL_PRIORITY_CLASS,NULL,NULL,&SI,&PI);
if(!bRet) return 0;
return PI.hProcess;}
int main(int argc, char* argv[])
{
CString strFilenameTem="tempfile.exe";
MYBINDINFO bind;
//char szModule[MAX_PATH];
memset(&bind,0,sizeof(MYBINDINFO));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(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);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))
{
if(bytewrite+byteread>bind.nFileSizeHost)
byteread=bind.nFileSizeHost-bytewrite;
bytewrite+=fwrite(buf,1,byteread,fpwrite);
}printf("the size of host is %d\n\r",bind.nFileSizeHost);
fclose(fpwrite);
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)
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);printf("cover the client file sucess!\n\r");
HANDLE handle1,handle2;
handle1=Create_Process(srFilenameHost);
handle2=Create_Process(srFilenameCilent);
if(handle1)
{
WaitForSingleObject(handle1,INFINITE);
DeleteFile(srFilenameHost);
}if(handle2)
{
WaitForSingleObject(handle2,INFINITE);
DeleteFile(srFilenameCilent);
}
return 0;