主程序代码 PEComp.c

  1 #include <windows.h>
  2 #include <Richedit.h>
  3 #include <Commctrl.h>
  4 #include "resource.h"
  5 
  6 
  7 HINSTANCE hInstance;
  8 DWORD dwCount;
  9 
 10 HWND hWinEdit;    //富文本框句柄
 11 
 12 
 13 /*
 14 初始化窗口程序
 15 */
 16 void _Init(HWND hWinMain)
 17 {
 18     HICON hIcon;
 19     CHARFORMAT stCf;
 20     TCHAR szFont[] = TEXT("宋体");
 21 
 22 
 23     hWinEdit = GetDlgItem(hWinMain, IDC_INFO);
 24     hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(ICO_MAIN));
 25     SendMessage(hWinMain, WM_SETICON, ICON_BIG, (LPARAM)hIcon);//为窗口设置图标
 26     SendMessage(hWinEdit, EM_SETTEXTMODE, TM_PLAINTEXT, 0);//设置编辑控件
 27 
 28     RtlZeroMemory(&stCf, sizeof(stCf));
 29     stCf.cbSize = sizeof(stCf);
 30     stCf.yHeight = 10 * 20;
 31     stCf.dwMask = CFM_FACE | CFM_SIZE | CFM_BOLD;
 32     lstrcpy(stCf.szFaceName, szFont);
 33     SendMessage(hWinEdit, EM_SETCHARFORMAT, 0, (LPARAM)&stCf);
 34     SendMessage(hWinEdit, EM_EXLIMITTEXT, 0, -1);
 35 }
 36 
 37 /*
 38  清除ListView中的内容
 39  删除所有的行和所有的列
 40 */
 41 void _ListViewClear(HWND _hWinView)
 42 {
 43     SendMessage(_hWinView, LVM_DELETEALLITEMS, 0, 0);
 44     
 45     while (SendMessage(_hWinView, LVM_DELETECOLUMN, 0, 0)){}
 46 }
 47 
 48 
 49 /*
 50 在ListView中增加一个列
 51 输入:_dwColumn = 增加的列编号
 52 _dwWidth = 列的宽度
 53 _lpszHead = 列的标题字符串 
 54 */
 55 void _ListViewAddColumn(HWND _hWinView,DWORD _dwColumn,DWORD _dwWidth,PTCHAR _lpszHead)
 56 {
 57     LV_COLUMN stLVC;
 58 
 59     RtlZeroMemory(&stLVC, sizeof(LV_COLUMN));
 60     stLVC.mask = LVCF_TEXT | LVCF_WIDTH | LVCF_FMT;
 61     stLVC.fmt = LVCFMT_LEFT;
 62     stLVC.pszText = _lpszHead;
 63     stLVC.cx = _dwWidth;
 64     stLVC.iSubItem = _dwColumn;
 65     SendMessage(_hWinView, LVM_INSERTCOLUMN, _dwColumn, (LPARAM)&stLVC);
 66 }
 67 
 68 /*
 69 初始化结果表格
 70 */
 71 void _clearResultView(HWND hProcessModuleTable)
 72 {
 73     _ListViewClear(hProcessModuleTable);
 74     
 75     //添加表头
 76     _ListViewAddColumn(hProcessModuleTable, 1, 200, TEXT("PE数据结构相关字段"));
 77     _ListViewAddColumn(hProcessModuleTable, 2, 222, TEXT("文件1的值(H)"));
 78     _ListViewAddColumn(hProcessModuleTable, 3, 222, TEXT("文件2的值(H)"));
 79 
 80     dwCount = 0;
 81 }
 82 
 83 
 84 void _GetListViewItem(HWND _hWinView, DWORD _dwLine, DWORD  _dwCol, PTCHAR _lpszText)
 85 {
 86     LV_ITEM stLVI;
 87 
 88 
 89     RtlZeroMemory(&stLVI, sizeof(LV_ITEM));
 90     RtlZeroMemory(_lpszText, 512);
 91 
 92     stLVI.cchTextMax = 512;
 93     stLVI.mask = LVIF_TEXT;
 94     stLVI.pszText = _lpszText;
 95     stLVI.iSubItem = _dwCol;
 96     SendMessage(_hWinView, LVM_GETITEMTEXT, _dwLine, (LPARAM)&stLVI);
 97 }
 98 
 99 
