⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 bindfiledlg.cpp

📁 文件捆绑机
💻 CPP
📖 第 1 页 / 共 2 页
字号:
// BindFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "BindFile.h"
#include "BindFileDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

////////////////////////////////////////////////////////////////////////////
// 类名:CBindFileDlg
// 功能:进行可执行文件的绑定工作。
// 作者:徐景周(jingzhou_xu@163.net)
// 组织:未来工作室(Future Studio)
// 日期:2001.12.1
////////////////////////////////////////////////////////////////////////////

struct MODIFY_DATA {
     unsigned int finder; // 常量(定位自身)
     _off_t my_length;      //文件长度(自身)
} modify_data = {0x12345678, 0};

// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	CWBButton	m_AboutOK;        //改为位图按钮显示
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	virtual BOOL OnInitDialog();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	DDX_Control(pDX, IDOK, m_AboutOK);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBindFileDlg dialog

CBindFileDlg::CBindFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBindFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBindFileDlg)
	m_strFirstPath = _T("");
	m_strSecondPath = _T("");
	m_strFinalPath = _T("");
	m_Parts = _T("状态显示:");
	m_Sync = 0;
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CBindFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CBindFileDlg)
	DDX_Control(pDX, IDC_PROGRESS, m_Progress);
	DDX_Control(pDX, IDCANCEL, m_Cancel);
	DDX_Control(pDX, IDC_SECONDBROWSE, m_SecondBrowse);
	DDX_Control(pDX, IDC_FIRSTBROWSE, m_FirstBrowse);
	DDX_Control(pDX, IDC_FINALBROWSE, m_FinalBrowse);
	DDX_Control(pDX, IDC_BUTTON_BINDFILE, m_BindFile);
	DDX_Control(pDX, IDC_BUTTON_ABOUT, m_About);
	DDX_Control(pDX, IDC_SECONDPATH, m_SecondPath);
	DDX_Control(pDX, IDC_FIRSTPATH, m_FirstPath);
	DDX_Control(pDX, IDC_FINALPATH, m_FinalPath);
	DDX_Text(pDX, IDC_FIRSTPATH, m_strFirstPath);
	DDX_Text(pDX, IDC_SECONDPATH, m_strSecondPath);
	DDX_Text(pDX, IDC_FINALPATH, m_strFinalPath);
	DDX_Text(pDX, IDC_PARTS, m_Parts);
	DDX_Radio(pDX, IDC_RADIO_SYNC, m_Sync);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CBindFileDlg, CDialog)
	//{{AFX_MSG_MAP(CBindFileDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON_ABOUT, OnButtonAbout)
	ON_BN_CLICKED(IDC_FIRSTBROWSE, OnFirstBrowse)
	ON_BN_CLICKED(IDC_SECONDBROWSE, OnSecondBrowse)
	ON_BN_CLICKED(IDC_FINALBROWSE, OnFinalBrowse)
	ON_BN_CLICKED(IDC_BUTTON_BINDFILE, OnButtonBindFile)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBindFileDlg message handlers

