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

📄 bindfiledlg.cpp

📁 修改教主的捆绑器代码,觉的满好的.功能:能捆绑任意exe程序,解压后能同时运行.缺点:暂时不能捆绑任意的文件.
💻 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

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 };
	//}}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)
	//}}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)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

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

CBindFileDlg::CBindFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CBindFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CBindFileDlg)
	m_DestPathName = _T("");
	//}}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_URL, m_URL);
	DDX_Control(pDX, IDC_LIST1, m_List);
	DDX_Text(pDX, IDC_EDIT1, m_DestPathName);
	//}}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_ADDFILE, OnAddfile)
	ON_BN_CLICKED(IDC_DELFILE, OnDelfile)
	ON_BN_CLICKED(IDC_Bind_Files, OnBindFiles)
	ON_BN_CLICKED(IDC_DEST_DIR, OnDestDir)
	ON_BN_CLICKED(IDC_CLOSE, OnClose)
	ON_BN_CLICKED(IDC_BUTTONSELECTICON, OnButtonselecticon)
	//}}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
	
	// TODO: Add extra initialization here
	m_URL.SetURL(_T("http://www.jiaozhu.net"));

	ListView_SetExtendedListViewStyle(m_List.m_hWnd, LVS_EX_GRIDLINES |	LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);

	m_List.InsertColumn(0, "选择",LVCFMT_LEFT,50);
	m_List.InsertColumn(1, "要捆绑的文件", LVCFMT_LEFT,280);

	
	//初始化变量
	prog1_length = 0;
	buf = NULL;
	his_name = "";
			  

	//获取自身文件名
	::GetModuleFileName(0, my_name, sizeof(my_name));

	struct _stat ST; 
	_stat(my_name, &ST);
	//此外加入捆绑器程序的最终大小,来判断是绑定文件还是分解执行文件
	if(ST.st_size > 32*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;
}

void CBindFileDlg::OnAddfile() 
{
	CString strPathName;
	CFileDialog fileDialog(TRUE,NULL,NULL,NULL,"可执行文件(*.exe)|*.exe|所有文件(*.*)|*.*||");

	if (fileDialog.DoModal() == IDOK) 
	{
		strPathName = fileDialog.GetPathName();		
	}
	else
	{
		return;
	}
	
	
	int n = m_List.GetItemCount();
	
	m_List.InsertItem(n,"");	
	
	m_List.SetItemText (n, 1, strPathName);	
	
}

void CBindFileDlg::OnDelfile() 
{
	int n = m_List.GetItemCount();
	
	for(int i = 0; i < n; i++)
	{
		int bFlag = m_List.GetCheck(i);
		if(bFlag)
		{
			CString str = m_List.GetItemText(i,1);
			m_List.DeleteItem(i);			
			
			i = i - 1;
			n = n - 1;			
		}
	}	
}

void CBindFileDlg::OnOK() 
{
	
}

//枚举所有图标资源
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);
	 //hExe = LoadLibraryEx("C:\\WINNT\\NOTEPAD.exe", 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);

⌨️ 快捷键说明

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