News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Why isn't this correctly including MessageBoxA?

Started by Ben321, March 24, 2015, 05:13:08 AM

Previous topic - Next topic

Ben321

INCLUDELIB c:\windows\syswow64\user32.dll
EXTERN MessageBoxA:dword


Compiling appears to go fine, but when the linker takes the object file, I end up with this error.

Quotetest.obj : error LNK2001: unresolved external symbol MessageBoxA
test.exe : fatal error LNK1120: 1 unresolved externals

How is it unresolved? I clearly included the library that it needed to use that function?

Gunther

Hi Ben,

what exactly is the command line of the linker?

Gunther
You have to know the facts before you can distort them.

dedndave

you don't want to resolve directly to the DLL
that's because the code for that function is not loaded into memory until the program is executed
(and, the OS takes care of that)

    INCLUDE    user32.inc
    INCLUDELIB user32.lib


user32.inc is a plain text file that contains the PROTOtypes (no EXTERN needed)
user32.lib is a static "import" library that provides the necessary PE file info to load the module and function at runtime

Ben321

Quote from: dedndave on March 24, 2015, 06:48:20 AM
you don't want to resolve directly to the DLL
that's because the code for that function is not loaded into memory until the program is executed
(and, the OS takes care of that)

    INCLUDE    user32.inc
    INCLUDELIB user32.lib


user32.inc is a plain text file that contains the PROTOtypes (no EXTERN needed)
user32.lib is a static "import" library that provides the necessary PE file info to load the module and function at runtime

In NASM, the code for including a DLL is simple
EXTERN MessageBoxA ;Tell the compiler that this assembly source file uses an externally provided function called MessageBoxA
IMPORT MessageBoxA user32.dll ;Tell the compiler that MessageBoxA is found in user32.dll


INCLUDELIB seems to be MASM's equivalent to NASM's IMPORT.
INCLUDELIB always requires the FULL PATH (c:\windows\syswow64\user32.dll) while NASM's IMPORT only requires you provide the file name if the rest of the path is in the c:\windows\system32\ or c:\windows\syswow64 folder or in the same folder as nasm.exe.
EXTERN in MASM requires typing in the names and types of input parameters, as well as the output type.
EXTERN in NASM requires none of this, just EXTERN MessageBoxA will suffice. All you need to do after that use some PUSH commands in your code to put the data needed pieces of data on the stack and then use CALL MessageBoxA to call it. With MASM, all of this with push commands still needs to be done, but you also have to waste more time typing longer lines for the INCLUDELIB and EXTERN statements.

Is there a way to make INCLUDELIB and EXTERN in MASM work like IMPORT and EXTERN in NASM? I am planning on switching to MASM (previously have been extensively using NASM), but only for one reason. MASM's ORG command is a lot more powerful than the ORG command in NASM, allowing you to arbitrarily determine where in the output file that code is to be compiled (in NASM it only uses ORG to calculate offsets but has no effect on placement of compiled code in the output file, expecting you to keep track of where the compiler's output pointer is in the output file). However all the other NASM syntax is far more ideal than the syntax of MASM. So if there's a command line switch for MASM or a compiler directive for it, which makes its expected syntax be more like NASM's syntax, I'd love to know about it.

user32.lib doesn't come in my windows\syswow64 folder. I do know though that it has user32.dll. The ONLY advantage to a LIB file is that when compiled it becomes part of the compiled EXE, removing the dependency on a DLL. But since user32.dll comes with EVERY COPY OF WINDOWS, everybody already has the DLL file, making it redundant to compile the functionality of the library into the EXE file via a LIB file. I plan to just stick with using user32.dll, rather than scouring the internet looking for a copy of user32.lib.

dedndave

in the upper right corner of the forum page, you will see a link to download the Masm32 pacakge
most of the include and library files you need are a big part of the package
and - Hutch has added many Masm32.lib functions, help files, tutorials, utilities, and probably 100+ example programs

i've been using masm for over 30 years - lol - not likely i will change to something else
my advice: try not to think in terms of "do it like nasm" - just have to learn a few new things
masm, for all it's shortcomings, is a pretty good tool

one of the forum members, Erol (Vortex) has created a number of great tools for working with libraries, object files, etc

http://www.vortex.masmcode.com/

MichaelW

Import libraries facilitate Load-Time Dynamic Linking. Unlike static libraries, import libraries contain no executable code.

;=====================================================================================
; EXE size 1KB
;=====================================================================================
.486                                      ; create 32 bit code
.model flat, stdcall                      ; 32 bit memory model
option casemap :none                      ; case sensitive
include \masm32\include\windows.inc
;includelib \masm32\lib\user32.lib
MessageBoxA PROTO :HWND,:LPCTSTR,:LPCTSTR,:UINT
;=====================================================================================
.data
.code
;=====================================================================================
start:
;=====================================================================================
    ;invoke MessageBoxA,0,0,0,0
    ret
end start


;=====================================================================================
; EXE size: 1KB
;=====================================================================================
.486                                      ; create 32 bit code
.model flat, stdcall                      ; 32 bit memory model
option casemap :none                      ; case sensitive
include \masm32\include\windows.inc
includelib \masm32\lib\user32.lib
MessageBoxA PROTO :HWND,:LPCTSTR,:LPCTSTR,:UINT
;=====================================================================================
.data
.code
;=====================================================================================
start:
;=====================================================================================
    ;invoke MessageBoxA,0,0,0,0
    ret
end start


;=====================================================================================
; EXE size 2KB
;=====================================================================================
.486                                      ; create 32 bit code
.model flat, stdcall                      ; 32 bit memory model
option casemap :none                      ; case sensitive
include \masm32\include\windows.inc
includelib \masm32\lib\user32.lib
MessageBoxA PROTO :HWND,:LPCTSTR,:LPCTSTR,:UINT
;=====================================================================================
.data
.code
;=====================================================================================
start:
;=====================================================================================
    invoke MessageBoxA,0,0,0,0
    ret
end start

Well Microsoft, here's another nice mess you've gotten us into.