Extracting filenames from the command line using assembly

Source: Internet
Author: User
Tags command line

In DOS there is such a command type filename, use this command to view the contents of the file, the file name to be processed directly after the program name, this usage is very common, still in Windows, but you may not feel it. Double-click a text file, the system will open the file with Notepad, in fact, you double-click the file name as a Notepad parameters to deal with.

Therefore, as a program under Windows, you can get the file name you want to process from the command line, and if not on the command line, then show the Open File dialog box to let the user choose a file is not too late!

The following example extracts the name of the file to be processed from the command line, assuming that the filename is the first parameter after the program name.


----------------------------------------------------------
; File name: 15.asm
; Take the command-line arguments and display to analyze what might happen to the command line arguments
.386
. Model Flat,stdcall
Option Casemap:none
Include Windows.inc
Include Kernel32.inc
Include User32.inc
Include Masm32.inc
Includelib Kernel32.lib
Includelib User32.lib
Includelib Masm32.lib
. Data
Szcaption db "GetCommandLine", 0
. Data?
FileName DB 256 dup (?)
. Code
Getfilenamefromcommandline proto:lpstr; Used to store the processed file name
Start
Invoke Getfilenamefromcommandline,addr FileName
. If Eax!=null
Invoke Messagebox,null,eax,addr SZCAPTION,MB_OK
. endif
Invoke Exitprocess,null
; If eax=null indicates that the processed file cannot be extracted from the command line
; otherwise eax point to the filename address
getfilenamefromcommandline proc uses ESI EDI, LPSTRING:LPSTR
MOV edi,lpstring
Invoke getcommandline; Fetch command line arguments
MOV esi,eax
XOR Eax,eax
Cld
; Skips the program name entry, which may be in the middle of a double quote
G1:
Lodsb
CMP al, ' "'; program name starts with double quotes?
JNZ G3
G2:
Lodsb
CMP al, ' "'; quotes indicate the end of a program name entry
JNZ G2
G3:
LODSB, there is at least one blank between the program name entry and the subsequent arguments
CMP al, '
JNZ G3
; Skip whitespace and tables, point to file name
G4:
Lodsb
CMP al,0
JZ G7
CMP al, '
JZ G4
CMP al,9
JZ G4
CMP al, ' "'; does the filename start with double quotes?
JNZ G5
Stosb
G4P:
Lodsb
Stosb
CMP al, ' '; Find the quote is the end of the filename
JNZ g4p
JMP G6
; Skip to end of file
G5:
STOSB; transfer file name to buffer
Lodsb
CMP al,0
JZ G6
CMP al, '
JZ G6
CMP al,9
JNZ G5
G6:
XOR al,al in buffer file name end
Stosb
mov eax,lpstring; eax point to Buffer head address
G7:
Ret
Getfilenamefromcommandline ENDP
End Start

-----------------------------------------------------------------

To execute the example:

D:\kz_z>getfilename "\my Documents\pop.exe"

D:\kz_z>getfilename Cala.exe

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.