要获取一个文件的长度,我们可以使用API函数 GetFileSize()。
对于PE格式的文件,我们还可以利用PE文件头中的信息来获取文件长度,方法是:optional header中的 SizeOfHeaders值 + 所有节表中SizeOfRawData值 = 文件长度
PE文件格式说明可参考:Iczelion的PE教程
http://www.google.cn/search?complete=1&hl=zh-CN&newwindow=1&q=Iczelion%E7%9A%84PE%E6%95%99%E7%A8%8B&btnG=Google+%E6%90%9C%E7%B4%A2&meta=cr%3DcountryCN&aq=f
下面的代码也是在PE教程中的演示代码上修改而来。
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;FileName:FileSize.asm
;Function:SelectaPEfileanddisplayit'ssizewithit'sPEfileheadinfo
;Author:PupleEndurer
;
;log
;-----------------------------------------------
;2008-04-09Created!
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
.386
.modelflat,stdcall
optioncasemap:none
include/masm32/include/windows.inc
include/masm32/include/kernel32.inc
include/masm32/include/comdlg32.inc
include/masm32/include/user32.inc
includelib/masm32/lib/user32.lib
includelib/masm32/lib/kernel32.lib
includelib/masm32/lib/comdlg32.lib
d_UseSEHequ0
SEHstruct
PrevLinkdd?;theaddressoftheprevioussehstructure
CurrentHandlerdd?;theaddressoftheexceptionhandler
SafeOffsetdd?;Theoffsetwhereit'ssafetocontinueexecution
PrevEspdd?;theoldvalueinesp
PrevEbpdd?;Theoldvalueinebp
SEHends
GetPeFileSizeproto:LPSTR
IsPeFileMapproto:DWORD
CountSectionSizeproto:dword,:dword
CountPeFileSizeproto:HANDLE
.data
g_szAppNamedb"PEFileSize",0
g_stOfnOPENFILENAME<>
g_szFilterStringdb"*.exe,*.dll",0,"*.exe;*.dll",0
db"*.*",0,"*.*",0,0
g_szFileOpenErrordb"未能打开文件以读",0
g_szFileOpenMappingErrordb"未能打开文件用于内存映射",0
g_szFileMappingErrordb"未能映射文件到内存",0
g_szFileInValidPEdb"非"
g_szFileValidPEdb"有效PE文件",0
g_szTmpFileSizedb"文件长度为%d字节",0
;.data?
g_buffer512db512dup(?)
g_dwValidPEdd?
.code
startproc
movg_stOfn.lStructSize,SIZEOFg_stOfn
movg_stOfn.lpstrFilter,OFFSETg_szFilterString
movg_stOfn.lpstrFile,OFFSETg_buffer512
movg_stOfn.nMaxFile,512
movg_stOfn.Flags,OFN_FILEMUSTEXISTorOFN_PATHMUSTEXISTorOFN_LONGNAMESorOFN_EXPLORERorOFN_HIDEREADONLY
invokeGetOpenFileName,ADDRg_stOfn
.ifeax==TRUE
invokeGetPeFileSize,OFFSETg_buffer512
invokeMessageBox,0,eax,addrg_szAppName,MB_OK
.endif
invokeExitProcess,0
startendp
GetPeFileSizeproclpszFileSpec:LPSTR
localhFile,hMapping,pMapping:dword
invokeCreateFile,lpszFileSpec,GENERIC_READ,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL
.ifeax!=INVALID_HANDLE_VALUE
movhFile,eax
invokeCreateFileMapping,hFile,NULL,PAGE_READONLY,0,0,0
.ifeax!=NULL
movhMapping,eax
invokeMapViewOfFile,hMapping,FILE_MAP_READ,0,0,0
.ifeax!=NULL
movpMapping,eax
invokeIsPeFileMap,eax
.ifg_dwValidPE==TRUE
invokeCountPeFileSize,pMapping
invokewsprintf,addrg_buffer512,addrg_szTmpFileSize,eax
;invokeMessageBox,NULL,addrg_buffer512,addrg_buffer512,NULL
pushoffsetg_buffer512;g_szFileValidPE
.else
pushoffsetg_szFileInValidPE
.endif
.else
pushoffsetg_szFileMappingError
.endif
invokeCloseHandle,hMapping
.else
pushoffsetg_szFileOpenMappingError
.endif
invokeCloseHandle,hFile
popeax
.else
moveax,offsetg_szFileOpenError
.endif
ret
GetPeFileSizeendp
IsPeFileMapprocpMapping:DWORD
ifd_UseSEHeq1
localseh:SEH
endif;d_UseSEH
movg_dwValidPE,FALSE
movedi,pMapping
ifd_UseSEHeq1
assumefs:nothing
pushfs:[0]
popseh.PrevLink
movseh.CurrentHandler,offsetSEHHandler
movseh.SafeOffset,offsetFinalExit
leaeax,seh
movfs:[0],eax
movseh.PrevEsp,esp
movseh.PrevEbp,ebp
endif;d_UseSEH
assumeedi:ptrIMAGE_DOS_HEADER
.if[edi].e_magic==IMAGE_DOS_SIGNATURE
addedi,(IMAGE_DOS_HEADERptr[edi]).e_lfanew;addedi,[edi].e_lfanew
assumeedi:ptrIMAGE_NT_HEADERS
.if[edi].Signature==IMAGE_NT_SIGNATURE
movg_dwValidPE,TRUE
.endif
.endif
FinalExit:
ret
IsPeFileMapendp
;Input:dwNumberOfSections--numberofsections
;pSectionTabBeginAddr--thebegionaddressofthefirstsectiontable
;Output:eax=allsectionssize
CountSectionSizeprocdwNumberOfSections:dword,pSectionTabBeginAddr:dword
movedi,dwNumberOfSections
movesi,pSectionTabBeginAddr
xoreax,eax
.while(edi>0)
addeax,(IMAGE_SECTION_HEADERptr[esi]).SizeOfRawData
decedi
addesi,sizeofIMAGE_SECTION_HEADER
.endw
ret
CountSectionSizeendp
;Input:pMapping--thepointertopefilemapping
;Output:eax=filesize
CountPeFileSizeprocpMapping:HANDLE
movedi,pMapping
addedi,(IMAGE_DOS_HEADERptr[edi]).e_lfanew
movzxeax,(IMAGE_NT_HEADERSptr[edi]).FileHeader.NumberOfSections
testeax,eax
.if!ZERO?
pushedi
addedi,sizeofIMAGE_NT_HEADERS
invokeCountSectionSize,eax,edi
popedi
.endif
addeax,(IMAGE_NT_HEADERSptr[edi]).OptionalHeader.SizeOfHeaders
ret
CountPeFileSizeendp
ifd_UseSEHeq1
SEHHandlerprocusesedxpExcept:DWORD,pFrame:DWORD,pContext:DWORD,pDispatch:DWORD
movedx,pFrame
assumeedx:ptrSEH
moveax,pContext
assumeeax:ptrCONTEXT
push[edx].SafeOffset
pop[eax].regEip
push[edx].PrevEsp
pop[eax].regEsp
push[edx].PrevEbp
pop[eax].regEbp
movg_dwValidPE,FALSE
moveax,ExceptionContinueExecution
ret
SEHHandlerendp
endif;d_UseSEH
endstart
分享到:
相关推荐
masm32安装时,如果是64位系统,会提示缺少几个lib文件,如kernel32.lib,user32.lib等
上回用MASM32编程使用了Windows系统提供的API函数:NetScheduleJobEnum()来枚举Windows计划任务(详见 MASM32编程枚举Windows计划任务,http://blog.csdn.net/Purpleendurer/archive/2009/11/05/4774148.aspx),...
Masm32使用教程详细描述了Masm32基本使用方法、思路,本人认为较好
Win7 64位系统下安装好的MASM32 SDK文件
Masm32 库文件,Masm32 库文件,Masm32 库文件,Masm32 库文件
Masm32使用指南,主要介绍、说明32位汇编环境的建立和使用。
MASM32教程MASM32教程MASM32教程MASM32教程MASM32教程MASM32教程MASM32教程
include \masm32\include\masm32.inc includelib \masm32\lib\kernel32.lib includelib \masm32\lib\masm32.lib .data HelloWorld db "Hello World!", 0 .code start: invoke StdOut, addr HelloWorld invoke ...
详尽地讲解如何构建Win32汇编的编程环境
可以把编译链接过程写成批命令文件,以减少键盘输入量。例如: MLEXE.BAT ML /Zi /c /Fl /coff %1.asm LINK /subsystem:console %1.obj del %1.obj dir %1.* 如果要编译链接e.asm,则只需输入: MLEXE e 回车
MASM32编程实现运行时自动提示要求以管理员帐户来运行的演示源代码和exe。详细信息请移步:http://blog.csdn.net/purpleendurer/article/details/54099224
学习汇编的助手 汇编的小工具 masm32v8编程工具
masm32 SDK 软件包,一些工具的集合,很棒!
masm32sdk中的user32库文件,缺什么库文件都可以上网找,正在看罗云彬的win32汇编.
::将此段代码复制到记事本,存为**.bat,将set Masm32Dir=后面的地址改为你的Masm32地址 ::使用方法:将.asm源文件拖到此批处理文件上,在源文件目录生成.exe文件 ::错误提示: ::1.Masm32编译器地址存在同名程序 ::...
MASM32是面向WIN32程序开发的,MASM611是面向DOS的,MASM32不使用中断调用了,而是使用微软的应用程序开发接口API和其他高级开发语言差不多了。而MASM611才是使用中断。 解压后大约15M 而压缩后才3.4M 分享学习的...
MASM32编程通过WMI获取BIOS版本和序列号,主板序列号,硬盘序列号,CPU ID,网卡MAC(源代码+exe)
MASM32编程枚举通过At命令创建的Windows计划任务,输出任务信息
MASM32 Editor
MASM32是不同工具软件的大集合,它的汇编编译器用的是MASM软件包中的Ml.exe,资源编译器和32位链接器用的是Microsoft Visual Studio中的Rc.exe和Link.exe,同时包含了Microsoft Visual Studio中的其他一些工具,如...