【问题标题】:Nasm kernel32.dll DeleteFileNasm kernel32.dll 删除文件
【发布时间】:2014-03-10 00:29:40
【问题描述】:

好的,我尝试使用 kernel32.dll 中的 DeleteFile 方法(使用 nasm 汇编程序),但它并没有删除文件,而是出现错误退出。

extern _GetStdHandle@4
extern _WriteConsoleA@20
extern _DeleteFileA@4
extern _ExitProcess@4

section .data
    msg: db "Could not delete the file", 10, 0
    len: equ $- msg

section .bss
    numCharsWritten resb 1

section .text
    global _start

    _start:
        mov edx, [esp+8]
        push dword [edx]            ; pushes argument.
        call _DeleteFileA@4         ; deletes file

        add esp, 8                  ; removes 2 arguments

        cmp eax, 0                  ; <cmp> = (eax == 0)
        je _error                   ; if(<cmp>) jump to _error

        push dword 0x0A             ; exit value
        call _ExitProcess@4         ; exit

    _error:
        push dword -0x0B
        call _GetStdHandle@4

        push dword 0                ; Arg4, unused
        push numCharsWritten        ; Arg3, POINTER to numCharsWritten
        push dword len              ; Arg2, length of the string
        push msg                    ; Arg1, the string
        push eax                    ; Arg0, _GetStdHandle@4
        call _WriteConsoleA@20      ; Writes the string

        push dword 0x0A             ; exit code
        call _ExitProcess@4         ; exit

它只是打印无法删除文件,然后退出。这段代码有错误吗?

【问题讨论】:

  • add esp, 8 一个,你似乎没有给它一个论点。
  • 您确信您已正确地从命令行中获取了文件名吗?我想你可能想要push [edx + 4] ...但我不做 Windows。

标签: assembly nasm kernel32


【解决方案1】:

除非您链接到 C 库(使用 gcc 或类似的东西),否则 Windows 程序没有 argc 或 argv,因此尝试使用 esp 访问参数将不起作用。相反,您需要使用GetCommandLineW,它将返回一个指向当前进程的命令行字符串的指针。要将其转换为 argc 和 argv,然后使用 CommandLineToArgvW。是的,Unicode 版本。这是一个示例,我使用printf 使显示更容易。

%define     STD_OUTPUT_HANDLE -11

; Shell32.dll
extern  CommandLineToArgvW

; Kernel32.dll
extern ExitProcess, WriteConsoleW, LocalFree
extern GetStdHandle, GetCommandLineW
%define GetCommandLine GetCommandLineW

;  msvcrt.dll
extern _printf

section .bss
stdout          resd 1
szArglist       resd 1
nArgs           resd 1

section .data
fmtst     db  "%ws", 13, 10, 0

section .text
global _start

_start:
    push    STD_OUTPUT_HANDLE
    call    GetStdHandle
    mov     dword [stdout], eax

    call    GetCommandLine

    push    nArgs
    push    eax
    call    CommandLineToArgvW
    mov     dword [szArglist], eax
    mov     esi, eax
    xor     ebx, ebx
    sub     dword [nArgs], 1

.DisplayArgs:
    push    dword [esi + 4 * ebx]
    push    fmtst
    call    _printf
    add     esp, 4 * 2

    inc     ebx
    cmp     ebx, dword [nArgs]
    jle     .DisplayArgs

    push    dword [szArglist]
    call    LocalFree

    push    0
    call    ExitProcess

还有输出:

【讨论】:

    猜你喜欢
    • 2015-04-18
    • 2014-04-26
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2019-02-25
    • 1970-01-01
    • 2015-04-09
    • 1970-01-01
    相关资源
    最近更新 更多