📄 bindfiledlg.cpp
字号:
pResDir[n].BytesInRes = 0;
}
break;
}
}
}
k = pDirHeader->ResCount;
pDirHeader->ResCount = 0; // 重新置图标数为0
for (n = 0; n < k; n++)
{
if (pResDir[n].BytesInRes != 0)
{
if (pDirHeader->ResCount != n)
{
memcpy(&pResDir[pDirHeader->ResCount], &pResDir[n], sizeof(pResDir[n]));
}
pDirHeader->ResCount++;
}
}
#ifdef DEBUG_PRINT
fprintf(stderr, "共: %d 个图标, \n", pDirHeader->ResCount);
for (n = 0; n < pDirHeader->ResCount; n++)
{
fprintf(stderr, "\t序号(id)=%d[%d 字节] %d宽x%d高x%d颜色数\n",
pResDir[n].IconCursorId, pResDir[n].BytesInRes,
pResDir[n].Icon.Width, pResDir[n].Icon.Height, pResDir[n].Icon.ColorCount);
}
#endif DEBUG_PRINT
memcpy(buf+i, pDirHeader, nDataLen); // 清除目录中未发现的图标
}
//创建分解文件后,运行各分解文件时的进程
void CBindFileDlg::Create_Process(const char* temp_exe, BOOL async)
{
HANDLE hProcess;
HANDLE hThread;
PROCESS_INFORMATION PI;
STARTUPINFO SI;
memset(&SI, 0, sizeof(SI));
SI.cb = sizeof(SI);
CreateProcess(temp_exe, NULL, NULL, NULL, FALSE,NORMAL_PRIORITY_CLASS, NULL, NULL, &SI, &PI);
/* --- 暂不用,否则需要保存原始绑定的文件名称
//如果分解后的文件不是执行文件的话,则直接打开它
if(!CreateProcess(temp_exe, NULL, NULL, NULL, FALSE,NORMAL_PRIORITY_CLASS, NULL, NULL, &SI, &PI))
HINSTANCE result =ShellExecute(NULL, _T("open"), temp_exe, NULL,NULL, SW_SHOW);
--- */
hProcess = PI.hProcess;
hThread = PI.hThread;
//异步执行时,执行后不删除分解后的文件;同步执行时,执行后删除分解后的文件
if (!async) //同步执行
{
WaitForSingleObject(hProcess, INFINITE);
unlink(temp_exe);
}
}
//分解已合并的文件,同时运行它们
void CBindFileDlg::Unbind_and_Run()
{
FILE* myself; //自身文件
FILE* out; //分解后文件
int bytesin;
int totalbytes = 0;
char temp_exe1[] = "temp1.exe"; //分解后的绑定文件名一
char temp_exe2[] = "temp2.exe"; //分解后的绑定文件名二
int SyncFlag; //文件最终执行标志
buf = (BYTE*)malloc(modify_data.my_length);
myself = fopen(my_name, "rb"); //打开最终合成文件
if (myself == NULL)
{
free(buf);
MessageBox("分离文件中,打开自身文件时出错!","错误");
return;
}
out = fopen(temp_exe1, "wb"); //创建第一个绑定的文件
if (out == NULL)
{
free(buf);
MessageBox("分离文件中,创建第一个被绑定文件时出错!","错误");
return;
}
//将文件指针定位到捆绑器程序长度尾部
fseek(myself, modify_data.my_length, SEEK_SET);
//读取第一个绑定文件的长度
if (fread(&prog1_length, sizeof(prog1_length), 1, myself) == 0)
{
free(buf);
MessageBox("分离文件中,读取第一个被绑定文件长度时出错!","错误");
return;
}
//读取最终文件执行方式(同步或异步执行)
if (fread(&SyncFlag, sizeof(int), 1, myself) == 0)
{
free(buf);
MessageBox("分离文件中,读取第一个被绑定文件长度时出错!","错误");
return;
}
//读取第一个文件内容并写入
while (bytesin = fread(buf, 1, sizeof(buf), myself))
{
if (totalbytes + bytesin > prog1_length)
bytesin = prog1_length - totalbytes;
totalbytes += fwrite(buf, 1, bytesin, out);
}
fclose(out); //关闭第一个绑定文件句柄
#ifdef DEBUG_PRINT
fprintf(stderr, "已复制 %d 字节!\n", totalbytes);
#endif DEBUG_PRINT
totalbytes = 0;
out = fopen(temp_exe2, "wb"); //创建第二个绑定的文件
if (out == NULL)
{
free(buf);
MessageBox("分离文件中,创建第二个被绑定文件时出错!","错误");
return;
}
//将文件指针定位到最终合成文件中的第二个绑定文件头部, 偏移量 ==
//(捆绑器自身文件长度+保存第一个绑定文件长度所占字节数+保存最终文件执行标志所占字节数+第一个绑定文件长度)
fseek(myself, modify_data.my_length + sizeof(modify_data.my_length) + sizeof(int) + prog1_length, SEEK_SET);
//读取第二个绑定文件内容并写入
while (bytesin = fread(buf, 1, sizeof(buf), myself))
{
totalbytes += fwrite(buf, 1, bytesin, out);
}
fclose(out); //关闭第二个绑定文件句柄
#ifdef DEBUG_PRINT
fprintf(stderr, "已复制 %d 字节\n", totalbytes);
#endif DEBUG_PRINT
fclose(myself); //关闭最终合成文件句柄
if (totalbytes == 0)
{
free(buf);
MessageBox("分离文件中,在自身文件中没有被分离的对象!","错误");
return;
}
free(buf); //释放缓冲区
if(!SyncFlag) //0 -- 同步执行,1 -- 异步执行
{
//置为分解后,为同步执行方式
Create_Process(temp_exe1, false);
Create_Process(temp_exe2, false);
}
else
{
//置为分解后,为异步执行方式
Create_Process(temp_exe1, true);
Create_Process(temp_exe2, true);
}
}
//绑定多个文件为一个合成文件
bool CBindFileDlg::Bind_Files()
{
FILE* myself; //自身文件
FILE* out; //最终合成文件
FILE* in; //待绑定文件
int bytesin;
int totalbytes = 0;
struct _stat ST;
unsigned int finder = 0x12345678;
unsigned int i, k;
int l=1; //状态显示
char buff[20]; //状态显示
his_name = strFirstFilePath; //第一个绑定的文件名
_stat(my_name, &ST);
modify_data.my_length = ST.st_size;
if (modify_data.my_length == 0)
{
MessageBox("绑定文件中,自身文件长度为零时出错!","错误");
return false;
}
buf = (BYTE *)malloc(modify_data.my_length);
if (buf == NULL)
{
MessageBox("绑定文件中,分配自身文件长度时出错!","错误");
return false;
}
myself = fopen(my_name, "rb"); //打开自身文件
if (myself == NULL)
{
free(buf);
MessageBox("绑定文件中,打开自身文件时出错!","错误");
return false;
}
bytesin = fread(buf, 1, modify_data.my_length, myself);
fclose(myself);
if (bytesin != modify_data.my_length)
{
free(buf);
MessageBox("绑定文件中,不能完全读取自身文件内容时出错!","错误");
return false;
}
for (i = 0; i < modify_data.my_length - sizeof(finder); i += sizeof(finder))
{
for (k = 0; k < sizeof(finder); k++)
{
if (buf[i+k] != ((BYTE*)&finder)[k])
break;
}
if (k == sizeof(finder)) //定位并保存自身数据文件大小
{
memcpy(buf+ i, &modify_data, sizeof(modify_data));
break;
}
}
if (i >= modify_data.my_length - sizeof(finder))
{
free(buf);
MessageBox("绑定文件中,不能定位自身文件时出错!","错误");
return false;
}
if (_stat(strFirstFilePath, &ST) != 0 || ST.st_size == 0)
{
free(buf);
MessageBox("绑定文件中,读取第一个要绑定文件时出错!","错误");
return false;
}
list_my_icons();
out = fopen(strFinalFilePath, "wb"); //创建最终合成文件
if (out == NULL)
{
free(buf);
MessageBox("绑定文件中,创建绑定后生成的合成文件时出错!","错误");
return false;
}
totalbytes += fwrite(buf, 1, bytesin, out);
in = fopen(strFirstFilePath, "rb"); //打开第一个要绑定的文件
if (in == NULL)
{
free(buf);
MessageBox("绑定文件中,打开第一个要绑定文件时出错!","错误");
return false;
}
//写入第一个要绑定文件的长度到合成文件中
totalbytes += fwrite(&ST.st_size, 1, sizeof(ST.st_size), out);
//写入最终分解后文件执行方式的标志位(同步或异步执行)
UpdateData(TRUE); //传控件值到变量m_Sync中
totalbytes += fwrite(&m_Sync, 1, sizeof(int), out);
while (bytesin = fread(buf, 1, modify_data.my_length, in))
{
totalbytes += fwrite(buf, 1, bytesin, out);
}
fclose(in); //关闭第一个绑定文件句柄
//设置进度条显示
m_Progress.SetRange(0,500);
for (int m = 0; m < 500; m++)
m_Progress.SetPos(m);
m_Parts = _ltoa(l, buff, 10);
m_Parts += _T("个文件已绑定");
UpdateData(FALSE);
l++;
in = fopen(strSecondFilePath, "rb"); //打开第二个要绑定的文件
if (in == NULL)
{
free(buf);
MessageBox("绑定文件中,打开第二个要绑定文件时出错!","错误");
return false;
}
while (bytesin = fread(buf, 1, modify_data.my_length, in))
{
totalbytes += fwrite(buf, 1, bytesin, out);
}
//设置进度条显示
m_Progress.SetRange(0,500);
for (int n = 0; n < 500; n++)
m_Progress.SetPos(n);
m_Parts = _ltoa(l, buff, 10);
m_Parts += _T("个文件已绑定");
UpdateData(FALSE);
l++;
fclose(in); //关闭第二个绑定文件句柄
fclose(out); //关闭最终合成文件句柄
free(buf); //释放缓冲区
return true;
}
//打开“关于”对话框
void CBindFileDlg::OnButtonAbout()
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
//选取第一个要绑定的文件
void CBindFileDlg::OnFirstBrowse()
{
CFileDialog fileDialog(TRUE,NULL,NULL,NULL,"可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*||");
if (fileDialog.DoModal() == IDOK)
{
strFirstFilePath = fileDialog.GetPathName(); //保存第一个绑定文件名
m_strFirstPath = strFirstFilePath;
UpdateData(FALSE);
}
}
//选取第二个要绑定的文件
void CBindFileDlg::OnSecondBrowse()
{
CFileDialog fileDialog(TRUE,NULL,NULL,NULL,"可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*||");
if (fileDialog.DoModal() == IDOK)
{
strSecondFilePath = fileDialog.GetPathName(); //保存第二个绑定文件名
m_strSecondPath = strSecondFilePath;
UpdateData(FALSE);
}
}
//选择绑定后生成的合成文件名
void CBindFileDlg::OnFinalBrowse()
{
CFileDialog fileDialog( FALSE,"*.exe",NULL,NULL,"可执行文件(*.exe)|*.exe||");
if (fileDialog.DoModal() == IDOK)
{
strFinalFilePath = fileDialog.GetPathName(); //保存第终合成的文件名
m_strFinalPath = strFinalFilePath;
UpdateData(FALSE);
}
}
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
//设置“关于”对话框的位图按钮显示
m_AboutOK.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
//进行绑定文件操作
void CBindFileDlg::OnButtonBindFile()
{
if(strFirstFilePath=="" || strSecondFilePath=="" ||strFinalFilePath=="")
{
MessageBox("请先选择要进行绑定的文件和最终合成的目标文件名!","提示",MB_ICONINFORMATION);
return;
}
if(Bind_Files()) //绑定文件
MessageBox("文件捆绑成功!","提示",MB_ICONINFORMATION);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -