【问题标题】:How to link kernel32.lib and user32.lib using a combination of `nasm` and `alink`?如何使用 `nasm` 和 `alink` 的组合来链接 kernel32.lib 和 user32.lib?
【发布时间】:2016-11-08 09:28:37
【问题描述】:

如何使用nasmalink 的组合来链接kernel32.lib 和user32.lib?

我正在关注一些关于汇编编程的教程,该指南希望我执行以下命令:

nasm  -fobj hello.asm                                    
alink -oPE hello \lib\kernel32.lib \lib\user32.lib

第一个命令按预期执行,但是第二个命令失败。

为了链接 .lib 文件,我从

复制了它们
C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\Lib

进入我当前的文件夹。

我在执行第二条命令时得到的错误信息是:

Loading file hello.obj
Loading file Kernel32.lib
2327 symbols
Loaded first linker member
Loading file User32.lib
1385 symbols
Loaded first linker member
matched Externs
matched ComDefs
Unresolved external MessageBoxA
Unresolved external ExitProcess

现在,我有两个问题:

1) kernel32.lib和user32.lib在哪里?

2) 如何正确链接这些库文件?

操作系统为 Windows 10(64 位)。

更新:

; Coded for NASM                                           ;
; nasm  -fobj hello.asm                                    ;
; alink -oPE hello \lib\kernel32.lib \lib\user32.lib       ;
                                                           ;
extern MessageBoxA              ; APIs used                ;
extern ExitProcess              ; in this file             ;
                                                           ;
[SECTION CODE USE32 CLASS=CODE] ; code section             ;
..start:                        ; for the linker           ;
                                                           ;
    push byte 0                 ; only the buttons 'OK'    ; 
    push dword caption          ; caption of the BOX       ;
    push dword text             ; text in the BOX          ;
    push byte 0                 ; handle of the Box        ;
      call MessageBoxA          ; print BOX on screen      ;
                                                           ;
    push byte 0                 ;                          ;
      call ExitProcess          ; EXIT                     ;
                                                           ;
    caption db "Your first WIN32 programm",0               ;
    text db "HELLO",0                                      ;
                                                           ;
end                             ; for the linker

【问题讨论】:

  • 您正在从正确的文件夹中获取 LIB 文件,但您确实不应该将它们复制到其他地方。只需为链接器提供路径,以便它可以找到它们。否则,我猜你的问题是 ALINK 不支持生成 64 位可执行文件。您需要生成 32 位目标代码,或者使用不同的链接器。
  • 您编译/链接 32 位或 64 位代码?以及如何在 asm 文件中声明 MessageBoxA 和 ExitProcess ?
  • 我的目的是编译/链接 32 位代码。我在上面添加了完整的示例。

标签: windows winapi assembly windows-10 nasm


【解决方案1】:

我还没有找到ALINK 可以使用的kernel.libuser.lib。这可能是由于所需 .obj 文件的格式,而大多数 Windows .obj 的格式为 COFF ALINK 希望使用 OMF。

合适的WIN32.LIBhere。它包括MessageBoxA,但不包括ExitProcess。不建议使用简单的RET 来终止纯 Windows 程序。

不过,NASM 也能胜任这项工作:

; Import the needed Win32 API functions.- http://www.nasm.us/doc/nasmdoc7.html#section-7.4.4
IMPORT ExitProcess kernel32.dll
IMPORT MessageBoxA user32.dll

; Still needed to be declared as external
EXTERN ExitProcess, MessageBoxA

[SECTION CODE USE32 CLASS=CODE] ; code section
..start:
    push 0                  ; only the buttons 'OK'
    push dword caption      ; caption of the BOX
    push dword text         ; text in the BOX 
    push 0                  ; handle of the Box
    call [MessageBoxA]      ; print BOX on screen

    push 0
    call [ExitProcess]

    caption db "Your first WIN32 programm",0
    text db "HELLO",0

请注意,函数在被调用时用括号装饰。此外,最好将变量放在单独的 DATA 部分中。

如果您计划一个大型项目,其中包含一堆 .DLL 中的一堆 IMPORT,请查看NASMX Project

【讨论】:

  • 根据 OSDev ALINK 应该支持 PECOFF objs,但我的猜测是 ALINK 不支持微软最近使用的新的短导入记录格式。来自 MinGW 的 kernel32.libuser32.lib,更具体地说是 w32api,应该使用旧的导入记录格式。
猜你喜欢
  • 1970-01-01
  • 2015-02-21
  • 2012-10-19
  • 2013-12-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-09-02
  • 2011-09-19
相关资源
最近更新 更多