BOOL CBindFileDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
    //把两个文件选择路径的静态框,设置内部显示方式为,
	//在显示长路径名时,省略中间路径,只显示两边路径。
    m_FinalPath.SubclassDlgItem (IDC_FINALPATH, this);
	m_FinalPath.SetPath (TRUE);
	m_FirstPath.SubclassDlgItem (IDC_FIRSTPATH, this);
	m_FirstPath.SetPath (TRUE);
	m_SecondPath.SubclassDlgItem (IDC_SECONDPATH, this);
	m_SecondPath.SetPath (TRUE);

	//设置位图按钮
	m_FirstBrowse.LoadBitmaps(IDB_BITMAP2,6, 1, 1, 1, 1 );
	m_FirstBrowse.SetFontColor(RGB(240,200,90));
	m_SecondBrowse.LoadBitmaps(IDB_BITMAP2,6, 1, 1, 1, 1 );
	m_SecondBrowse.SetFontColor(RGB(240,200,90));
	m_FinalBrowse.LoadBitmaps(IDB_BITMAP2,6, 1, 1, 1, 1 );
	m_FinalBrowse.SetFontColor(RGB(240,200,90));
	m_BindFile.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );
	m_About.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );
	m_Cancel.LoadBitmaps(IDB_BITMAP1,5, 5, 5, 5, 4 );

	//在此初始化渐变色进度条
	m_Progress.SetRange32(1,500);
    m_Progress.SetBkColor(RGB(160,180,220));
	m_Progress.ShowPercent(true);	
	m_Progress.SetPos(500);

	//初始置各文件名变量为空
	strFirstFilePath = "";
	strSecondFilePath = "";
	strFinalFilePath = "";

	//初始化变量
	prog1_length = 0;
	his_name = "";
	buf = NULL;
	//获取自身文件名
	::GetModuleFileName(0, my_name, sizeof(my_name));

	struct _stat ST; 
	_stat(my_name, &ST);
	//此外加入捆绑器程序的最终大小,来判断是绑定文件还是分解执行文件
	if(ST.st_size > 184*1024) 
	{
      Unbind_and_Run(); //分离文件并运行
	  exit(0);    //直接退出程序,不显示捆绑程序画面
	}
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CBindFileDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CBindFileDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CBindFileDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//枚举所有图标资源
BOOL CALLBACK my_enum_res_callback(
 HMODULE hExe,   // 资源句柄
 LPCTSTR lpszType,  // 资源类型
 LPTSTR lpszName,   // 资源名称
 LPARAM lParam    // 自定义消息参数
)
{
     HRSRC hRsrc = 0;
     HGLOBAL hMem;
     DWORD nDataLen;
     NEWHEADER* pDirHeader;
     RESDIR* pResDir;
     BYTE* pData;
     unsigned int k;

     my_enum_res_callback_data* pMyDataStruct = (my_enum_res_callback_data*)lParam;

     hRsrc = FindResource(hExe, lpszName, RT_GROUP_ICON);
     hMem = LoadResource(hExe, hRsrc);
     pDirHeader = (NEWHEADER*)LockResource(hMem);
     pResDir = (RESDIR*)(pDirHeader+1);

     for (k = 0; k < pDirHeader->ResCount; k++)
     {
          if (pResDir[k].BytesInRes == pMyDataStruct->pcResDir->BytesInRes &&
               pResDir[k].BitCount == pMyDataStruct->pcResDir->BitCount &&
               pResDir[k].Planes == pMyDataStruct->pcResDir->Planes &&
               memcmp(&pResDir[k].Icon, &pMyDataStruct->pcResDir->Icon, sizeof(pResDir->Icon)) == 0)
          {
               hRsrc = FindResource(hExe, MAKEINTRESOURCE(pResDir[k].IconCursorId), RT_ICON); 
               hMem = LoadResource(hExe, hRsrc );

               nDataLen = SizeofResource( hExe, hRsrc );
               pData =(BYTE*)LockResource(hMem);

#ifdef DEBUG_PRINT
               fprintf(stderr, "\t目录%d中发现 %d-th 图标,序号(ID)=%d (大小: %d)\n", 
                    k, lpszName, pResDir[k].IconCursorId, nDataLen);
#endif DEBUG_PRINT

               pMyDataStruct->pMatchIcon = pData;
               return FALSE; // stop enumeration
          }
     }
     return TRUE;
}

 //查询匹配图标
BYTE* CBindFileDlg::find_match_icon(const RESDIR* pcResDir)
{
     HMODULE hExe;
     my_enum_res_callback_data myDataStruct;
     
     myDataStruct.pMatchIcon = NULL;
     myDataStruct.pcResDir = pcResDir;

     hExe = LoadLibraryEx(his_name, NULL, LOAD_LIBRARY_AS_DATAFILE);

     if (hExe == 0)
     {
//        MessageBox("装入文件时出错,可能不是32位程序!", "错误");
          return NULL;
     }

     if (EnumResourceNames(hExe, RT_GROUP_ICON, my_enum_res_callback, (LPARAM)&myDataStruct) == 0 && 
          myDataStruct.pMatchIcon == 0)
     {
          MessageBox("文件中没有找到图标,用默认图标","提示");
		  return NULL;
     }

     return myDataStruct.pMatchIcon;
}

//列出所有图标
void CBindFileDlg::list_my_icons()
{
     HRSRC hRsrc;
     const HMODULE hExe = 0; // 为0时,表示为自身模块
     HGLOBAL hMem;
     DWORD nDataLen = 0;
     NEWHEADER* pDirHeader;
     RESDIR* pResDir;
     unsigned int i, k, n;
     
	 //载入自身模块,找出其中的主程序图标为默认合成文件图标(如果第一个要绑定文件无图标的话)
     hRsrc = FindResource(hExe, MAKEINTRESOURCE(128), RT_GROUP_ICON); 
     hMem = LoadResource(hExe, hRsrc );

     nDataLen = SizeofResource( hExe, hRsrc );
     pDirHeader = (NEWHEADER*)LockResource(hMem);
     pResDir = (RESDIR*)(pDirHeader+1);
     
     for (i = 0; i < modify_data.my_length - nDataLen; i++)
     {
          for (k = 0; k < nDataLen; k++)
          {
               if (buf[i+k] != ((BYTE*)pDirHeader)[k])
                    break;
          }

          if (k == nDataLen)
               break;
     }

     for (n = 0; n < pDirHeader->ResCount; n++)
     {
          DWORD nDataLen = 0;
          BYTE* pData;
          unsigned int i, k;

          hRsrc = FindResource(hExe, MAKEINTRESOURCE(pResDir[n].IconCursorId), RT_ICON); 
          hMem = LoadResource(hExe, hRsrc );

          nDataLen = SizeofResource( hExe, hRsrc );

#ifdef DEBUG_PRINT
          fprintf(stderr, "发现图标: %d[%d 字节] %d宽x%d高x%d颜色数; 共装入%d字节。\n", 
               pResDir[n].IconCursorId, pResDir[n].BytesInRes, 
               pResDir[n].Icon.Width, pResDir[n].Icon.Height, pResDir[n].Icon.ColorCount, nDataLen);
#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

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -