[11-22]MASM32编程获取文件语言、版本信息v3

本文介绍了一个使用MASM编写的程序,该程序能够读取并显示指定文件的版本信息,包括产品名称、版本号等详细数据。此外,还提供了语言和代码页的信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
;FileName:VerInfo3.asm
;Function:Demothewaytogetfile'sversioninformationandlanguage
;Author:PurpleEndurer
;
;LOG
;----------------------------------------------------------------------------------------
;2006-11-22CandisplayinCN
;2006-11-20Addedcomments!
;2006-09-14Created!
;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

.386
.modelflat,stdcall
optioncasemap:none
include/masm32/include/windows.inc

include/masm32/include/kernel32.inc
includelib/masm32/lib/kernel32.lib
include/masm32/include/user32.inc
includelib/masm32/lib/user32.lib
include/masm32/include/shell32.inc
includelib/masm32/lib/shell32.lib
include/masm32/include/comdlg32.inc
includelib/masm32/lib/comdlg32.lib
include/masm32/include/version.inc
includelib/masm32/lib/version.lib


WinMainPROTO:DWORD,:DWORD,:DWORD,:DWORD
ResizeConctrolPROTO
ShowVerInfoPROTO:LPSTR
btnShow_ClickPROTO
btnBrowse_ClickPROTO


;.const
IDC_BtnBrowseequ101
IDC_BtnShowequ103
IDC_EdtFileSpecequ105
IDC_EdtVerInfoequ107
c_BufLenEQUMAX_PATH


c_EditFileSpecLeftequ2
c_EditFileSpecTopequ5
c_EditFileSpecWidthequ300
c_EditFileSpecHeightequ20

c_BtnBrowseTopequc_EditFileSpecTop
c_BtnBrowseLeftequ(c_EditFileSpecLeft+c_EditFileSpecWidth+10)
c_BtnBrowseWidthequ70
c_BtnBrowseHeightequc_EditFileSpecHeight

c_BtnShowTopequc_EditFileSpecTop
c_BtnShowLeftequ(c_BtnBrowseLeft+c_BtnBrowseWidth+10)
c_BtnShowWidthequ70
c_BtnShowHeightequc_BtnBrowseHeight

c_EdtVerInfoLeftequ2
c_EdtVerInfoTopequ30
c_EdtVerInfoWidthequc_BtnShowLeft+c_BtnShowWidth+2
c_EdtVerInfoHeightequ165

c_WinWidthequc_EdtVerInfoLeft+c_EdtVerInfoWidth+10
c_WinHeightequc_EdtVerInfoTop+c_EdtVerInfoHeight+30

m_CatStrMACROszStr:REQ
invokeSendMessage,g_hEditVerInfo,EM_REPLACESEL,FALSE,ADDRszStr
ENDM

m_GoNextLineMACRO
invokeSendMessage,g_hEditVerInfo,EM_REPLACESEL,FALSE,ADDRg_szCR
ENDM


.data
g_szClsNamedb"FileVerInfoCls",0
g_szAppNamedb"文件版本信息",0
g_szEditClsdb"EDIT",0
g_szBtnClsdb"button",0
g_szBtnBrowseTextdb"&B浏览",0
g_szBtnShowTextdb"&S显示",0
g_szEnterFileErrdb"请先输入文件说明符",0
g_szFailGetVerSizedb"获取文件版本信息大小失败!",0
g_szFailAllocMemdb"申请内存失败!",0
g_szFailGetVerInfodb"获取文件版本信息失败!",0
g_szFailFailGetLangPagedb"获取语言和代码页失败!",0
g_szCRdb0dh,0ah,0
g_szOpdb"properties",0
g_szPeFileFilterdb"*.EXE;*.DLL",0,"*.EXE;*.DLL",0,0


.data?
g_hInstanceHINSTANCE?
g_hWndMainHANDLE?
g_hEditFileSpecHANDLE?
g_hBtnShowHANDLE?
g_hBtnBrowseHANDLE?
g_hEditVerInfoHANDLE?
g_szFileSpecdbc_BufLendup(?)
g_ofnOPENFILENAME<?>
g_ShlExecInfoSHELLEXECUTEINFO<?>

