If the filename cannot be obtained from the command-line arguments, we can use a common dialog box (Open File dialog) to ask the user to select a file to obtain the file object to manipulate.
---------------------------------------------------------------------
; file name: 16.asm, displaying an open dialog box
.386
. Model Flat, StdCall
Option Casemap:none
Include Windows.inc
Include Kernel32.inc
Include User32.inc
Include Comdlg32.inc
Includelib Kernel32.lib
Includelib User32.lib
Includelib Comdlg32.lib
. Data
Ofn openfilename <0> Open File dialog box to use this structure
szFileName DB 256 dup (0)
Szfilterstring db ' executable file (*.exe) ', 0, ' *.exe ', 0,0; file filter string
Szmytitle db ' Please select the file to open ', 0
Szmessagetitle db ' The file you selected is ', 0
. Code
Getfilenamefromdialog proc
mov ofn.lstructsize,sizeof ofn; size of structure
mov ofn.lpstrfilter,offset szfilterstring; file filter
mov ofn.lpstrfile,offset szfilename; location of file name
mov ofn.nmaxfile,256; maximum length of file name
mov ofn. Flags,ofn_filemustexist or Ofn_hidereadonly or ofn_longnames
mov ofn.lpstrtitle,offset szmytitle; " Open the title of the dialog box
Invoke Getopenfilename,addr ofn; Show Open dialog box
Ret
Getfilenamefromdialog ENDP
Start:
Call Getfilenamefromdialog
. if eax!=0; If you choose to have a file, display it
Invoke Messageboxa,null,addr szfilename,addr Szmessagetitle,null
. endif
Invoke Exitprocess,null; End Program
End Start
--------------------------------------------------------------------------
The interface of the program at run time:
The display after selecting a file