首先是界面:
使用了论坛破解的SkinH,下面是运行时的效果:
下面是控件与变量的关联:
添加两个成员变量,分别是要对比的两个文件的对象,添加一个成员函数,用于文件偏移转为RVA
public:
CFile m_f2;
CFile m_f1;
DWORD FileOffsetToVa(DWORD addr);
文件打开按钮的代码:
void CCPDlg::OnOpen1()
{
CFileDialog dlg(TRUE);
if (dlg.DoModal() == IDOK)
{
m_File1 = dlg.GetPathName();
UpdateData(FALSE);
}
}
文件对比函数的代码(内含操作列表控件):
void CCPDlg::OnDo()
{
unsigned len,i;
unsigned char c1,c2;
DWORD n;
CString temp;
m_f1.Open(m_File1,CFile::modeRead);
m_f2.Open(m_File2,CFile::modeRead);
len = m_f1.GetLength();
if (len != m_f2.GetLength())
{
MessageBox("文件大小不同,不具可比性!","提示");
}
else
{
i = 0;
for (n=0;n<len;n++)
{
m_f1.Read(&c1,1);
m_f2.Read(&c2,1);
if (c1 != c2)
{
temp.Format("%02d",i+1);
m_List.InsertItem(i,temp);
temp.Format("0x%08X",FileOffsetToVa(n));
m_List.SetItemText(i,1,temp);
temp.Format("0x%08X",n);
m_List.SetItemText(i,2,temp);
temp.Format("0x%02X",c1);
m_List.SetItemText(i,3,temp);
temp.Format("0x%02X",c2);
m_List.SetItemText(i,4,temp);
i++;
}
}
if (i == 0)
{
MessageBox("两个文件完全相同!","完成");
}
else
{
temp.Format("两个文件共 %d 字节不同!",i);
MessageBox(temp,"完成");
}
}
temp.ReleaseBuffer();
m_f1.Close();
m_f2.Close();
}
文件偏移转为RVA的代码:
DWORD CCPDlg::FileOffsetToVa(DWORD addr)
{
int n;
IMAGE_DOS_HEADER dh;
IMAGE_NT_HEADERS nh;
IMAGE_SECTION_HEADER sh;
DWORD tempSeek,RVA;
tempSeek = m_f1.GetPosition();
m_f1.SeekToBegin();
m_f1.Read(&dh,sizeof(IMAGE_DOS_HEADER));
m_f1.Seek(dh.e_lfanew,CFile::begin);
m_f1.Read(&nh,sizeof(IMAGE_NT_HEADERS));
m_f1.Read(&sh,sizeof(IMAGE_SECTION_HEADER));
if (addr < sh.PointerToRawData)
{
RVA = addr+nh.OptionalHeader.ImageBase;
}
else if (addr <= sh.PointerToRawData+sh.SizeOfRawData)
{
RVA = addr+nh.OptionalHeader.ImageBase+sh.VirtualAddress-sh.PointerToRawData;
}
else
{
for (n=1;n<nh.FileHeader.NumberOfSections;n++)
{
m_f1.Read(&sh,sizeof(IMAGE_SECTION_HEADER));
if (addr <= sh.PointerToRawData+sh.SizeOfRawData)
{
break;
}
}
RVA = addr+nh.OptionalHeader.ImageBase+sh.VirtualAddress-sh.PointerToRawData;
}
m_f1.Seek(tempSeek,CFile::begin);
return RVA;
}
最后,添加了关于对话框,使用的是非模态对话框,并且添加了水波效果:
下面是完整源码,以及可运行文件:
Release.rar
(159.66 KB, 下载次数: 293)
src.rar
(120.05 KB, 下载次数: 218)
|