100 int _MemCmp(PTCHAR _lp1, PTCHAR _lp2, int  _size)
101 {
102     DWORD dwResult = 0;
103 
104     for (int i = 0; i < _size; ++i)
105     {
106         if (_lp1[i] != _lp2[i])
107         {
108             dwResult = 1;
109             break;
110         }
111     }
112     return dwResult;
113 }
114 
115 
116 
117 /*
118 在ListView中新增一行,或修改一行中某个字段的内容
119 输入:_dwItem = 要修改的行的编号
120 _dwSubItem = 要修改的字段的编号,-1表示插入新的行,>=1表示字段的编号
121 */
122 DWORD _ListViewSetItem(HWND _hWinView, DWORD _dwItem, DWORD _dwSubItem, PTCHAR _lpszText)
123 {
124     LV_ITEM stLVI;
125 
126 
127     RtlZeroMemory(&stLVI, sizeof(LV_ITEM));
128     stLVI.cchTextMax = lstrlen(_lpszText);
129     stLVI.mask = LVIF_TEXT;
130     stLVI.pszText = _lpszText;
131     stLVI.iItem = _dwItem;
132     stLVI.iSubItem = _dwSubItem;
133 
134     if (_dwSubItem == -1)
135     {
136         stLVI.iSubItem = 0;
137         return SendMessage(_hWinView, LVM_INSERTITEM, 0, (LPARAM)&stLVI);
138     }
139     else
140     {
141         return SendMessage(_hWinView, LVM_SETITEM, 0, (LPARAM)&stLVI);
142     }
143 }
144 
145 
146 
147 /*
148 将_lpSZ位置处_Size个字节转换为16进制的字符串
149 szBuffer处为转换后的字符串
150 */
151 void _Byte2Hex(PTCHAR _lpSZ, PTCHAR szBuffer, int _Size)
152 {
153     TCHAR szBuf[4];
154 
155     for (int i = 0; i < _Size; ++i)
156     {
157         wsprintf(szBuf, TEXT("%02X "), (TBYTE)_lpSZ[i]);
158         lstrcat(szBuffer, szBuf);
159     }
160 }
161 
162 
163 void _addLine(HWND hProcessModuleTable,PTCHAR _lpSZ, PTCHAR _lpSP1, PTCHAR _lpSP2, int _Size)
164 {
165     TCHAR szBuffer[256];
166 
167 
168 
169 
170     dwCount = _ListViewSetItem(hProcessModuleTable, dwCount, -1, _lpSZ); // 在表格中新增加一行
171     _ListViewSetItem(hProcessModuleTable, dwCount, 0, _lpSZ);//显示字段名
172 
173     //将指定字段按照十六进制显示,格式:一个字节+一个空格
174 
175     RtlZeroMemory(szBuffer, 256);
176     _Byte2Hex(_lpSP1, szBuffer, _Size);
177     _ListViewSetItem(hProcessModuleTable, dwCount, 1, szBuffer);//第一个文件中的值
178 
179 
180     RtlZeroMemory(szBuffer, 256);
181     _Byte2Hex(_lpSP2, szBuffer, _Size);
182     _ListViewSetItem(hProcessModuleTable, dwCount, 2, szBuffer);//第二个文件中的值
183 }
184 
185 
186 /*
187 IMAGE_DOS_HEADER头信息
188 */
189 void _Header1(HWND hProcessModuleTable, PTCHAR lpMemory, PTCHAR lpMemory1)
190 {
191     int offbuf[19] = { 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 8, 2, 2, 20, 4};
192     TCHAR szRec[19][30] = {TEXT("IMAGE_DOS_HEADER.e_magic"),
193                            TEXT("IMAGE_DOS_HEADER.e_cblp"),
194                            TEXT("IMAGE_DOS_HEADER.e_cp"),
195                            TEXT("IMAGE_DOS_HEADER.e_crlc"),
196                            TEXT("IMAGE_DOS_HEADER.e_cparhdr"),
197                            TEXT("IMAGE_DOS_HEADER.e_minalloc"),
198                            TEXT("IMAGE_DOS_HEADER.e_maxalloc"),
199                            TEXT("IMAGE_DOS_HEADER.e_ss"),
200                            TEXT("IMAGE_DOS_HEADER.e_sp"),
201                            TEXT("IMAGE_DOS_HEADER.e_csum"),
202                            TEXT("IMAGE_DOS_HEADER.e_ip"),
203                            TEXT("IMAGE_DOS_HEADER.e_cs"),
204                            TEXT("IMAGE_DOS_HEADER.e_lfarlc"),
205                            TEXT("IMAGE_DOS_HEADER.e_ovno"),
206                            TEXT("IMAGE_DOS_HEADER.e_res"),
207                            TEXT("IMAGE_DOS_HEADER.e_oemid"),
208                            TEXT("IMAGE_DOS_HEADER.e_oeminfo"),
209                            TEXT("IMAGE_DOS_HEADER.e_res2"),
210                            TEXT("IMAGE_DOS_HEADER.e_lfanew"),};
211 
212     for (int i = 0, off = 0; i < 19; ++i)
213     {
214         _addLine(hProcessModuleTable,szRec[i], lpMemory + off, lpMemory1 + off, offbuf[i]);
215         off += offbuf[i];
216     }
217 }
218 
219 
220 /*
221 IMAGE_DOS_HEADER头信息
222 */
223 void _Header2(HWND hProcessModuleTable, PTCHAR lpMemory, PTCHAR lpMemory1)
224 {
225     int offbuf[70] = { 4, 2, 2, 4, 4, 4, 2, 2, 2, 1, 1, 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 2, 2, 2, 2,
226         4, 4, 4, 4, 2, 2, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
227         4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4 };
228     TCHAR szRec[70][60] = { TEXT("IMAGE_NT_HEADERS.Signature"),
229                             TEXT("IMAGE_FILE_HEADER.Machine"),
230                             TEXT("IMAGE_FILE_HEADER.NumberOfSections"),
231                             TEXT("IMAGE_FILE_HEADER.TimeDateStamp"),
232                             TEXT("IMAGE_FILE_HEADER.PointerToSymbolTable"),
233                             TEXT("IMAGE_FILE_HEADER.NumberOfSymbols"),
234                             TEXT("IMAGE_FILE_HEADER.SizeOfOptionalHeader"),
235                             TEXT("IMAGE_FILE_HEADER.Characteristics"),
236                             TEXT("IMAGE_OPTIONAL_HEADER32.Magic"),
237                             TEXT("IMAGE_OPTIONAL_HEADER32.MajorLinkerVersion"),
238                             TEXT("IMAGE_OPTIONAL_HEADER32.MinorLinkerVersion"),
239                             TEXT("IMAGE_OPTIONAL_HEADER32.SizeOfCode"),
240                             TEXT("IMAGE_OPTIONAL_HEADER32.SizeOfInitializedData"),
241                             TEXT("IMAGE_OPTIONAL_HEADER32.SizeOfUninitializedData"),
242                             TEXT("IMAGE_OPTIONAL_HEADER32.AddressOfEntryPoint"),
243                             TEXT("IMAGE_OPTIONAL_HEADER32.BaseOfCode"),
244                             TEXT("IMAGE_OPTIONAL_HEADER32.BaseOfData"),
245                             TEXT("IMAGE_OPTIONAL_HEADER32.ImageBase"),
246                             TEXT("IMAGE_OPTIONAL_HEADER32.SectionAlignment"),
247                             TEXT("IMAGE_OPTIONAL_HEADER32.FileAlignment"),
248                             TEXT("IMAGE_OPTIONAL_HEADER32.MajorOperatingSystemVersion"),
249                             TEXT("IMAGE_OPTIONAL_HEADER32.MinorOperatingSystemVersion"),
250                             TEXT("IMAGE_OPTIONAL_HEADER32.MajorImageVersion"),
251                             TEXT("IMAGE_OPTIONAL_HEADER32.MinorImageVersion"),
252                             TEXT("IMAGE_OPTIONAL_HEADER32.MajorSubsystemVersion"),
253                             TEXT("IMAGE_OPTIONAL_HEADER32.MinorSubsystemVersion"),
254                             TEXT("IMAGE_OPTIONAL_HEADER32.Win32VersionValue"),
255                             TEXT("IMAGE_OPTIONAL_HEADER32.SizeOfImage"),
256                             TEXT("IMAGE_OPTIONAL_HEADER32.SizeOfHeaders"),
257                             TEXT("IMAGE_OPTIONAL_HEADER32.CheckSum"),
258                             TEXT("IMAGE_OPTIONAL_HEADER32.Subsystem"),
259                             TEXT("IMAGE_OPTIONAL_HEADER32.DllCharacteristics"),
260                             TEXT("IMAGE_OPTIONAL_HEADER32.SizeOfStackReserve"),
261                             TEXT("IMAGE_OPTIONAL_HEADER32.SizeOfStackCommit"),
262                             TEXT("IMAGE_OPTIONAL_HEADER32.SizeOfHeapReserve"),
263                             TEXT("IMAGE_OPTIONAL_HEADER32.SizeOfHeapCommit"),
264                             TEXT("IMAGE_OPTIONAL_HEADER32.LoaderFlags"),
265                             TEXT("IMAGE_OPTIONAL_HEADER32.NumberOfRvaAndSizes"),
266                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Export)"),
267                             TEXT("IMAGE_DATA_DIRECTORY.isize(Export)"),
268                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Import)"),
269                             TEXT("IMAGE_DATA_DIRECTORY.isize(Import)"),
270                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Resource)"),
271                             TEXT("IMAGE_DATA_DIRECTORY.isize(Resource)"),
272                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Exception)"),
273                             TEXT("IMAGE_DATA_DIRECTORY.isize(Exception)"),
274                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Security)"),
275                             TEXT("IMAGE_DATA_DIRECTORY.isize(Security)"),
276                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(BaseReloc)"),
277                             TEXT("IMAGE_DATA_DIRECTORY.isize(BaseReloc)"),
278                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Debug)"),
279                             TEXT("IMAGE_DATA_DIRECTORY.isize(Debug)"),
280                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Architecture)"),
281                             TEXT("IMAGE_DATA_DIRECTORY.isize(Architecture)"),
282                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(GlobalPTR)"),
283                             TEXT("IMAGE_DATA_DIRECTORY.isize(GlobalPTR)"),
284                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(TLS)"),
285                             TEXT("IMAGE_DATA_DIRECTORY.isize(TLS)"),
286                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Load_Config)"),
287                             TEXT("IMAGE_DATA_DIRECTORY.isize(Load_Config)"),
288                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Bound_Import)"),
289                             TEXT("IMAGE_DATA_DIRECTORY.isize(Bound_Import)"),
290                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(IAT)"),
291                             TEXT("IMAGE_DATA_DIRECTORY.isize(IAT)"),
292                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Delay_Import)"),
293                             TEXT("IMAGE_DATA_DIRECTORY.isize(Delay_Import)"),
294                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Com_Descriptor)"),
295                             TEXT("IMAGE_DATA_DIRECTORY.isize(Com_Descriptor)"),
296                             TEXT("IMAGE_DATA_DIRECTORY.VirtualAddress(Reserved)"),
297                             TEXT("IMAGE_DATA_DIRECTORY.isize(Reserved)") };
298 
299     for (int i = 0, off = 0; i < 70; ++i)
300     {
301         _addLine(hProcessModuleTable,szRec[i], lpMemory + off, lpMemory1 + off, offbuf[i]);
302         off += offbuf[i];
303     }
304 }
305 
306 
307 /*
308 节表
309 */
310 void _Header3(HWND hProcessModuleTable, PTCHAR lpMemory, PTCHAR lpMemory1, DWORD _dwValue)
311 {
312     int offbuf[10] = { 8, 4, 4, 4, 4, 4, 4, 2, 2, 4};
313     TCHAR szBuffer[256];
314     TCHAR szRec[10][50] = { TEXT("IMAGE_SECTION_HEADER%d.Name1"),
315                             TEXT("IMAGE_SECTION_HEADER%d.VirtualSize"),
316                             TEXT("IMAGE_SECTION_HEADER%d.VirtualAddress"),
317                             TEXT("IMAGE_SECTION_HEADER%d.SizeOfRawData"),
318                             TEXT("IMAGE_SECTION_HEADER%d.PointerToRawData"),
319                             TEXT("IMAGE_SECTION_HEADER%d.PointerToRelocations"),
320                             TEXT("IMAGE_SECTION_HEADER%d.PointerToLinenumbers"),
321                             TEXT("IMAGE_SECTION_HEADER%d.NumberOfRelocations"),
322                             TEXT("IMAGE_SECTION_HEADER%d.NumberOfLinenumbers"),
323                             TEXT("IMAGE_SECTION_HEADER%d.Characteristics")};
324 
325 
326     for (int i = 0, off = 0; i < 10; ++i)
327     {
328         wsprintf(szBuffer, szRec[i], _dwValue);
329         _addLine(hProcessModuleTable,szBuffer, lpMemory + off, lpMemory1 + off, offbuf[i]);
330         off += offbuf[i];
331     }
332 }
333 
334 
335 
336 
337 /*
338 打开PE文件并处理
339 */
340 void _openFile(HWND hWinMain, HWND hProcessModuleTable, PTCHAR szFileNameOpen1, PTCHAR szFileNameOpen2)
341 {
342     HANDLE hFile;
343     HANDLE hMapFile = NULL;
344     HANDLE hFile1;
345     HANDLE hMapFile1=NULL;
346     DWORD dwFileSize, dwFileSize1;
347     static LPVOID lpMemory, lpMemory1;
348 
349 
350 
351 
352     hFile = CreateFile(szFileNameOpen1, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
353         OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL);
354 
355     if (hFile != INVALID_HANDLE_VALUE)
356     {
357         dwFileSize = GetFileSize(hFile, NULL);
358         if (dwFileSize)
359         {
360             hMapFile = CreateFileMapping(hFile, NULL, PAGE_READONLY, 0, 0, NULL);// 内存映射文件
361             if (hMapFile)
362             {
363                 lpMemory = MapViewOfFile(hMapFile, FILE_MAP_READ, 0, 0, 0);
364                 if (((PIMAGE_DOS_HEADER)lpMemory)->e_magic != IMAGE_DOS_SIGNATURE)//判断是否有MZ字样
365                 {
366                 _ErrFormat:
367                     MessageBox(hWinMain, TEXT("这个文件不是PE格式的文件!"), NULL, MB_OK);
368                     UnmapViewOfFile(lpMemory);
369                     CloseHandle(hMapFile);
370                     CloseHandle(hFile);
371                     return;
372                 }
373                 if (((PIMAGE_NT_HEADERS)((PTCHAR)lpMemory +
374                     ((PIMAGE_DOS_HEADER)lpMemory)->e_lfanew))->Signature !=
375                     IMAGE_NT_SIGNATURE)//判断是否有PE字样
376                 {
377                     goto _ErrFormat;
378                 }
379             }
380         }
381     }
382 
383 
384 
385     hFile1 = CreateFile(szFileNameOpen2, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL,
386         OPEN_EXISTING, FILE_ATTRIBUTE_ARCHIVE, NULL);
387 
388     if (hFile1 != INVALID_HANDLE_VALUE)
389     {
390         dwFileSize1 = GetFileSize(hFile1, NULL);
391         if (dwFileSize1)
392         {
393             hMapFile1 = CreateFileMapping(hFile1, NULL, PAGE_READONLY, 0, 0, NULL);// 内存映射文件
394             if (hMapFile1)
395             {
396                 lpMemory1 = MapViewOfFile(hMapFile1, FILE_MAP_READ, 0, 0, 0);
397 
398                 if (((PIMAGE_DOS_HEADER)lpMemory1)->e_magic != IMAGE_DOS_SIGNATURE)//判断是否有MZ字样
399                 {
400                 _ErrFormat1:
401                     MessageBox(hWinMain, TEXT("这个文件不是PE格式的文件!"), NULL, MB_OK);
402                     UnmapViewOfFile(lpMemory1);
403                     CloseHandle(hMapFile1);
404                     CloseHandle(hFile1);
405                     return;
406                 }
407                 
408                 if (((PIMAGE_NT_HEADERS)((PTCHAR)lpMemory1 +
409                     ((PIMAGE_DOS_HEADER)lpMemory1)->e_lfanew))->Signature !=
410                     IMAGE_NT_SIGNATURE)//判断是否有PE字样
411                 {
412                     goto _ErrFormat1;
413                 }
414             }
415         }
416     }
417 
418 
419 
420 
421 /*
422 到此为止,两个内存文件的指针已经获取到了。
423 lpMemory和lpMemory1分别指向两个文件头
424 下面是从这个文件头开始,找出各数据结构的字段值,进行比较。
425 */
426 
427     _Header1(hProcessModuleTable, (PTCHAR)lpMemory, (PTCHAR)lpMemory1);
428     
429     //调整指针指向PE文件头
430     (PTCHAR)lpMemory += ((PIMAGE_DOS_HEADER)lpMemory)->e_lfanew;
431     (PTCHAR)lpMemory1 += ((PIMAGE_DOS_HEADER)lpMemory1)->e_lfanew;
432     _Header2(hProcessModuleTable, (PTCHAR)lpMemory, (PTCHAR)lpMemory1);
433 
434 
435     //节的数量
436     WORD dNum, dNum1, dNum2;
437     dNum1 = ((PIMAGE_NT_HEADERS)lpMemory)->FileHeader.NumberOfSections;
438     dNum2 = ((PIMAGE_NT_HEADERS)lpMemory1)->FileHeader.NumberOfSections;
439     dNum = dNum1 > dNum2 ? dNum1 : dNum2;
440 
441     //整指针指向节表
442     (PTCHAR)lpMemory += sizeof(IMAGE_NT_HEADERS);
443     (PTCHAR)lpMemory1 += sizeof(IMAGE_NT_HEADERS);
444 
445     DWORD _dwValue = 1;//节序号
446     while (dNum--)
447     {
448         _Header3(hProcessModuleTable, (PTCHAR)lpMemory, (PTCHAR)lpMemory1, _dwValue++);
449 
450         (PTCHAR)lpMemory += sizeof(IMAGE_SECTION_HEADER);
451         (PTCHAR)lpMemory1 += sizeof(IMAGE_SECTION_HEADER);
452     }
453 
454     UnmapViewOfFile(lpMemory);
455     CloseHandle(hMapFile);
456     CloseHandle(hFile);
457 
458     UnmapViewOfFile(lpMemory1);
459     CloseHandle(hMapFile1);
460     CloseHandle(hFile1);
461 }
462 
463 /*
464 打开输入文件
465 */
466 void _OpenFile1(HWND hWinMain, HWND hText1, PTCHAR szFileNameOpen1)
467 {
468     OPENFILENAME stOF;
469 
470 
471     RtlZeroMemory(&stOF, sizeof(stOF));
472     stOF.lStructSize = sizeof(stOF);
473     stOF.hwndOwner = hWinMain;
474     stOF.hInstance = hInstance;
475     stOF.lpstrFilter = TEXT("Excutable Files\0*.exe;*.com\0\0");
476     stOF.lpstrFile = szFileNameOpen1;
477     stOF.nMaxFile = MAX_PATH;
478     stOF.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
479     if (GetOpenFileName(&stOF))//显示“打开文件”对话框
480     {
481         SetWindowText(hText1, szFileNameOpen1);
482     }
483 
484 }
485 
486 
487 /*
488 打开输入文件
489 */
490 void _OpenFile2(HWND hWinMain, HWND hText2, PTCHAR szFileNameOpen2)
491 {
492     OPENFILENAME stOF;
493     
494 
495     RtlZeroMemory(&stOF, sizeof(stOF));
496     stOF.lStructSize = sizeof(stOF);
497     stOF.hwndOwner = hWinMain;
498     stOF.hInstance = hInstance;
499     stOF.lpstrFilter = TEXT("Excutable Files\0*.exe;*.com\0\0");
500     stOF.lpstrFile = szFileNameOpen2;
501     stOF.nMaxFile = MAX_PATH;
502     stOF.Flags = OFN_FILEMUSTEXIST | OFN_HIDEREADONLY | OFN_PATHMUSTEXIST;
503     if (GetOpenFileName(&stOF))//显示“打开文件”对话框
504     {
505         SetWindowText(hText2, szFileNameOpen2);
506     }
507 
508 }
509 
510 
511 
512 
513 
514 
515 INT_PTR CALLBACK _resultProcMain(HWND hProcessModuleDlg, UINT wMsg, WPARAM wParam, LPARAM lParam)
516 {
517     static HWND hWinMain, hProcessModuleTable, hText1, hText2;
518     TCHAR bufTemp1[0x200], bufTemp2[0x200];
519     static TCHAR szFileNameOpen1[MAX_PATH], szFileNameOpen2[MAX_PATH];
520 
521     switch (wMsg)
522     {
523     case WM_CLOSE:
524         EndDialog(hProcessModuleDlg, 0);
525         break;
526 
527     case WM_INITDIALOG:            //初始化
528         hWinMain = (HWND)lParam;
529 
530         hProcessModuleTable = GetDlgItem(hProcessModuleDlg, IDC_MODULETABLE);
531         hText1 = GetDlgItem(hProcessModuleDlg, ID_TEXT1);
532         hText2 = GetDlgItem(hProcessModuleDlg, ID_TEXT2);
533 
534         //定义表格外观
535         SendMessage(hProcessModuleTable, LVM_SETEXTENDEDLISTVIEWSTYLE, 0,
536             LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
537         ShowWindow(hProcessModuleTable, SW_SHOW);
538         
539         _clearResultView(hProcessModuleTable);//清空表格内容
540         break;
541 
542     case WM_NOTIFY:
543         if(((NMHDR *)lParam)->hwndFrom == hProcessModuleTable)//更改各控件状态
544         {
545             if (((NMHDR *)lParam)->code == NM_CUSTOMDRAW)//绘画时
546             {
547                 if (((NMLVCUSTOMDRAW *)lParam)->nmcd.dwDrawStage == CDDS_PREPAINT)
548                 {
549                     SetWindowLong(hProcessModuleDlg, DWL_MSGRESULT, CDRF_NOTIFYITEMDRAW);
550                 }
551                 else if (((NMLVCUSTOMDRAW *)lParam)->nmcd.dwDrawStage == CDDS_ITEMPREPAINT)
552                 {
553                     //当每一单元格内容预画时,判断两列的值是否一致
554                     //如果一致,则将文本的背景色设置为浅红色,否则黑色
555                     _GetListViewItem(hProcessModuleTable, ((NMLVCUSTOMDRAW *)lParam)->nmcd.dwItemSpec,
556                         1, bufTemp1);
557                     _GetListViewItem(hProcessModuleTable, ((NMLVCUSTOMDRAW *)lParam)->nmcd.dwItemSpec,
558                         2, bufTemp2);
559                     if (_MemCmp(bufTemp1, bufTemp2, lstrlen(bufTemp1)))
560                     {
561                         ((NMLVCUSTOMDRAW *)lParam)->clrTextBk = 0xa0a0ff;
562                     }
563                     else
564                     {
565                         ((NMLVCUSTOMDRAW *)lParam)->clrTextBk = 0xffffff;
566                     }
567                     SetWindowLong(hProcessModuleDlg, DWL_MSGRESULT, CDRF_DODEFAULT);
568                 }
569             }
570         }
571         break;
572 
573     case WM_COMMAND:            //菜单
574         switch (LOWORD(wParam))
575         {
576         case IDC_OK:            //刷新
577             _openFile(hWinMain, hProcessModuleTable, szFileNameOpen1, szFileNameOpen2);
578             break;
579 
580         case IDC_BROWSE1:            //用户选择第一个文件
581             _OpenFile1(hWinMain, hText1, szFileNameOpen1);
582             break;
583         case IDC_BROWSE2:            //用户选择第二个文件
584             _OpenFile2(hWinMain, hText2, szFileNameOpen2);
585             break;
586         }
587         break;
588 
589     default:
590         return FALSE;
591     }
592 
593     return TRUE;
594 
595 
596 }
597 
598 
599 
600 /*
601 窗口程序
602 */
603 INT_PTR CALLBACK _ProcDlgMain(HWND hWnd, UINT wMsg, WPARAM wParam, LPARAM lParam)
604 {
605     switch (wMsg)
606     {
607     case WM_CLOSE:
608         EndDialog(hWnd, 0);
609         break;
610 
611     case WM_INITDIALOG:            //初始化
612         _Init(hWnd);
613         break;
614 
615     case WM_COMMAND:            //菜单
616         switch (LOWORD(wParam))
617         {
618         case IDM_EXIT:            //退出
619             EndDialog(hWnd, 0);
620             break;
621 
622         case IDM_OPEN:            //打开PE对比对话框
623             DialogBoxParam(hInstance, MAKEINTRESOURCE(RESULT_MODULE), hWnd,
624                 _resultProcMain, (LPARAM)hWnd);
625         case IDM_1:
626         case IDM_2:
627         case IDM_3:
628         default:
629             break;
630         }
631         break;
632 
633     default:
634         return FALSE;
635     }
636 
637     return TRUE;
638 }
639 
640 
641 
642 
643 int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
644 {
645     HMODULE hRichEdit;
646     
647     hInstance = hInst;
648     InitCommonControls();
649     hRichEdit = LoadLibrary(TEXT("RichEd20.dll"));
650     DialogBoxParam(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, _ProcDlgMain, (LPARAM)NULL);
651     FreeLibrary(hRichEdit);
652     return 0;
653 }

 

头文件 resource.h

 1 //{{NO_DEPENDENCIES}}
 2 // Microsoft Visual C++ 生成的包含文件。
 3 // 供 Resource.rc 使用
 4 //
 5 #define ICO_MAIN                        101
 6 #define IDM_MAIN                        103
 7 #define DLG_MAIN                        104
 8 #define RESULT_MODULE             105
 9 #define IDC_INFO                         1001
10 #define ID_TEXT1                         1002
11 #define ID_TEXT2                         1008
12 #define IDC_BROWSE1                 1003
13 #define ID_STATIC                        1007
14 #define ID_STATIC1                      1004
15 #define IDC_EDIT2                        1005
16 #define IDC_BROWSE2                 1006
17 #define IDC_MODULETABLE          1009
18 #define IDC_THESAME                   1010
19 #define IDC_BUTTON3                   1011
20 #define IDC_OK                             1011
21 #define ID_40002                         40002
22 #define ID_40003                         40003
23 #define ID_40004                         40004
24 #define ID_40005                         40005
25 #define ID_40006                         40006
26 #define IDM_OPEN                        40007
27 #define IDM_1                               40008
28 #define IDM_2                              40009
29 #define IDM_3                              40010
30 #define IDM_EXIT                        40011
31 #define ID_40012                        40012
32 #define ID_40013                        40013
33 #define ID_40014                        40014
34 #define ID_40015                        40015
35 #define IDM_4                           40016
36 
37 // Next default values for new objects
38 // 
39 #ifdef APSTUDIO_INVOKED
40 #ifndef APSTUDIO_READONLY_SYMBOLS
41 #define _APS_NEXT_RESOURCE_VALUE        106
42 #define _APS_NEXT_COMMAND_VALUE         40017
43 #define _APS_NEXT_CONTROL_VALUE         1012
44 #define _APS_NEXT_SYMED_VALUE           101
45 #endif
46 #endif

 

资源文件 resource.rc

  1 // Microsoft Visual C++ generated resource script.
  2 //
  3 #include "resource.h"
  4 
  5 #define APSTUDIO_READONLY_SYMBOLS
  6 /////////////////////////////////////////////////////////////////////////////
  7 //
  8 // Generated from the TEXTINCLUDE 2 resource.
  9 //
 10 #include "winres.h"
 11 
 12 /////////////////////////////////////////////////////////////////////////////
 13 #undef APSTUDIO_READONLY_SYMBOLS
 14 
 15 /////////////////////////////////////////////////////////////////////////////
 16 // 中文(简体,中国) resources
 17 
 18 #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_CHS)
 19 LANGUAGE LANG_CHINESE, SUBLANG_CHINESE_SIMPLIFIED
 20 
 21 #ifdef APSTUDIO_INVOKED
 22 /////////////////////////////////////////////////////////////////////////////
 23 //
 24 // TEXTINCLUDE
 25 //
 26 
 27 1 TEXTINCLUDE 
 28 BEGIN
 29     "resource.h\0"
 30 END
 31 
 32 2 TEXTINCLUDE 
 33 BEGIN
 34     "#include ""winres.h""\r\n"
 35     "\0"
 36 END
 37 
 38 3 TEXTINCLUDE 
 39 BEGIN
 40     "\r\n"
 41     "\0"
 42 END
 43 
 44 #endif    // APSTUDIO_INVOKED
 45 
 46 
 47 /////////////////////////////////////////////////////////////////////////////
 48 //
 49 // Icon
 50 //
 51 
 52 // Icon with lowest ID value placed first to ensure application icon
 53 // remains consistent on all systems.
 54 ICO_MAIN                ICON                    "main.ico"
 55 
 56 /////////////////////////////////////////////////////////////////////////////
 57 //
 58 // Menu
 59 //
 60 
 61 IDM_MAIN MENU
 62 BEGIN
 63     POPUP "文件(&F)"
 64     BEGIN
 65         MENUITEM "打开PE对比对话框",                   IDM_OPEN
 66         MENUITEM "---",                         IDM_1
 67         MENUITEM "---",                         IDM_2
 68         MENUITEM "---",                         IDM_3, CHECKED
 69         MENUITEM SEPARATOR
 70         MENUITEM "退出(&x)",                      IDM_EXIT
 71     END
 72     POPUP "编辑(&E)"
 73     BEGIN
 74         MENUITEM SEPARATOR
 75     END
 76     POPUP "格式(&O)"
 77     BEGIN
 78         MENUITEM SEPARATOR
 79     END
 80     POPUP "查看(&V)"
 81     BEGIN
 82         MENUITEM "源文件",                         IDM_1
 83         MENUITEM "窗口透明度",                       IDM_2
 84         MENUITEM SEPARATOR
 85         MENUITEM "大小",                          IDM_3
 86         MENUITEM "宽度",                          IDM_4
 87     END
 88     POPUP "帮助(&H)"
 89     BEGIN
 90         MENUITEM SEPARATOR
 91     END
 92 END
 93 
 94 
 95 /////////////////////////////////////////////////////////////////////////////
 96 //
 97 // Dialog
 98 //
 99 
100 DLG_MAIN DIALOG 50, 50, 544, 399
101 STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
102 CAPTION "PEComp"
103 MENU IDM_MAIN
104 FONT 9, "宋体"
105 BEGIN
106     CONTROL         "",IDC_INFO,"RichEdit20A",ES_MULTILINE | ES_AUTOVSCROLL | ES_AUTOHSCROLL | ES_READONLY | ES_WANTRETURN | WS_BORDER | WS_VSCROLL | WS_TABSTOP,0,0,540,396
107 END
108 
109 RESULT_MODULE DIALOGEX 76, 10, 455, 473
110 STYLE DS_SETFONT | DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
111 CAPTION "PE文件对比结果"
112 FONT 9, "宋体", 0, 0, 0x0
113 BEGIN
114     LTEXT           "您选定的第一个文件为:",ID_STATIC,10,10,89,8
115     EDITTEXT        ID_TEXT1,97,10,296,15
116     PUSHBUTTON      "浏览...",IDC_BROWSE1,394,10,50,14
117     LTEXT           "您选定的第二个文件为:",ID_STATIC1,10,33,89,8
118     EDITTEXT        ID_TEXT2,97,33,296,15
119     PUSHBUTTON      "浏览...",IDC_BROWSE2,394,33,50,14
120     CONTROL         "",IDC_MODULETABLE,"SysListView32",LVS_REPORT | LVS_SINGLESEL | LVS_SHOWSELALWAYS | WS_BORDER | WS_TABSTOP,7,60,438,390
121     CONTROL         "只显示不同的值",IDC_THESAME,"Button",BS_AUTOCHECKBOX | WS_TABSTOP,10,453,100,14
122     PUSHBUTTON      "执行...(&R)",IDC_OK,394,453,50,14
123 END
124 
125 
126 /////////////////////////////////////////////////////////////////////////////
127 //
128 // DESIGNINFO
129 //
130 
131 #ifdef APSTUDIO_INVOKED
132 GUIDELINES DESIGNINFO
133 BEGIN
134     DLG_MAIN, DIALOG
135     BEGIN
136         LEFTMARGIN, 7
137         RIGHTMARGIN, 302
138         TOPMARGIN, 7
139         BOTTOMMARGIN, 170
140     END
141 
142     RESULT_MODULE, DIALOG
143     BEGIN
144         LEFTMARGIN, 7
145         RIGHTMARGIN, 445
146         BOTTOMMARGIN, 467
147     END
148 END
149 #endif    // APSTUDIO_INVOKED
150 
151 #endif    // 中文(简体,中国) resources
152 /////////////////////////////////////////////////////////////////////////////
153 
154 
155 
156 #ifndef APSTUDIO_INVOKED
157 /////////////////////////////////////////////////////////////////////////////
158 //
159 // Generated from the TEXTINCLUDE 3 resource.
160 //
161 
162 
163 /////////////////////////////////////////////////////////////////////////////
164 #endif    // not APSTUDIO_INVOKED

 

posted on 2016-11-18 05:43  卧勒个槽  阅读(489)  评论(0编辑  收藏  举报