Sulaiman Chang Personal Website
Tutorial 17 : Dynamic Link Libraries


code for tut_17a.asm
format PE GUI 4.0 DLL
entry DllMain

include '%fasminc%\win32a.inc'

section '.data' data readable writeable
        DllErrBoxTitle  db 'DllMsgBox - Error',0

section '.code' code readable executable
        proc    DllMain, hinstDll, fdwReason, lpvReserved
                mov  eax,TRUE
                return
        endp
        
        proc    DllErrBox, hWnd, errMsg
                invoke  MessageBox,[hWnd],[errMsg],DllErrBoxTitle,MB_OK + MB_ICONERROR
                return
        endp

section '.idata' import data readable
        library USER32, 'USER32.DLL'

        import  USER32,\
                MessageBox,     'MessageBoxA'

section '.edata' export data readable
        export  'TUT_17.DLL',\
                DllErrBox,      'DllErrBox'

section '.reloc' fixups data discardable

code for tut_17b.asm
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

section '.data' data readable writeable
        msg1    db '[code]',13,10
                db '      Invoke DllErrBox,HWND_DESKTOP,msg1',13,10,13,10
                db '      import TUT_17A,\',13,10
                db '      DllErrBox,   ''DllErrBox''',13,10
                db '[/code]',0

section '.code' code readable executable
        start:
                invoke  DllErrBox,HWND_DESKTOP,msg1
                invoke  ExitProcess,0

section '.idata' import data readable
        library KERNEL32,'KERNEL32.DLL',\
                TUT_17A, 'TUT_17A.DLL'
        
        import  KERNEL32,\
                ExitProcess,    'ExitProcess'
        
        import  TUT_17A,\
                DllErrBox,      'DllErrBox'

code for tut_17c.asm
format PE GUI 4.0
entry start

include '%fasminc%\win32a.inc'

section '.data' data readable writeable
        dllName         db 'TUT_17A.DLL',0              ;dll name that to be loaded
        funcName        db 'DllErrBox',0                ;function / proc name that wish to be called
        funcAddr        dd ?                            ;store function address
        
        msgBoxTitle     db 'Error',0
        msgBoxDll       db 'The Requested DLL not found!',0
        msgBoxProc      db 'The Requested Procedure not found!',0
        
        msg1            db '[code]',13,10
                        db '      invoke  LoadLibrary,dllName',13,10
                        db '      invoke  GetProcAddress,eax,funcName',13,10
                        db '      invoke  funcAddr,HWND_DESKTOP,msg1',13,10
                        db '[/code]',0

section '.code' code readable executable
        start:
                invoke  LoadLibrary,dllName
                        cmp  eax,NULL                   ;the dll not found
                        je   dll_not_found
                invoke  GetProcAddress,eax,funcName
                        cmp  eax,NULL                   ;requested function not found
                        je   proc_not_found
                        mov  [funcAddr],eax
                invoke  funcAddr,HWND_DESKTOP,msg1
                        jmp  exit
                
                dll_not_found:
                        invoke  MessageBox,HWND_DESKTOP,msgBoxDll,msgBoxTitle,MB_OK + MB_ICONERROR
                                jmp  exit
                proc_not_found:
                        invoke  MessageBox,HWND_DESKTOP,msgBoxProc,msgBoxTitle,MB_OK + MB_ICONERROR
                                jmp  exit
        exit:
                        invoke  ExitProcess,0

section '.idata' import data readable
        library KERNEL32,'KERNEL32.DLL',\
                USER32,  'USER32.DLL'
        
        import  KERNEL32,\
                GetProcAddress, 'GetProcAddress',\
                LoadLibrary,    'LoadLibraryA',\
                ExitProcess,    'ExitProcess'

        import  USER32,\
                MessageBox,     'MessageBoxA'
Result :
tut_17b.asm

tut_17c.asm

Copyright © 2004 Sulaiman Chang. All Rights Reserved.