用API中的FindFirstFile和FindNextFile遍历文件夹,然后用GetFileSize取得各个文件大小,累加,即可得出……
FindFile.ASM>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.386
.model flat, stdcall
option casemap :none
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Include 文件定义
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
include windows.inc
include user32.inc
includelib user32.lib
include kernel32.inc
includelib kernel32.lib
include ole32.inc
includelib ole32.lib
include shell32.inc
includelib shell32.lib
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Equ 等值定义
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN equ 1000
DLG_MAIN equ 100
IDC_PATH equ 101
IDC_BROWSE equ 102
IDC_NOWFILE equ 103
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
数据段
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
hInstance dd ?
hWinMain dd ?
dwFileSizeHigh dd ?
dwFileSizeLow dd ?
dwFileCount dd ?
dwFolderCount dd ?
szPath db MAX_PATH dup (?)
dwOption db ?
F_SEARCHING equ 0001h
F_STOP equ 0002h
.const
szStart db '开始(&S)',0
szStop db '停止(&S)',0
szFilter db '*.*',0
szSearchInfo db '共找到 %d 个文件夹,%d 个文件,共 %luK 字节',0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
代码段
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
include _BrowseFolder.asm
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
处理找到的文件
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcessFile proc _lpszFile
local @hFile
inc dwFileCount
invoke SetDlgItemText,hWinMain,IDC_NOWFILE,_lpszFile
invoke CreateFile,_lpszFile,GENERIC_READ,FILE_SHARE_READ,0,\
OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,0
.if eax != INVALID_HANDLE_VALUE
mov @hFile,eax
invoke GetFileSize,eax,NULL
add dwFileSizeLow,eax
adc dwFileSizeHigh,0
invoke CloseHandle,@hFile
.endif
ret
_ProcessFile endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_FindFile proc _lpszPath
local @stFindFile:WIN32_FIND_DATA
local @hFindFile
local @szPath[MAX_PATH]:byte 用来存放“路径\”
local @szSearch[MAX_PATH]:byte 用来存放“路径\*.*”
local @szFindFile[MAX_PATH]:byte 用来存放“路径\找到的文件”
pushad
invoke lstrcpy,addr @szPath,_lpszPath
********************************************************************
在路径后面加上\*.*
********************************************************************
@@:
invoke lstrlen,addr @szPath
lea esi,@szPath
add esi,eax
xor eax,eax
mov al,'\'
.if byte ptr [esi-1] != al
mov word ptr [esi],ax
.endif
invoke lstrcpy,addr @szSearch,addr @szPath
invoke lstrcat,addr @szSearch,addr szFilter
********************************************************************
寻找文件
********************************************************************
invoke FindFirstFile,addr @szSearch,addr @stFindFile
.if eax != INVALID_HANDLE_VALUE
mov @hFindFile,eax
.repeat
invoke lstrcpy,addr @szFindFile,addr @szPath
invoke lstrcat,addr @szFindFile,addr @stFindFile.cFileName
.if @stFindFile.dwFileAttributes &FILE_ATTRIBUTE_DIRECTORY
.if @stFindFile.cFileName != '.'
inc dwFolderCount
invoke _FindFile,addr @szFindFile
.endif
.else
invoke _ProcessFile,addr @szFindFile
.endif
invoke FindNextFile,@hFindFile,addr @stFindFile
.until (eax == FALSE) || (dwOption &F_STOP)
invoke FindClose,@hFindFile
.endif
popad
ret
_FindFile endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcThread proc uses ebx ecx edx esi edi,lParam
local @szBuffer[256]:byte
********************************************************************
设置标志位,并灰化“浏览”按钮和路径输入栏
********************************************************************
and dwOption,not F_STOP
or dwOption,F_SEARCHING
invoke GetDlgItem,hWinMain,IDC_PATH
invoke EnableWindow,eax,FALSE
invoke GetDlgItem,hWinMain,IDC_BROWSE
invoke EnableWindow,eax,FALSE
invoke SetDlgItemText,hWinMain,IDOK,addr szStop
xor eax,eax
mov dwFileSizeHigh,eax
mov dwFileSizeLow,eax
mov dwFileCount,eax
mov dwFolderCount,eax
invoke _FindFile,addr szPath
********************************************************************
退出时显示找到文件的总大小
********************************************************************
mov edx,dwFileSizeHigh
mov eax,dwFileSizeLow
mov ecx,1000
div ecx
invoke wsprintf,addr @szBuffer,addr szSearchInfo,dwFolderCount,dwFileCount,eax
invoke SetDlgItemText,hWinMain,IDC_NOWFILE,addr @szBuffer
********************************************************************
设置标志位,并启用“浏览”按钮和路径输入栏
********************************************************************
invoke GetDlgItem,hWinMain,IDC_BROWSE
invoke EnableWindow,eax,TRUE
invoke GetDlgItem,hWinMain,IDC_PATH
invoke EnableWindow,eax,TRUE
invoke SetDlgItemText,hWinMain,IDOK,addr szStart
invoke SetDlgItemText,hWinMain,IDC_PATH,addr szPath
and dwOption,not F_SEARCHING
ret
_ProcThread endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_ProcDlgMain proc uses ebx edi esi hWnd,wMsg,wParam,lParam
local @dwTemp,@szBuffer[MAX_PATH]:byte
mov eax,wMsg
.if eax == WM_CLOSE
.if ! (dwOption &F_SEARCHING)
invoke EndDialog,hWnd,NULL
.endif
********************************************************************
.elseif eax == WM_INITDIALOG
push hWnd
pop hWinMain
invoke LoadIcon,hInstance,ICO_MAIN
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,eax
invoke SendDlgItemMessage,hWnd,IDC_PATH,EM_SETLIMITTEXT,MAX_PATH,0
********************************************************************
.elseif eax == WM_COMMAND
mov eax,wParam
.if ax == IDC_BROWSE
invoke _BrowseFolder,hWnd,addr szPath
.if eax
invoke SetDlgItemText,hWnd,IDC_PATH,addr szPath
.endif
.elseif ax == IDC_PATH
invoke GetDlgItemText,hWnd,IDC_PATH,addr @szBuffer,MAX_PATH
mov ebx,eax
invoke GetDlgItem,hWnd,IDOK
invoke EnableWindow,eax,ebx
********************************************************************
按下开始按钮,如果在寻找中则设置停止标志
如果没有开始寻找则建立一个寻找文件的线程
********************************************************************
.elseif ax == IDOK
.if dwOption &F_SEARCHING
or dwOption,F_STOP
.else
invoke GetDlgItemText,hWnd,IDC_PATH,addr szPath,MAX_PATH
invoke CreateThread,NULL,0,offset _ProcThread,NULL,\
NULL,addr @dwTemp
invoke CloseHandle,eax
.endif
.endif
********************************************************************
.else
mov eax,FALSE
ret
.endif
mov eax,TRUE
ret
_ProcDlgMain endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
start:
invoke GetModuleHandle,NULL
mov hInstance,eax
invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
invoke ExitProcess,NULL
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
end start
FindFile.RC
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#include <resource.h>
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
#define ICO_MAIN 1000
#define DLG_MAIN 100
#define IDC_PATH 101
#define IDC_BROWSE 102
#define IDC_NOWFILE 103
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN ICON "Main.ico"
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DLG_MAIN DIALOG 141, 208, 344, 38
STYLE DS_MODALFRAME | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU
CAPTION "全盘文件搜索"
FONT 9, "宋体"
{
EDITTEXT IDC_PATH, 43, 5, 211, 12, ES_AUTOHSCROLL | WS_BORDER | WS_TABSTOP
PUSHBUTTON "浏览(&B)", IDC_BROWSE, 259, 4, 40, 14
PUSHBUTTON "开始(&S)", IDOK, 299, 4, 40, 14, WS_DISABLED | WS_TABSTOP
LTEXT "起始目录", -1, 7, 8, 35, 8
LTEXT "", IDC_NOWFILE, 7, 24, 330, 8
}
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_BrowseFolder.asm
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
定义几个基本的 COM 接口
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IUnknown interface
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
externdefIID_IUnknown:IID
LPUNKNOWNtypedef DWORD
LPPUNKNOWN typedef ptr LPUNKNOWN
IUnknown_QueryInterfaceProto typedef proto :DWORD, :DWORD, :DWORD
IUnknown_AddRefProto typedef proto :DWORD
IUnknown_ReleasePrototypedef proto :DWORD
IUnknown_QueryInterface typedef ptr IUnknown_QueryInterfaceProto
IUnknown_AddRef typedef ptr IUnknown_AddRefProto
IUnknown_Release typedef ptr IUnknown_ReleaseProto
IUnknown struct DWORD
QueryInterfaceIUnknown_QueryInterface ?
AddRefIUnknown_AddRef ?
Release IUnknown_Release ?
IUnknown ends
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
IMalloc Interface
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
externdefIID_IMalloc:IID
LPMALLOC typedef DWORD
LPPMALLOCtypedef ptr LPMALLOC
IMalloc_AllocProto typedef proto :DWORD, :DWORD
IMalloc_ReallocProto typedef proto :DWORD, :DWORD, :DWORD
IMalloc_FreePrototypedef proto :DWORD, :DWORD
IMalloc_GetSizeProto typedef proto :DWORD, :DWORD
IMalloc_DidAllocPrototypedef proto :DWORD, :DWORD
IMalloc_HeapMinimizePrototypedef proto :DWORD
IMalloc_Alloctypedef ptr IMalloc_AllocProto
IMalloc_Realloc typedef ptr IMalloc_ReallocProto
IMalloc_Free typedef ptr IMalloc_FreeProto
IMalloc_GetSize typedef ptr IMalloc_GetSizeProto
IMalloc_DidAlloc typedef ptr IMalloc_DidAllocProto
IMalloc_HeapMinimize typedef ptr IMalloc_HeapMinimizeProto
IMalloc struct DWORD
QueryInterfaceIUnknown_QueryInterface ?
AddRefIUnknown_AddRef ?
Release IUnknown_Release ?
Alloc IMalloc_Alloc?
Realloc IMalloc_Realloc ?
Free IMalloc_Free ?
GetSize IMalloc_GetSize ?
DidAlloc IMalloc_DidAlloc ?
HeapMinimize IMalloc_HeapMinimize ?
IMalloc ends
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.data?
_BrowseFolderTmp dd ?
.const
_szDirInfo db '请选择目录:',0
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
.code
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
d出选择目录的对话框
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_BrowseFolderCallBack proc hWnd,uMsg,lParam,lpData
local @szBuffer[260]:byte
mov eax,uMsg
.if eax == BFFM_INITIALIZED
invoke SendMessage,hWnd,BFFM_SETSELECTION,TRUE,_BrowseFolderTmp
.elseif eax == BFFM_SELCHANGED
invoke SHGetPathFromIDList,lParam,addr @szBuffer
invoke SendMessage,hWnd,BFFM_SETSTATUSTEXT,0,addr @szBuffer
.endif
xor eax,eax
ret
_BrowseFolderCallBack endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
_BrowseFolder proc _hWnd,_lpszBuffer
local @stBrowseInfo:BROWSEINFO
local @stMalloc
local @pidlParent,@dwReturn
pushad
invoke CoInitialize,NULL
invoke SHGetMalloc,addr @stMalloc
.if eax == E_FAIL
mov @dwReturn,FALSE
jmp @F
.endif
invoke RtlZeroMemory,addr @stBrowseInfo,sizeof @stBrowseInfo
********************************************************************
SHBrowseForFolder 选择一个目录,把不含路径的目录名放入
stBrowseInfo.pszDisplayName 中,SHGetPathFromIDList 把
stBrowseInfo.pszDisplayName 转换成含全部路径的目录名
********************************************************************
push _hWnd
pop @stBrowseInfo.hwndOwner
push _lpszBuffer
pop _BrowseFolderTmp
mov @stBrowseInfo.lpfn,offset _BrowseFolderCallBack
mov @stBrowseInfo.lpszTitle,offset _szDirInfo
mov @stBrowseInfo.ulFlags,BIF_RETURNONLYFSDIRS or BIF_STATUSTEXT
invoke SHBrowseForFolder,addr @stBrowseInfo
mov @pidlParent,eax
.if eax != NULL
invoke SHGetPathFromIDList,eax,_lpszBuffer
mov eax,TRUE
.else
mov eax,FALSE
.endif
mov @dwReturn,eax
mov eax,@stMalloc
mov eax,[eax]
invoke (IMalloc PTR [eax]).Free,@stMalloc,@pidlParent
mov eax,@stMalloc
mov eax,[eax]
invoke (IMalloc PTR [eax]).Release,@stMalloc
@@:
invoke CoUninitialize
popad
mov eax,@dwReturn
ret
_BrowseFolder endp
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
Sample code for <Win32ASM Programming 2nd Edition>
by 罗云彬
用MasmPuls 编译、连接
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)