本文介绍了错误LNK2001:无法解析的外部符号_MessageBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试仅使用masm而不使用masm32库来创建helloworld程序.这是代码段:

I am trying to create a helloworld program using only masm and not masm32 libs. Here is the code snippet:

.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进行组装:

I am able to assemble this using 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

但是,我无法将其链接:

However, I am unable to link it:

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

我在链接期间包含了库,因此不确定为什么它仍然显示未解析的符号吗?

I am including the libs during linking, so not sure why it still says unresolved symbols?

更新:

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

推荐答案

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

几乎所有Win32 API函数都是stdcall,因此它们的名称都经过修饰带有@符号,然后是其参数占用的字节数.

Almost all Win32 API functions are stdcall, so their names are decorated with an @ sign, followed by the number of bytes taken up by their parameters.

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

Additionally, when a Win32 function takes a string, there are two variants: one that takes an ANSI string (name ends in A) and one that takes a Unicode string (name ends in W). You're supplying an ANSI string, so you want the A version.

当您不进行汇编编程时,编译器会为您解决这些问题.

When you're not programming in assembly the compiler takes care of these points for you.

这篇关于错误LNK2001:无法解析的外部符号_MessageBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 00:16