.code
start:
invokeGetModuleHandle,NULL
movg_hInstance,eax
invokeWinMain,g_hInstance,NULL,NULL,SW_SHOWDEFAULT
invokeExitProcess,eax


WinMainprochInst:DWORD,hPrevInst:DWORD,CmdLine:DWORD,CmdShow:DWORD
LOCALwc:WNDCLASSEX
LOCALmsg:MSG
LOCALhwnd:HWND

movwc.cbSize,SIZEOFWNDCLASSEX
movwc.style,CS_HREDRAWorCS_VREDRAW
movwc.lpfnWndProc,OFFSETWndProc
movwc.cbClsExtra,NULL
movwc.cbWndExtra,NULL
moveax,g_hInstance
movwc.hInstance,eax
movwc.hbrBackground,COLOR_APPWORKSPACE
movwc.lpszMenuName,NULL
movwc.lpszClassName,OFFSETg_szClsName
invokeLoadIcon,NULL,IDI_APPLICATION
movwc.hIcon,eax
movwc.hIconSm,eax
invokeLoadCursor,NULL,IDC_ARROW
movwc.hCursor,eax
invokeRegisterClassEx,addrwc
invokeCreateWindowEx,WS_EX_TOPMOST,ADDRg_szClsName,/
ADDRg_szAppName,WS_OVERLAPPEDWINDOW+WS_VISIBLE,/
CW_USEDEFAULT,CW_USEDEFAULT,c_WinWidth,c_WinHeight,/
NULL,NULL,hInst,NULL
movhwnd,eax
.whileTRUE
invokeGetMessage,ADDRmsg,NULL,0,0
.BREAK.IF(!eax)

;---processkeystrokesdirectlyinthemessageloop
.ifmsg.message==WM_SYSKEYUP
.ifmsg.wParam==VK_B;Alt+B
invokePostMessage,hwnd,WM_COMMAND,IDC_BtnBrowse,BM_CLICK
.elseifmsg.wParam==VK_S;Alt+S
invokePostMessage,hwnd,WM_COMMAND,IDC_BtnShow,BM_CLICK
.endif
.endif
;------------------------------------------------
invokeTranslateMessage,ADDRmsg
invokeDispatchMessage,ADDRmsg
.endw
moveax,msg.wParam
ret
WinMainendp


WndProcprochWnd:HWND,uMsg:UINT,wParam:WPARAM,lParam:LPARAM
LOCALrect:RECT
LOCALhdc:DWORD

.ifuMsg==WM_CREATE
moveax,hWnd
movg_hWndMain,eax

;---Createfilespeceditbox
invokeCreateWindowEx,NULL,offsetg_szEditCls,NULL,
WS_CHILD+WS_VISIBLE+ES_AUTOHSCROLL+WS_BORDER,/
c_EditFileSpecLeft,c_EditFileSpecTop,c_EditFileSpecWidth,c_EditFileSpecHeight,/
hWnd,IDC_EdtFileSpec,g_hInstance,NULL
movg_hEditFileSpec,eax

;---Createbrowsebutton
invokeCreateWindowEx,NULL,offsetg_szBtnCls,offsetg_szBtnBrowseText,/
WS_CHILD+WS_VISIBLE,c_BtnBrowseLeft,c_BtnBrowseTop,c_BtnBrowseWidth,c_BtnBrowseHeight,/
hWnd,IDC_BtnBrowse,g_hInstance,NULL
movg_hBtnBrowse,eax

;---Createshowbutton
invokeCreateWindowEx,NULL,offsetg_szBtnCls,offsetg_szBtnShowText,/
WS_CHILD+WS_VISIBLE,c_BtnShowLeft,c_BtnShowTop,c_BtnShowWidth,c_BtnShowHeight,/
g_hWndMain,IDC_BtnShow,g_hInstance,NULL
movg_hBtnShow,eax

