【问题标题】:GetModuleFileName in NASM x86 assemblyNASM x86 程序集中的 GetModuleFileName
【发布时间】:2015-09-20 10:53:53
【问题描述】:

这是代码...

extern GetStdHandle
extern GetModuleFileNameW
extern WriteFile
extern ExitProcess

import GetStdHandle kernel32.dll
import GetModuleFileNameW kernel32.dll
import WriteFile kernel32.dll
import ExitProcess kernel32.dll

global ..start

segment .code USE32

..start:

push dword -11
call [GetStdHandle]
mov dword [hStdOut], eax

;Getting the filepath of exe on disk...
push dword 256 ;MAX_PATH
push dword [filepath] ;Pointer to the buffer
push dword 0 ;NULL
call [GetModuleFileNameW]

;Trying to output the filepath...
push dword 0
push dword nBytes
push dword 256 ;MAX_PATH
push dword filepath
push dword [hStdOut]
call [WriteFile]

xor eax, eax
push eax
call [ExitProcess]

segment .data

segment .bss
hStdOut resd 1
nBytes resd 1
filepath resd 32

我已经玩了一点,我得到的只是一个空白输出。当我玩弄它时,我也得到了一堆乱码。仍然没有文件路径,仍然没有喜悦。我有一种预感,我在 GetModuleFileNameW 上做错了什么,但我不能确定。我遵循了 Microsoft 网站上的文档,并按照您在汇编语言中的相反顺序放置了参数。我做错了什么?

【问题讨论】:

  • 这个push dword [filepath] 看起来很可疑...

标签: winapi assembly x86 nasm


【解决方案1】:
;Getting the filepath of exe on disk...
push dword 256 ;MAX_PATH
push dword [filepath] ;Pointer to the buffer
push dword 0 ;NULL
call [GetModuleFileNameW]

...必须改为...

;Getting the filepath of exe on disk...
push dword 256 ;MAX_PATH
push dword filepath ;Pointer to the buffer
push dword 0 ;NULL
call [GetModuleFileNameW]

【讨论】:

  • 还要注意GetModuleFileNameW 处理TCHARs 中的长度(输入和返回的长度),这意味着对于Unicode 版本,长度是字数(16 位)分配/复制。您要保留 32 个 DWORD,即 128 字节,这只是 64 个 unicode 字符的空间,而不是 256 个。
  • 更新:我没有使用 Unicode,我使用的是 ANSI 字符的 GetModuleFileNameA。我确实看到使用 GetModuleFileNameW 时字符之间似乎有空格,这可能只是 Unicode 格式的方式。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-22
  • 1970-01-01
  • 2015-09-22
  • 1970-01-01
相关资源
最近更新 更多