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?

Recommended Answers

All 5 Replies

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:

http://alink.sourceforge.net/download.html

; 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 
 
..start 
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?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.