;---Createfileverinfoeditbox
invokeCreateWindowEx,NULL,addrg_szEditCls,NULL,/
WS_CHILD+WS_VISIBLE+ES_MULTILINE+WS_HSCROLL+WS_VSCROLL+WS_BORDER,/
c_EdtVerInfoLeft,c_EdtVerInfoTop,c_EdtVerInfoWidth,c_EdtVerInfoHeight,/
g_hWndMain,IDC_EdtVerInfo,g_hInstance,NULL
movg_hEditVerInfo,eax

;---InitializeOPENFILENAMEstructure
invokeRtlZeroMemory,offsetg_ofn,sizeofg_ofn
movg_ofn.lStructSize,sizeofOPENFILENAME
moveax,g_hInstance
movg_ofn.hInstance,eax
movg_ofn.nMaxFile,c_BufLen
movg_ofn.Flags,OFN_HIDEREADONLY+OFN_FILEMUSTEXIST+OFN_PATHMUSTEXIST+OFN_EXPLORER
movg_ofn.lpstrFilter,OFFSETg_szPeFileFilter
movg_ofn.lpstrFile,OFFSETg_szFileSpec
moveax,g_hWndMain
movg_ofn.hWndOwner,eax

;---InitializeSHELLEXECUTEINFOstructure
invokeRtlZeroMemory,offsetg_ShlExecInfo,sizeofSHELLEXECUTEINFO
movg_ShlExecInfo.cbSize,sizeofg_ShlExecInfo
movg_ShlExecInfo.fMask,SEE_MASK_INVOKEIDLIST
movg_ShlExecInfo.lpVerb,offsetg_szOp
movg_ShlExecInfo.lpFile,offsetg_szFileSpec

.elseifuMsg==WM_COMMAND
.IFlParam
movedx,wParam
moveax,edx
shredx,16
.ifdx==BN_CLICKED
.IFax==IDC_BtnShow
invokebtnShow_Click
.else
invokebtnBrowse_Click
.endif
.ENDIF
.endif
.elseifuMsg==WM_DESTROY
invokePostQuitMessage,NULL
.elseifuMsg==WM_SIZE
invokeResizeConctrol
xoreax,eax
jz@F
.else
@@:
invokeDefWindowProc,hWnd,uMsg,wParam,lParam
ret
.endif
xoreax,eax
ret
WndProcendp


btnShow_Clickproc
invokeGetWindowText,g_hEditFileSpec,ADDRg_szFileSpec,SIZEOFg_szFileSpec
.IFeax==0
invokeMessageBox,g_hWndMain,OFFSETg_szEnterFileErr,OFFSETg_szAppName,MB_ICONERROR+MB_OK
.ELSE
;---clearthefileverinfobox
invokeSendMessage,g_hEditVerInfo,WM_SETTEXT,0,NULL
invokeShowVerInfo,OFFSETg_szFileSpec
.ifeax>0
.IFeax==1
moveax,OFFSETg_szFailGetVerSize
.ELSEIFeax==2
moveax,OFFSETg_szFailAllocMem
.ELSEIFeax==3
moveax,OFFSETg_szFailGetVerInfo
.ELSEIFeax==4
moveax,OFFSETg_szFailFailGetLangPage
.ENDIF
invokeMessageBox,g_hWndMain,eax,OFFSETg_szAppName,MB_ICONERROR+MB_OK
.else
invokeShellExecuteEx,offsetg_ShlExecInfo
.endif
.ENDIF
ret
btnShow_Clickendp


btnBrowse_Clickproc
invokeGetOpenFileName,ADDRg_ofn
.ifeax!=0
invokeSendMessage,g_hEditFileSpec,WM_SETTEXT,0,OFFSETg_szFileSpec
.endif
ret
btnBrowse_Clickendp


ResizeConctrolPROC
LOCALst_Rect:RECT

invokeGetClientRect,g_hWndMain,ADDRst_Rect

;---ResizetheShowbutton
moveax,st_Rect.right
subeax,5+c_BtnShowWidth
pusheax
invokeMoveWindow,g_hBtnShow,eax,c_BtnShowTop,c_BtnShowWidth,c_BtnShowHeight,TRUE

