Hello. I was wondering if someone here could tell me what is needed to call a win32 function from NASM. I thought I could declare MessageBox for example with EXTERN and then use LINK to link my object module with user32.lib (from visual studio) but I get "unresolved symbol MessageBox" error when linking. Any ideas what I'm missing or what I'm doing wrong here?

8 Years
Discussion Span
Last Post by kindnumbernine

Thank you for the link. I had to rename the function to _MessageBoxA@16 in my source and it worked. Now I wonder why functions are named like this in the visual studio libraries. Is it a requirement of the linker? How would one go about writing their own routine in assembly to import a function directly from a DLL?


Didn't use to be that way, but due to multi-language there are various libraries.

MessageBox is merely a macro mapping to functions such as:
MessageBoxA Single-Byte Character System
MessageBoxW Wide-Character (Unicode)


An alternative is to use the Alink downloads:


; To assemble: nasm -f obj hw.asm 
; To link: alink -oPE hw .obj win32.lib 
extern MessageBoxA 
extern ExitProcess
segment .data USE32 
title db 'My First Win32 ASM Program', 0 
message db 'Hello, World!', 0 
segment .text USE32 
push dword 0Eh
push dword title 
push dword message 
push dword 0 
call MessageBoxA 
push dword 0
call ExitProcess

Thanks for the responses! I'm still confused though as to why functions are named like that. You say its because of multi-language - could you elaborate on that? Also, Microsoft's linker requires the entry point label to begin with an underscore but not other global names I put in my code. Could someone explain why that is?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.