错误LNK2001:未解析的外部符号_MessageBox

时间:2010-11-08 10:27:20

标签: winapi assembly masm masm32

我正在尝试使用masm而不是masm32 libs创建一个helloworld程序。以下是代码段:

.386
.model flat, stdcall
option casemap :none

extrn MessageBox : PROC
extrn ExitProcess : PROC

.data
        HelloWorld db "Hello There!", 0

.code
start:

        lea eax, HelloWorld
        mov ebx, 0
        push ebx
        push eax
        push eax
        push ebx
        call MessageBox
        push ebx
        call ExitProcess

end start

我可以使用masm组装它:

c:\masm32\code>ml /c /coff demo.asm
Microsoft (R) Macro Assembler Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

 Assembling: demo.asm

但是,我无法链接它:

c:\masm32\code>link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user
32.lib demo.obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

demo.obj : error LNK2001: unresolved external symbol _MessageBox
demo.obj : error LNK2001: unresolved external symbol _ExitProcess
demo.exe : fatal error LNK1120: 2 unresolved externals

我在链接期间包含了libs,所以不确定为什么它仍然说未解析的符号?

更新:

c:\masm32\code>link /subsystem:windows /defaultlib:kernel32.lib /defaultlib:user
32.lib demo.obj
Microsoft (R) Incremental Linker Version 9.00.21022.08
Copyright (C) Microsoft Corporation.  All rights reserved.

demo.obj : error LNK2001: unresolved external symbol _MessageBox@16
demo.exe : fatal error LNK1120: 1 unresolved externals

更新2:最终工作代码!

.386
.model flat, stdcall
option casemap :none

extrn MessageBoxA@16 : PROC
extrn ExitProcess@4 : PROC

.data
        HelloWorld db "Hello There!", 0

.code
start:

        lea eax, HelloWorld
        mov ebx, 0
        push ebx
        push eax
        push eax
        push ebx
        call MessageBoxA@16
        push ebx
        call ExitProcess@4

end start

2 个答案:

答案 0 :(得分:17)

正确的函数名称为MessageBoxA@16ExitProcess@4

几乎所有Win32 API函数都是stdcall,因此their names are decorated带有@符号,后跟参数占用的字节数。

此外,当Win32函数采用字符串时,有两种变体:一种采用ANSI字符串(名称以A结尾),另一种采用Unicode字符串(名称以W结尾)。您正在提供ANSI字符串,因此您需要A版本。

如果您没有使用汇编编程,编译器会为您处理这些问题。

答案 1 :(得分:5)

尝试在.data段之前添加此内容:

include \masm32\include\kernel32.inc
include \masm32\include\user32.inc
includelib \masm32\lib\kernel32.lib
includelib \masm32\lib\user32.lib
相关问题