;---ResizetheShowbutton
popeax
subeax,5+c_BtnShowWidth
pusheax
invokeMoveWindow,g_hBtnBrowse,eax,c_BtnBrowseTop,c_BtnBrowseWidth,c_BtnBrowseHeight,TRUE

;---Resizethefilespeceditbox
popeax
subeax,10
invokeMoveWindow,g_hEditFileSpec,c_EditFileSpecLeft,c_EditFileSpecTop,eax,c_EditFileSpecHeight,TRUE

;---ResizetheVerinfoeditbox
moveax,st_Rect.right
subeax,5

movedi,st_Rect.bottom
subedi,30

invokeMoveWindow,g_hEditVerInfo,c_EdtVerInfoLeft,c_EdtVerInfoTop,eax,edi,TRUE

ret
ResizeConctrolENDP


;/////////////////////////////////
;Result:
;eax==0sucess
;eax==1Failtogetfileverinfosize
;eax==2Failtoallocglobalmemory
;eax==3Failtogetfileverinfo
;eax==4Failtogetthelanguageidandcodepage
;/////////////////////////////////
ShowVerInfoPROClpszFileSpec:LPSTR
LOCALdwBufSize:DWORD
LOCALhMem:HANDLE
LOCALbInfoStr[MAX_PATH]:byte
LOCALlpszValue:LPSTR
LOCALbLangCharset[9]:byte

invokeGetFileVersionInfoSize,lpszFileSpec,ADDRdwBufSize
oreax,eax
jnz@F
moveax,1
ret
@@:
movdwBufSize,eax
invokeGlobalAlloc,GMEM_ZEROINIT,eax
cmpeax,NULL
jnz@F
moveax,2
ret
@@:
movhMem,eax
;BOOLGetFileVersionInfo(
;LPTSTRlptstrFilename,//pointertofilenamestring
;DWORDdwHandle,//ignored
;DWORDdwLen,//sizeofbuffer
;LPVOIDlpData//pointertobuffertoreceivefile-versioninfo.
;);

invokeGetFileVersionInfo,lpszFileSpec,0,dwBufSize,hMem
oreax,eax
jnz@F
invokeGlobalFree,hMem
moveax,3
ret
g_szTranslationdb"/VarFileInfo/Translation",0
@@:
;BOOLVerQueryValue(
;constLPVOIDpBlock,//addressofbufferforversionresource
;LPTSTRlpSubBlock,//addressofvaluetoretrieve
;LPVOID*lplpBuffer,//addressofbufferforversionpointer
;PUINTpuLen//addressofversion-valuelengthbuffer
;);


invokeVerQueryValue,hMem,OFFSETg_szTranslation,ADDRlpszValue,ADDRdwBufSize
oreax,eax
jnz@F
invokeGlobalFree,hMem
moveax,4
ret
g_szProductNamedb'ProductName',0
g_szProductVersiondb'ProductVersion',0
g_szFileDescriptiondb'FileDescription',0
g_szLegalCopyrightdb'LegalCopyright',0
g_szFileVersiondb'FileVersion',0
g_szCompanyNamedb'CompanyName',0
g_szLegalTradeMarksdb'LegalTradeMarks',0
g_szInternalNamedb'InternalName',0
g_szOriginalFileNamedb'OriginalFileName',0
g_szCommentsdb"Comments",0

g_lpszInfolabeldword
dwordOFFSETg_szFileVersion
dwordOFFSETg_szFileDescription
dwordOFFSETg_szLegalCopyright
dwordOFFSETg_szComments
dwordOFFSETg_szProductVersion
dwordOFFSETg_szProductName
dwordOFFSETg_szCompanyName
dwordOFFSETg_szLegalTradeMarks
dwordOFFSETg_szInternalName
dwordOFFSETg_szOriginalFileName
c_lpszInfoLenequ($-g_lpszInfo)/4

c_language_cnequ1
ifc_language_cneq1
g_szProductName_cndb'产品名称',0
g_szProductVersion_cndb'产品版本',0
g_szFileDescription_cndb'说明',0
g_szLegalCopyright_cndb'版权',0
g_szFileVersion_cndb'文件版本',0
g_szCompanyName_cndb'公司名称',0
g_szLegalTradeMarks_cndb'合法商标',0
g_szInternalName_cndb'内部名称',0
g_szOriginalFileName_cndb'源文件名',0
g_szComments_cndb"备注",0

