;@GOTO TRANSLATE

.586P

.MODEL            FLAT, STDCALL


   OPTION         CASEMAP: NONE
   INCLUDE        WINDOWS.inc
   UNICODE        = FALSE
   INCLUDE        APIMACRO.mac

   ;INCLUDELIB     iKERNEL32
   ;INCLUDELIB     iUSER32

   INCLUDELIB     KERNEL32
   INCLUDELIB     USER32
   INCLUDELIB     ADVAPI32

   INCLUDELIB     DELAYIMP   ;comes with VC++
;------------------------------------------------------------------

   ;LINK creates stubs for APIs imported from delayed DLLs
   ;A part of this stub is JMP API -> that's why it is more economical
   ;(saves 1 byte) to use INVOKE (E8 call) instead of iWin32 (FF15 call)
   ;for such APIs. On the other hand when API is executed more times
   ;iWin32 solution is faster.

   ;I want to delay loading user32.dll and advapi32.dll

   MessageBoxA PROTO :DWORD, :DWORD, :DWORD, :DWORD
   MessageBoxW PROTO :DWORD, :DWORD, :DWORD, :DWORD

   RegCloseKey PROTO :DWORD

.CODE
   TEXTA    zTitle0,     <DelayDll.exe/0>
   TEXTA    zMessage0,   <USER32.dll loaded./0>
   TEXTW    zTitle1,     <DelayDll.exe/0>
   TEXTW    zMessage1,   <MessageBoxW./0>

 ExeMain    PROC
   INVOKE   MessageBoxA, NULL, szMessage0, szTitle0, NULL
   INVOKE   RegCloseKey, -1
   INVOKE   MessageBoxW, NULL, szMessage1, szTitle1, NULL
   iWin32@  ExitProcess, EAX
 ExeMain    ENDP

END ExeMain

:TRANSLATE
@ECHO OFF
ML    /c /coff  /nologo DelayDll.bat
LINK  DelayDll /filealign:0x200 /delayload:user32.dll /delayload:advapi32.dll /nologo /optidata /IGNORE:4078,4086 /SUBSYSTEM:WINDOWS /MERGE:.rdata=.text /MERGE:.data=.text /SECTION:.text,EWR
DEL   DelayDll.obj
PAUSE
CLS