|
我想这些代码对初学SDK的同学一定有很大帮助,写文件用的是文件映射,这样便于大文件的操作。考虑到代码和z叔的有很大不同,还是放出来了……
这个是运行效果图,使用方法为填写好区段名把程序拽上去即可……
- BOOL CALLBACK DialogProc(HWND hDlg, UINT message,WPARAM wParam, LPARAM lParam)
- {
- char strPath[MAX_PATH] ={NULL};
- switch(message)
- {
- case WM_INITDIALOG:
- SendMessage(hDlg,WM_SETICON,1,(LONG)LoadIcon(GetModuleHandle(NULL),MAKEINTRESOURCE(IDI_MAIN)));
- SetDlgItemText(hDlg,IDC_Info," Ready.");
- break;
- case WM_DROPFILES:
- DragQueryFile((HDROP)wParam,0,strPath,MAX_PATH);
- DragFinish((HDROP)wParam);
- HANDLE hFile;
- hFile= CreateFile(strPath,GENERIC_READ|GENERIC_WRITE,
- 0,0,OPEN_EXISTING,
- FILE_ATTRIBUTE_NORMAL,0);
- if (hFile == INVALID_HANDLE_VALUE)
- {
- MessageBox(NULL,"打开文件错误!","错误",MB_ICONERROR);
- return false;
- }
- HANDLE hFileMap;
- LPVOID lpFile;
- PIMAGE_DOS_HEADER pDH;
- PIMAGE_NT_HEADERS pNtH;
- pNtH=NULL;
- pDH=NULL;
- lpFile=hFileMap=NULL;
- hFileMap= CreateFileMapping(hFile,NULL,PAGE_READWRITE,0,0,NULL);
- lpFile= MapViewOfFile(hFileMap,FILE_MAP_ALL_ACCESS,NULL,NULL,0x200);
- if (!lpFile)
- {
- return false;
- }
- pDH = (PIMAGE_DOS_HEADER)lpFile;
- if (pDH->e_magic!=IMAGE_DOS_SIGNATURE)
- {
- return false;
- }
- pNtH = (PIMAGE_NT_HEADERS32)((DWORD)pDH+pDH->e_lfanew);
- if (pNtH->Signature!=IMAGE_NT_SIGNATURE)
- {
- return false;
- }
- int iSection;
- iSection= pNtH->FileHeader.NumberOfSections;
- char SectionName[9];
- GetDlgItemText(hDlg,IDC_EDIT1,SectionName,8);
- PIMAGE_SECTION_HEADER pSH;
- pSH= IMAGE_FIRST_SECTION(pNtH);
- int i;
- for (i=0;i<iSection;i++)
- {
- memcpy(pSH,SectionName,8);
- pSH++;
- }
- UnmapViewOfFile(lpFile);
- CloseHandle(hFileMap);
- CloseHandle(hFile);
- SetDlgItemText(hDlg,IDC_Info," Job Done!");
- case WM_COMMAND:
- // itoa(wParam,strPath,10);
- // MessageBox(NULL,strPath,"",NULL);
- switch(LOWORD(wParam))
- {
- case IDExit:
- case WM_DESTROY:
- EndDialog(hDlg,0);
- return true;
- }
- }
- return false;
- }
|
|