Windows 95 长文件名的使用


概述:

     Windows 95 的长文件名给我们带来了很大的方便,本文讲述有关长文件名的使用方法。
    长文件名的使用涉及到几个中断,在使用中,原先 DOS 中 INT 21H 的中断中参数有 ASC 文件名的地方,都有一个新的中断对应,如 INT 21H 的 3DH,6CH 对应 716CH (打开/建立文件),56H 对应 7156H(文件改名),41H 对应 7141H(文件删除)等等,其他参数中没有 ASC 文件名的,则使用原来的中断,如 3EH,3FH,40H (关闭/读/写)等等,一般编程中先检测系统是否支持长文件名,如果支持,则用新功能打开文件,再用普通的 3EH,3FH,40H 等对文件进行操作。本文中有几个例子,是用长文件名建立一个文件,写入一段文字,再打开文件、改名,然后删除,大家可以对应中断说明分析一下。
    本程序要用到的 INT 21H 中断的 71xxH 功能如下:

INT 21H 中断的 716CH 功能:(打开或建立文件)

输入参数:

    AX = 716Ch
    BX = 存取模式和共享标志

    Bit(s)    Description
    2-0    file access mode
        000 read-only
        001 write-only
        010 read-write
        100 read-only, do not modify file's last-access time
    6-4    file sharing modes
    7    no-inherit flag
    8    do not buffer data (requires that all reads/writes be exact physical
        sectors)
    9    do not compress file even if volume normally compresses files
    10    use alias hint in DI as numeric tail for short-name alias
    12-11    unused??? (0)
    13    return error code instead of generating INT 24h if critical error

    CX = 文件属性
    DX = 执行方式
    Bit(s)    Description    (Table 01758)
    0    open file (fail if file does not exist)
    1    truncate file if it already exists (fail if file does not exist)
    4    create new file if file does not already exist (fail if exists)
    Note:    the only valid combinations of multiple flags are bits 4&0 and 4&1

    DS:SI -> ASC 码文件名
    DI = 转化成短文件名后面加的编号

返回参数:CF 清除则成功

    AX = file handle
    CX = action taken
        0001h file opened
        0002h file created
        0003h file replaced

                    CF 置位则失败
        AX = 7100H 则表示功能不被支持
返回参数:CF 清除则成功


INT 21H 中断的 7156H 功能:(文件改名)

输入参数:
    AX = 7156h
    DS:DX -> 原来的 ASC 码文件名
    ES:DI -> 新的 ASC 码文件名
返回参数:CF 清除则成功
                    CF 置位则失败
        AX = 7100H 则表示功能不被支持

INT 21H 中断的 7141H 功能:(文件删除)

输入参数:
    AX = 7141h
    DS:DX -> ASC 码文件名
    SI = 属性及是否支持通配符
        0000h 不支持通配符,忽略属性
        0001h 支持通配符
    CL = 属性
    CH = must-match attributes
返回参数:CF 清除则成功
                    CF 置位则失败
        AX = 7100H 则表示功能不被支持

源程序:

;长文件名示例之一,建立文件 This is a test file.txt,写入一句文字,再关闭

code		segment
		assume	cs:code,ds:code
		org	100h
start:
		jmp	start1
file_name	db	'This is a test file.txt',0
d_text		db	'Sample text',0dh,0ah
d_text_end	equ	this byte
d_error		db	'Create file error !',0dh,0ah,24h
start1:
		mov	ax,716ch
		mov	bx,2		;Read and write
		xor	cx,cx		;Normal attrib
		mov	dx,0010h	;Create new file
		mov	si,offset file_name
		xor	di,di
		int	21h
		jb	error
		mov	bx,ax
		mov	ah,40h
		mov	cx,offset d_text_end - offset d_text
		mov	dx,offset d_text
		int	21h

		mov	ah,3eh
		int	21h
		int	20h
error:
		mov	ah,9
		mov	dx,offset d_error
		int	21h
		int	20h
code		ends
		end	start
;长文件名示例之二,打开文件 This is a test file.txt,显示文件内容,再关闭
code		segment
		assume	cs:code,ds:code
		org	100h
start:
		jmp	start1
file_name	db	'This is a test file.txt',0
buffer		db	13 dup (0),0dh,0ah,24h
d_error		db	'Open file error !',0dh,0ah,24h
start1:
		mov	ax,716ch
		mov	bx,2		;Read and write
		xor	cx,cx		;Normal attrib
		mov	dx,1		;Open file
		mov	si,offset file_name
		mov	di,1
		int	21h
		jb	error
		mov	bx,ax
		mov	ah,3fh
		mov	dx,offset buffer
		mov	cx,13
		int	21h
		
		mov	ah,9
		mov	dx,offset buffer
		int	21h
		
		mov	ah,3eh
		int	21h
		int	20h
error:
		mov	ah,9
		mov	dx,offset d_error
		int	21h
		int	20h
code		ends
		end	start
		
;长文件名示例之三,把文件 This is a test file.txt 改名到 This is another file name.txt
code		segment
		assume	cs:code,ds:code
		org	100h
start:
		jmp	start1
file_name	db	'This is a test file.txt',0
new_name	db	'This is another file name.txt',0 
d_error		db	'Rename file error !',0dh,0ah,24h
start1:
		mov	ax,7156h
		mov	dx,offset file_name
		mov	di,offset new_name
		int	21h
		jb	error
		
		int	20h
error:
		mov	ah,9
		mov	dx,offset d_error
		int	21h
		int	20h
code		ends
		end	start
;长文件名示例之四,把文件 This is another file name.txt 删除
code		segment
		assume	cs:code,ds:code
		org	100h
start:
		jmp	start1
file_name	db	'This is another file name.txt',0 
d_error		db	'Delete file error !',0dh,0ah,24h
start1:
		mov	ax,7141h
		mov	dx,offset file_name
		xor	si,si
		xor	cx,cx
		int	21h
		jb	error
		
		int	20h
error:
		mov	ah,9
		mov	dx,offset d_error
		int	21h
		int	20h
code		ends
		end	start	




(C) Copyright by LuoYunBin's Win32 ASM Page,http://asm.yeah.net