g_lpszInfo_cnlabeldword
dwordOFFSETg_szFileVersion_cn
dwordOFFSETg_szFileDescription_cn
dwordOFFSETg_szLegalCopyright_cn
dwordOFFSETg_szComments_cn
dwordOFFSETg_szProductVersion_cn
dwordOFFSETg_szProductName_cn
dwordOFFSETg_szCompanyName_cn
dwordOFFSETg_szLegalTradeMarks_cn
dwordOFFSETg_szInternalName_cn
dwordOFFSETg_szOriginalFileName_cn
c_lpszInfo_cn_Lenequ($-g_lpszInfo_cn)/4
g_szLang_cndb"语言"
g_szBlkColonBlkdb":",0
else
g_szLangdb"Langeage"
g_szBlkColonBlkdb":",0
endif
g_szPredb'StringFileInfo/',0;080404B0
g_szInfoFmtdb"%s%s/%s",0
g_szHexFmtdb"%08X",0

@@:
;---Now,lpszValueisapointertofour4bytesofHexnumber,
;firsttwobytesarelanguageid,andlasttwobytesarecodepage.
;However,Lang_Charset_Stringneedsastringof4hexdigits,
;thefirsttwocharacterscorrespondtothelanguageid
;andlasttwothelasttwocharactercorrespondtothecodepageid.

ifc_language_cneq1
m_CatStrg_szLang_cn
else
m_CatStrg_szLang
endif
moveax,lpszValue
moveax,[eax]
pusheax

;---Getthedescriptionstring
;DWORDVerLanguageName(
;DWORDwLang,//Microsoftlanguageidentifier
;LPTSTRszLang,//pointertobufferforlanguagedescriptionstring
;DWORDnSize//sizeofbuffer
;);
movzxeax,ax
movdwBufSize,eax
invokeVerLanguageName,dwBufSize,ADDRbInfoStr,MAX_PATH
oreax,eax
jz@F
m_CatStrbInfoStr
@@:
m_GoNextLine
popeax

;---Changetheorderofthelanguageidandcodepage
roleax,16

;---Convertthelanguageidandcodepageintoa8bytesHexstringrepresentation
invokewsprintf,ADDRbLangCharset,ADDRg_szHexFmt,eax

;---Forexample,thelanguageidandcodepagemaylooklike040904E4
;Ortopullitallapart:
;04------=SUBLANG_ENGLISH_USA
;--09----=LANG_ENGLISH
;----04E4=1252=CodepageforWindows:Multilingual

c_showTranslationequ0
ifc_showTranslationeq1
;---Showthelanguageidandcodepageineditbox
m_CatStrg_szTranslation

m_CatStrg_szBlkColonBlk
m_CatStrbLangCharset
m_GoNextLine
endif

xoreax,eax
.whileeax<c_lpszInfoLen
pusheax
oreax,eax
jz@F
shleax,2;eax<--eax*4
@@:
pusheax

ifc_language_cneq1
invokeSendMessage,g_hEditVerInfo,EM_REPLACESEL,FALSE,[eax+g_lpszInfo_cn]
else
invokeSendMessage,g_hEditVerInfo,EM_REPLACESEL,FALSE,[eax+g_lpszInfo]
endif

m_CatStrg_szBlkColonBlk
popeax
invokewsprintf,ADDRbInfoStr,ADDRg_szInfoFmt,OFFSETg_szPre,ADDRbLangCharset,[eax+g_lpszInfo]
invokeVerQueryValue,hMem,ADDRbInfoStr,ADDRlpszValue,ADDRdwBufSize
oreax,eax
jz@F
invokeSendMessage,g_hEditVerInfo,EM_REPLACESEL,FALSE,lpszValue
@@:
m_GoNextLine
popeax
inceax
.endw
invokeGlobalFree,hMem
xoreax,eax
ret
ShowVerInfoENDP


endstart
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值