EXE捆绑器

 释放的方法真没想到  太神奇了

  1 // 文件捆绑器Dlg.cpp : 实现文件
  2 //
  3 
  4 #include "stdafx.h"
  5 #include "文件捆绑器.h"
  6 #include "文件捆绑器Dlg.h"
  7 #include "afxdialogex.h"
  8 
  9 #ifdef _DEBUG
 10 #define new DEBUG_NEW
 11 #endif
 12 
 13 
 14 struct MODIFY_DATA {
 15     unsigned int finder; // 常量(定位自身)
 16     _off_t my_length;      //文件长度(自身)
 17 } modify_data = { 0x12345678, 0 };
 18 //枚举所有图标资源
 19 BOOL CALLBACK my_enum_res_callback(
 20     HMODULE hExe,   // 资源句柄
 21     LPCTSTR lpszType,  // 资源类型
 22     LPTSTR lpszName,   // 资源名称
 23     LPARAM lParam    // 自定义消息参数
 24 );
 25 
 26 // 用于应用程序“关于”菜单项的 CAboutDlg 对话框
 27 
 28 class CAboutDlg : public CDialogEx
 29 {
 30 public:
 31     CAboutDlg();
 32 
 33 // 对话框数据
 34 #ifdef AFX_DESIGN_TIME
 35     enum { IDD = IDD_ABOUTBOX };
 36 #endif
 37 
 38     protected:
 39     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV 支持
 40 
 41 // 实现
 42 protected:
 43     DECLARE_MESSAGE_MAP()
 44 };
 45 
 46 CAboutDlg::CAboutDlg() : CDialogEx(IDD_ABOUTBOX)
 47 {
 48 }
 49 
 50 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
 51 {
 52     CDialogEx::DoDataExchange(pDX);
 53 }
 54 
 55 BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
 56 END_MESSAGE_MAP()
 57 
 58 
 59 // C文件捆绑器Dlg 对话框
 60 
 61 
 62 
 63 C文件捆绑器Dlg::C文件捆绑器Dlg(CWnd* pParent /*=NULL*/)
 64     : CDialogEx(IDD_MY_DIALOG, pParent)
 65     , m_DestPathName(_T(""))
 66 {
 67     //m_DestPathName = _T("");
 68     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
 69 }
 70 
 71 void C文件捆绑器Dlg::DoDataExchange(CDataExchange* pDX)
 72 {
 73     CDialogEx::DoDataExchange(pDX);
 74     DDX_Control(pDX, IDC_LIST_CONTROL, m_List);
 75     DDX_Text(pDX, IDC_EDIT1, m_DestPathName);
 76 }
 77 
 78 BEGIN_MESSAGE_MAP(C文件捆绑器Dlg, CDialogEx)
 79     ON_WM_SYSCOMMAND()
 80     ON_WM_PAINT()
 81     ON_WM_QUERYDRAGICON()
 82     ON_BN_CLICKED(IDOK, &C文件捆绑器Dlg::OnBnClickedOk)
 83     ON_BN_CLICKED(IDC_BUTTON_ADD, &C文件捆绑器Dlg::OnBnClickedButtonAdd)
 84     ON_BN_CLICKED(IDC_BUTTON_DELETE, &C文件捆绑器Dlg::OnBnClickedButtonDelete)
 85     ON_BN_CLICKED(IDC_BUTTON_SELECT, &C文件捆绑器Dlg::OnBnClickedButtonSelect)
 86     ON_BN_CLICKED(IDC_BUTTON2, &C文件捆绑器Dlg::OnBnClickedButton2)
 87     ON_BN_CLICKED(IDC_BUTTON3, &C文件捆绑器Dlg::OnBnClickedButton3)
 88 END_MESSAGE_MAP()
 89 
 90 
 91 // C文件捆绑器Dlg 消息处理程序
 92 
 93 BOOL C文件捆绑器Dlg::OnInitDialog()
 94 {
 95     CDialogEx::OnInitDialog();
 96 
 97     // 将“关于...”菜单项添加到系统菜单中。
 98 
 99     // IDM_ABOUTBOX 必须在系统命令范围内。
100     ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
101     ASSERT(IDM_ABOUTBOX < 0xF000);
102 
103     CMenu* pSysMenu = GetSystemMenu(FALSE);
104     if (pSysMenu != NULL)
105     {
106         BOOL bNameValid;
107         CString strAboutMenu;
108         bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
109         ASSERT(bNameValid);
110         if (!strAboutMenu.IsEmpty())
111         {
112             pSysMenu->AppendMenu(MF_SEPARATOR);
113             pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
114         }
115     }
116 
117     // 设置此对话框的图标。  当应用程序主窗口不是对话框时,框架将自动
118     //  执行此操作
119     SetIcon(m_hIcon, TRUE);            // 设置大图标
120     SetIcon(m_hIcon, FALSE);        // 设置小图标
121 
122     // TODO: 在此添加额外的初始化代码
123 
124     ListView_SetExtendedListViewStyle(m_List.m_hWnd, LVS_EX_GRIDLINES | LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
125 
126     m_List.InsertColumn(0, "选择", LVCFMT_LEFT, 50);
127     m_List.InsertColumn(1, "要捆绑的文件", LVCFMT_LEFT, 280);
128 
129 
130     //初始化变量
131     prog1_length = 0;
132     buf = NULL;
133     his_name = "";
134 
135 
136     //获取自身文件名
137     ::GetModuleFileName(0, my_name, sizeof(my_name));
138 
139     struct _stat ST;
140     _stat(my_name, &ST);
141     //此外加入捆绑器程序的最终大小,来判断是绑定文件还是分解执行文件
142     if (ST.st_size > 264256)
143     {
144         OnBnClickedButton2(); //分离文件并运行
145         exit(0);    //直接退出程序,不显示捆绑程序画面
146     }
147 
148     return TRUE;  // 除非将焦点设置到控件,否则返回 TRUE
149 }
150 
151 
152  
153 void C文件捆绑器Dlg::OnSysCommand(UINT nID, LPARAM lParam)
154 {
155     if ((nID & 0xFFF0) == IDM_ABOUTBOX)
156     {
157         CAboutDlg dlgAbout;
158         dlgAbout.DoModal();
159     }
160     else
161     {
162         CDialogEx::OnSysCommand(nID, lParam);
163     }
164 }
165 
166 // 如果向对话框添加最小化按钮,则需要下面的代码
167 //  来绘制该图标。  对于使用文档/视图模型的 MFC 应用程序,
168 //  这将由框架自动完成。
169 
170 void C文件捆绑器Dlg::OnPaint()
171 {
172     if (IsIconic())
173     {
174         CPaintDC dc(this); // 用于绘制的设备上下文
175 
176         SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
177 
178         // 使图标在工作区矩形中居中
179         int cxIcon = GetSystemMetrics(SM_CXICON);
180         int cyIcon = GetSystemMetrics(SM_CYICON);
181         CRect rect;
182         GetClientRect(&rect);
183         int x = (rect.Width() - cxIcon + 1) / 2;
184         int y = (rect.Height() - cyIcon + 1) / 2;
185 
186         // 绘制图标
187         dc.DrawIcon(x, y, m_hIcon);
188     }
189     else
190     {
191         CDialogEx::OnPaint();
192     }
193 }
194 
195 //当用户拖动最小化窗口时系统调用此函数取得光标
196 //显示。
197 HCURSOR C文件捆绑器Dlg::OnQueryDragIcon()
198 {
199     return static_cast<HCURSOR>(m_hIcon);
200 }
201 
202 
203 
204 void C文件捆绑器Dlg::OnBnClickedOk()
205 {
206     // TODO: 在此添加控件通知处理程序代码
207     FILE* myself;   //自身文件
208     FILE* out;      //最终合成文件
209     FILE* in;       //待绑定文件
210     int bytesin;
211     int totalbytes = 0;
212     struct _stat ST;
213     unsigned int finder = 0x12345678;
214     unsigned int i, k;
215     GetDlgItemText(IDC_EDIT1, m_DestPathName);
216      
217     int n = m_List.GetItemCount();
218     if (n > 0)
219     {
220         CString strIconPathName;
221         GetDlgItemText(IDC_EDIT2, strIconPathName);
222 
223         strIconPathName.TrimLeft();
224         if (strIconPathName.IsEmpty())
225         {
226             his_name = m_List.GetItemText(0, 1); //第一个绑定的文件名
227         }
228         else
229         {
230             his_name = strIconPathName;
231         }
232         //AfxMessageBox(his_name);
233     }
234     else
235     {
236         MessageBox("请先添加文件!", "提示", MB_ICONINFORMATION);
237         return ;
238     }
239 
240     _stat(my_name, &ST);
241     modify_data.my_length = ST.st_size;
242     if (modify_data.my_length == 0)
243     {
244         MessageBox("绑定文件中,自身文件长度为零时出错!", "错误");
245         return ;
246     }
247 
248     buf = (BYTE *)malloc(modify_data.my_length);
249     if (buf == NULL)
250     {
251         MessageBox("绑定文件中,分配自身文件长度时出错!", "错误");
252         return ;
253     }
254 
255     myself = fopen(my_name, "rb");  //打开自身文件
256     if (myself == NULL)
257     {
258         free(buf);
259         MessageBox("绑定文件中,打开自身文件时出错!", "错误");
260         return ;
261     }
262 
263     bytesin = fread(buf, 1, modify_data.my_length, myself);
264     fclose(myself);
265 
266     if (bytesin != modify_data.my_length)
267     {
268         free(buf);
269         MessageBox("绑定文件中,不能完全读取自身文件内容时出错!", "错误");
270         return ;
271     }
272 
273      for (i = 0; i < modify_data.my_length - sizeof(finder); i += sizeof(finder))
274     {
275         for (k = 0; k < sizeof(finder); k++)
276         {
277             if (buf[i + k] != ((BYTE*)&finder)[k])
278                 break;
279         }
280         if (k == sizeof(finder))   //定位并保存自身数据文件大小
281         {
282             memcpy(buf + i, &modify_data, sizeof(modify_data));
283             break;
284         }
285     }
286 
287     if (i >= modify_data.my_length - sizeof(finder))
288     {
289         free(buf);
290         MessageBox("绑定文件中,不能定位自身文件时出错!", "错误");
291         return ;
292     }
293 
294     //list_my_icons();
295 
296     out = fopen(m_DestPathName, "wb"); //创建最终合成文件
297     if (out == NULL)
298     {
299         free(buf);
300         MessageBox("绑定文件中,创建绑定后生成的合成文件时出错!", "错误");
301         return ;
302     }
303 
304 
305 
306     for (int j = 0; j < n; j++)
307     {
308         CString strFilePath = m_List.GetItemText(j, 1);
309 
310         if (_stat(strFilePath, &ST) != 0 || ST.st_size == 0)
311         {
312             free(buf);
313             MessageBox("绑定文件中,读取要绑定的文件时出错!", "错误");
314             return ;
315         }
316 
317         totalbytes += fwrite(buf, 1, bytesin, out);
318 
319 
320         in = fopen(strFilePath, "rb");  //打开要绑定的文件
321         if (in == NULL)
322         {
323             free(buf);
324             MessageBox("绑定文件中,打开要绑定的文件时出错!", "错误");
325             return ;
326         }
327 
328 
329         //写入要绑定文件的长度到合成文件中
330         totalbytes += fwrite(&ST.st_size, 1, sizeof(ST.st_size), out);
331 
332 
333         while (bytesin = fread(buf, 1, modify_data.my_length, in))
334         {
335             totalbytes += fwrite(buf, 1, bytesin, out);
336         }
337         fclose(in); //关闭绑定文件句柄         
338     }
339 
340 
341     fclose(out); //关闭最终合成文件句柄
342     free(buf);   //释放缓冲区
343 
344     MessageBox("捆绑成功!", "提示", MB_ICONINFORMATION);
345 
346      
347     CDialogEx::OnOK();
348 }
349 
350 
351 void C文件捆绑器Dlg::OnBnClickedButtonAdd()
352 {
353     // TODO: 在此添加控件通知处理程序代码
354     CString strPathName;
355     CFileDialog fileDialog(TRUE, NULL, NULL, NULL, "可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*||");
356 
357     if (fileDialog.DoModal() == IDOK)
358     {
359         strPathName = fileDialog.GetPathName();
360     }
361     else
362     {
363         return;
364     }
365 
366 
367 
368     int n = m_List.GetItemCount();
369 
370     m_List.InsertItem(n, "");
371 
372     m_List.SetItemText(n, 1, strPathName);
373 }
374 
375 
376 void C文件捆绑器Dlg::OnBnClickedButtonDelete()
377 {
378     // TODO: 在此添加控件通知处理程序代码
379     int n = m_List.GetItemCount();
380 
381     for (int i = 0; i < n; i++)
382     {
383         int bFlag = m_List.GetCheck(i);
384         if (bFlag)
385         {
386             CString str = m_List.GetItemText(i, 1);
387             //AfxMessageBox(str);
388             m_List.DeleteItem(i);
389 
390             i = i - 1;
391             n = n - 1;
392         }
393     }
394 }
395 
396 
397 //列出所有图标
398 void C文件捆绑器Dlg::list_my_icons()
399 {
400     HRSRC hRsrc;
401     const HMODULE hExe = 0; // 为0时,表示为自身模块
402     HGLOBAL hMem;
403     DWORD nDataLen = 0;
404     NEWHEADER* pDirHeader;
405     RESDIR* pResDir;
406     unsigned int i, k, n;
407 
408     //载入自身模块,找出其中的主程序图标为默认合成文件图标(如果第一个要绑定文件无图标的话)
409     hRsrc = FindResource(hExe, MAKEINTRESOURCE(128), RT_GROUP_ICON);
410     hMem = LoadResource(hExe, hRsrc);
411 
412     nDataLen = SizeofResource(hExe, hRsrc);
413     pDirHeader = (NEWHEADER*)LockResource(hMem);
414     pResDir = (RESDIR*)(pDirHeader + 1);
415 
416     for (i = 0; i < modify_data.my_length - nDataLen; i++)
417     {
418         for (k = 0; k < nDataLen; k++)
419         {
420             if (buf[i + k] != ((BYTE*)pDirHeader)[k])
421                 break;
422         }
423 
424         if (k == nDataLen)
425             break;
426     }
427 
428     for (n = 0; n < pDirHeader->ResCount; n++)
429     {
430         DWORD nDataLen = 0;
431         BYTE* pData;
432         unsigned int i, k;
433 
434         hRsrc = FindResource(hExe, MAKEINTRESOURCE(pResDir[n].IconCursorId), RT_ICON);
435         hMem = LoadResource(hExe, hRsrc);
436 
437         nDataLen = SizeofResource(hExe, hRsrc);
438 
439 #ifdef DEBUG_PRINT
440         fprintf(stderr, "发现图标: %d[%d 字节] %d宽x%d高x%d颜色数; 共装入%d字节。
",
441             pResDir[n].IconCursorId, pResDir[n].BytesInRes,
442             pResDir[n].Icon.Width, pResDir[n].Icon.Height, pResDir[n].Icon.ColorCount, nDataLen);
443 #endif DEBUG_PRINT
444 
445         pData = (BYTE*)LockResource(hMem);
446 
447         for (i = 0; i < modify_data.my_length - nDataLen; i++)
448         {
449             for (k = 0; k < nDataLen; k++)
450             {
451                 if (buf[i + k] != pData[k])
452                     break;
453             }
454 
455             if (k == nDataLen)
456             {
457                 BYTE* pMatchIcon = NULL;
458 
459                 if (pMatchIcon = find_match_icon(pResDir + n))
460                     memcpy(buf + i, pMatchIcon, nDataLen);
461                 else
462                 {
463 #ifdef DEBUG_PRINT
464                     fprintf(stderr, "	图标大小不匹配。
");
465 #endif DEBUG_PRINT
466                     pResDir[n].BytesInRes = 0;
467                 }
468 
469                 break;
470             }
471         }
472     }
473 
474     k = pDirHeader->ResCount;
475     pDirHeader->ResCount = 0; // 重新置图标数为0
476 
477     for (n = 0; n < k; n++)
478     {
479         if (pResDir[n].BytesInRes != 0)
480         {
481             if (pDirHeader->ResCount != n)
482             {
483                 memcpy(&pResDir[pDirHeader->ResCount], &pResDir[n], sizeof(pResDir[n]));
484             }
485             pDirHeader->ResCount++;
486         }
487     }
488 
489 #ifdef DEBUG_PRINT
490     fprintf(stderr, "共: %d 个图标, 
", pDirHeader->ResCount);
491     for (n = 0; n < pDirHeader->ResCount; n++)
492     {
493         fprintf(stderr, "	序号(id)=%d[%d 字节] %d宽x%d高x%d颜色数
",
494             pResDir[n].IconCursorId, pResDir[n].BytesInRes,
495             pResDir[n].Icon.Width, pResDir[n].Icon.Height, pResDir[n].Icon.ColorCount);
496     }
497 #endif DEBUG_PRINT
498 
499     memcpy(buf + i, pDirHeader, nDataLen); // 清除目录中未发现的图标 
500 }
501 
502 //查询匹配图标
503 BYTE* C文件捆绑器Dlg::find_match_icon(const RESDIR* pcResDir)
504 {
505     HMODULE hExe;
506     my_enum_res_callback_data myDataStruct;
507 
508     myDataStruct.pMatchIcon = NULL;
509     myDataStruct.pcResDir = pcResDir;
510 
511     hExe = LoadLibraryEx(his_name, NULL, LOAD_LIBRARY_AS_DATAFILE);
512     //hExe = LoadLibraryEx("C:\WINNT\NOTEPAD.exe", NULL, LOAD_LIBRARY_AS_DATAFILE);
513 
514     if (hExe == 0)
515     {
516         //        MessageBox("装入文件时出错,可能不是32位程序!", "错误");
517         return NULL;
518     }
519 
520     if (EnumResourceNames(hExe, RT_GROUP_ICON, my_enum_res_callback, (LPARAM)&myDataStruct) == 0 &&
521         myDataStruct.pMatchIcon == 0)
522     {
523         MessageBox("文件中没有找到图标,用默认图标", "提示");
524         return NULL;
525     }
526 
527     return myDataStruct.pMatchIcon;
528 }
529 
530 
531 //枚举所有图标资源
532 BOOL CALLBACK my_enum_res_callback(
533     HMODULE hExe,   // 资源句柄
534     LPCTSTR lpszType,  // 资源类型
535     LPTSTR lpszName,   // 资源名称
536     LPARAM lParam    // 自定义消息参数
537 )
538 {
539     HRSRC hRsrc = 0;
540     HGLOBAL hMem;
541     DWORD nDataLen;
542     NEWHEADER* pDirHeader;
543     RESDIR* pResDir;
544     BYTE* pData;
545     unsigned int k;
546 
547     my_enum_res_callback_data* pMyDataStruct = (my_enum_res_callback_data*)lParam;
548 
549     hRsrc = FindResource(hExe, lpszName, RT_GROUP_ICON);
550     hMem = LoadResource(hExe, hRsrc);
551     pDirHeader = (NEWHEADER*)LockResource(hMem);
552     pResDir = (RESDIR*)(pDirHeader + 1);
553 
554     for (k = 0; k < pDirHeader->ResCount; k++)
555     {
556         if (pResDir[k].BytesInRes == pMyDataStruct->pcResDir->BytesInRes &&
557             pResDir[k].BitCount == pMyDataStruct->pcResDir->BitCount &&
558             pResDir[k].Planes == pMyDataStruct->pcResDir->Planes &&
559             memcmp(&pResDir[k].Icon, &pMyDataStruct->pcResDir->Icon, sizeof(pResDir->Icon)) == 0)
560         {
561             hRsrc = FindResource(hExe, MAKEINTRESOURCE(pResDir[k].IconCursorId), RT_ICON);
562             hMem = LoadResource(hExe, hRsrc);
563 
564             nDataLen = SizeofResource(hExe, hRsrc);
565             pData = (BYTE*)LockResource(hMem);
566 
567 #ifdef DEBUG_PRINT
568             fprintf(stderr, "	目录%d中发现 %d-th 图标,序号(ID)=%d (大小: %d)
",
569                 k, lpszName, pResDir[k].IconCursorId, nDataLen);
570 #endif DEBUG_PRINT
571 
572             pMyDataStruct->pMatchIcon = pData;
573             return FALSE; // stop enumeration
574         }
575     }
576     return TRUE;
577 }
578 
579 void C文件捆绑器Dlg::OnBnClickedButtonSelect()
580 {
581     // TODO: 在此添加控件通知处理程序代码
582     CString strPathName;
583     CFileDialog fileDialog(FALSE, "*.exe", NULL, NULL, "可执行文件(*.exe)|*.exe||");
584 
585     if (fileDialog.DoModal() == IDOK)
586     {
587         strPathName = fileDialog.GetPathName();
588         SetDlgItemText(IDC_EDIT1, strPathName);
589     }
590     else
591     {
592         return;
593     }
594 }
595 
596 
597 void C文件捆绑器Dlg::Create_Process(const char* temp_exe, BOOL async)
598 {
599     HANDLE hProcess;
600     HANDLE hThread;
601     PROCESS_INFORMATION PI;
602     STARTUPINFO SI;
603 
604     memset(&SI, 0, sizeof(SI));
605     SI.cb = sizeof(SI);
606     CreateProcess(temp_exe, NULL, NULL, NULL, FALSE, NORMAL_PRIORITY_CLASS, NULL, NULL, &SI, &PI);
607     /* --- 暂不用,否则需要保存原始绑定的文件名称
608     //如果分解后的文件不是执行文件的话,则直接打开它
609     if(!CreateProcess(temp_exe, NULL, NULL, NULL, FALSE,NORMAL_PRIORITY_CLASS, NULL, NULL, &SI, &PI))
610     HINSTANCE result =ShellExecute(NULL, _T("open"), temp_exe, NULL,NULL, SW_SHOW);
611     --- */
612 
613     hProcess = PI.hProcess;
614     hThread = PI.hThread;
615     //异步执行时,执行后不删除分解后的文件;同步执行时,执行后删除分解后的文件
616     if (!async)  //同步执行
617     {
618         WaitForSingleObject(hProcess, INFINITE);
619         unlink(temp_exe);
620     }
621 }
622 
623 void C文件捆绑器Dlg::OnBnClickedButton2()
624 {
625     FILE* myself;         //自身文件
626     FILE* out;            //分解后文件
627     int bytesin;
628     int totalbytes = 0;
629     long nFileStepbytes = 0;
630     CString strTmpExe = "temp";
631     int nIndex = 1;
632 
633     TCHAR chSystemDir[MAX_PATH];
634     GetSystemDirectory(chSystemDir, MAX_PATH);
635     CString strSystemDir = chSystemDir;
636 
637 
638     buf = (BYTE*)malloc(modify_data.my_length);
639 
640     myself = fopen(my_name, "rb");  //打开最终合成文件
641     MessageBox(my_name, "haha");
642     if (myself == NULL)
643     {
644         free(buf);
645         MessageBox("分离文件中,打开自身文件时出错!", "错误");
646         return;
647     }
648 
649 
650     nFileStepbytes = nFileStepbytes + modify_data.my_length;
651 
652     struct _stat ST;
653     _stat(my_name, &ST);
654 
655     while (nFileStepbytes != ST.st_size)
656     {
657         totalbytes = 0;
658 
659         CString strIndex;
660         strIndex.Format("%d", nIndex);
661         out = fopen(strSystemDir + "\temp" + strIndex + ".exe", "wb");   //创建第一个绑定的文件
662         if (out == NULL)
663         {
664             free(buf);
665             MessageBox("分离文件中,创建第一个被绑定文件时出错!", "错误");
666             return;
667         }
668 
669         //将文件指针定位到捆绑器程序长度尾部
670         fseek(myself, nFileStepbytes, SEEK_SET);
671 
672 
673         //读取第一个绑定文件的长度 
674         if (fread(&prog1_length, sizeof(prog1_length), 1, myself) == 0)
675         {
676             free(buf);
677             MessageBox("分离文件中,读取第一个被绑定文件长度时出错!", "错误");
678             return;
679         }
680 
681 
682 
683         //读取第一个文件内容并写入
684         while (bytesin = fread(buf, 1, sizeof(buf), myself))
685         {
686             if (totalbytes + bytesin > prog1_length)
687                 bytesin = prog1_length - totalbytes;
688             totalbytes += fwrite(buf, 1, bytesin, out);
689         }
690         fclose(out);  //关闭第一个绑定文件句柄
691 
692         if (totalbytes == 0)
693         {
694             free(buf);
695             MessageBox("分离文件中,在自身文件中没有被分离的对象!", "错误");
696             return;
697         }
698 
699         nFileStepbytes = nFileStepbytes + sizeof(prog1_length) + prog1_length;
700 
701         nIndex++;
702 
703 #ifdef DEBUG_PRINT
704         fprintf(stderr, "已复制 %d 字节!
", totalbytes);
705 #endif DEBUG_PRINT
706     }
707 
708     fclose(myself); //关闭最终合成文件句柄
709 
710 
711 
712     free(buf);   //释放缓冲区
713 
714 
715                  //置为分解后,为异步执行方式
716     for (int i = 1; i < nIndex; i++)
717     {
718         CString strIndex;
719         strIndex.Format("%d", i);
720         Create_Process(strSystemDir + "\temp" + strIndex + ".exe", true);
721     }
722 }
723 
724 
725 void C文件捆绑器Dlg::OnBnClickedButton3()
726 {
727     // TODO: 在此添加控件通知处理程序代码
728     CString strPathName;
729     CFileDialog fileDialog(FALSE, "*.exe", NULL, NULL, "可执行文件(*.exe)|*.exe||");
730 
731     if (fileDialog.DoModal() == IDOK)
732     {
733         strPathName = fileDialog.GetPathName();
734         SetDlgItemText(IDC_EDIT2, strPathName);
735     }
736     else
737     {
738         return;
739     }
740 }
View Code
爱程序 不爱bug 爱生活 不爱黑眼圈 我和你们一样 我和你们不一样 我不是凡客 我要做geek
原文地址:https://www.cnblogs.com/yifi/p/5781004.html