td win32asm 312.asm

3
td_win32asm_312.asm ;============================================================================== ; Test Department's WINDOWS 32 BIT x86 ASSEMBLY EXAMPLE 312 ;============================================================================== ;============================================================================== ; ==> Part 312 : ASM example calling a function inside a DLL directly ! ;------------------------------------------------------------------------------ ; Thanks to Arnulfo for the idea to write this ASM / DLL example. ; If you encounter any ERROR please email me. ; OK, let's go : ; Because we are focussed to learn the DLL stuff here is no Main Window. ; This source code is like a standard asm file. ; API GetModuleHandleA gets our program ID. ; API LoadLibraryA loads our created DLL into memory. ; We check if an ERROR occured while loading and react with a message box. ; API GetProcAddress get the address of the specified function in the DLL. ; We use this pointer to call the function in the DLL. ; For testing purpose we also push two parameter ! ; API FreeLibrary unmaps the modul from address space of the calling process. ; API=ExitProcess terminates our program. ; Look to the end of this file how to create the EXE file. ;============================================================================== ; Assembler directives ;------------------------------------------------------------------------------ .386 ; specifies the processor our program want run on .Model Flat ,StdCall ; always the same for Win95 (32 Bit) option casemap:none ; case sensitive !!! ;============================================================================== ; Include all files where API functions resist you want use, set correct path ! ;------------------------------------------------------------------------------ include D:\Masm32\include\windows.inc includelib kernel32.lib includelib user32.lib ;============================================================================== ; Declaration of used API functions,take a look into WIN32.HLP and *.inc files ;------------------------------------------------------------------------------ GetModuleHandleA PROTO :DWORD LoadLibraryA PROTO :DWORD GetProcAddress PROTO :DWORD,:DWORD FreeLibrary PROTO :DWORD ExitProcess PROTO :DWORD MessageBoxA PROTO :DWORD,:DWORD,:DWORD,:DWORD ;============================================================================== ; .const = the constants area starts here, constants are defined & fixed ;------------------------------------------------------------------------------ .const ;============================================================================== ; .Data = the data area starts here, datas are defined but not fixed Page 1

Upload: z4rm4r

Post on 05-Jan-2016

213 views

Category:

Documents


0 download

DESCRIPTION

Skola asemblera TD zakon

TRANSCRIPT

Page 1: Td Win32asm 312.Asm

td_win32asm_312.asm;==============================================================================; Test Department's WINDOWS 32 BIT x86 ASSEMBLY EXAMPLE 312;==============================================================================

;==============================================================================; ==> Part 312 : ASM example calling a function inside a DLL directly !;------------------------------------------------------------------------------; Thanks to Arnulfo for the idea to write this ASM / DLL example.; If you encounter any ERROR please email me.; OK, let's go :; Because we are focussed to learn the DLL stuff here is no Main Window.; This source code is like a standard asm file.; API GetModuleHandleA gets our program ID.; API LoadLibraryA loads our created DLL into memory.; We check if an ERROR occured while loading and react with a message box.; API GetProcAddress get the address of the specified function in the DLL.; We use this pointer to call the function in the DLL.; For testing purpose we also push two parameter !; API FreeLibrary unmaps the modul from address space of the calling process.; API=ExitProcess terminates our program.; Look to the end of this file how to create the EXE file.

;==============================================================================; Assembler directives;------------------------------------------------------------------------------.386 ; specifies the processor our program want run on.Model Flat ,StdCall ; always the same for Win95 (32 Bit)option casemap:none ; case sensitive !!!

;==============================================================================; Include all files where API functions resist you want use, set correct path !;------------------------------------------------------------------------------include D:\Masm32\include\windows.incincludelib kernel32.libincludelib user32.lib

;==============================================================================; Declaration of used API functions,take a look into WIN32.HLP and *.inc files;------------------------------------------------------------------------------GetModuleHandleA PROTO :DWORDLoadLibraryA PROTO :DWORDGetProcAddress PROTO :DWORD,:DWORDFreeLibrary PROTO :DWORDExitProcess PROTO :DWORDMessageBoxA PROTO :DWORD,:DWORD,:DWORD,:DWORD

;==============================================================================; .const = the constants area starts here, constants are defined & fixed;------------------------------------------------------------------------------.const

;==============================================================================; .Data = the data area starts here, datas are defined but not fixed

Page 1

Page 2: Td Win32asm 312.Asm

td_win32asm_312.asm;------------------------------------------------------------------------------.DataDll_Function_Parameter1 db "inside a DLL, the pointer to this text",13,10 db "is given to the DLL as a parameter.",0Dll_Function_Parameter2 db "Message Box inside DLL",0Library_Name db "td_win32asm_310.dll",0 ;filename of the libraryFunction_Name db "Dll_Test01",0 ;function name inside libraryMB1Titel db "Message Box",0 ;message box nameDLL_error db "DLL not found",0 ;can not find/load DLLFunction_error db "Function not found",0 ;can't find/load functionhInstance dd 0 ;our program handlehLibrary dd 0 ;our library handlefPointer dd 0 ;pointer to choosen function

;==============================================================================; .Data? = the data? area starts here, not defined and not fixed;------------------------------------------------------------------------------.data?

;==============================================================================; .CODE = our code area starts here Main = label of our program code;------------------------------------------------------------------------------.CodeMain:

;==============================================================================; Always get your program ID first (API=GetModuleHandleA);------------------------------------------------------------------------------push 0h ;lpModuleHandle, 0=get program handlecall GetModuleHandleA ;- API Function -mov hInstance,eax ;return value in eax=handle of program

;==============================================================================; API LoadLibraryA maps the specified exe or dll module into the address space; of the calling process.;------------------------------------------------------------------------------push OFFSET Library_Name ;lpLibFileName, pointer filename modulecall LoadLibraryA ;- API Function -cmp eax,0h ;check if return value 0h=ERRORjne Library_OK ;if no error goto LABEL;------------------------------------------------------------------------------; On ERROR API "MessageBoxA" creates a message box, we can only click OK;------------------------------------------------------------------------------push 0h ;uType, style, 0=MB_OK Buttonpush OFFSET MB1Titel ;lpCaption,pointer to title textpush OFFSET DLL_error ;lpText,pointer to text message boxpush 0h ;handle of owner window 0=no ownercall MessageBoxA ;- API Function -jmp ExitPrg ;library not loaded, error on lib call ;library not loaded, exit program (!)

Library_OK:mov hLibrary,eax ;store handle of library in variable

Page 2

Page 3: Td Win32asm 312.Asm

td_win32asm_312.asm;------------------------------------------------------------------------------; API "GetProcAddress" gets the address of the specified function;------------------------------------------------------------------------------push OFFSET Function_Name ;pProcName, name of functionpush hLibrary ;hModule, handle to DLL modulecall GetProcAddress ;- API Function -cmp eax,0h ;check if return value 0h=ERRORjne Function_OK ;if no error goto LABEL;------------------------------------------------------------------------------; On ERROR API "MessageBoxA" creates a message box, we can only click OK;------------------------------------------------------------------------------push 0h ;uType, style, 0=MB_OK Buttonpush OFFSET MB1Titel ;lpCaption,pointer to title textpush OFFSET Function_error ;lpText,pointer to text message boxpush 0h ;handle of owner window 0=no ownercall MessageBoxA ;- API Function -jmp FreeLib ;library loaded, error on function call ;free library (!) before exit (!)

Function_OK:mov fPointer,eax ;store given pointer to the functionpush OFFSET Dll_Function_Parameter2 ;let's test if we can throw a parameter push OFFSET Dll_Function_Parameter1 ;let's test if we can throw a parameter call [fPointer] ;call function inside the DLL !

;------------------------------------------------------------------------------; API FreeLibrary unmaps the modul from address space of the calling process; Free the library if loaded (!) even if the called function not exist (!);------------------------------------------------------------------------------FreeLib:push hLibrary ;hLibModule, handle loaded lib. modulecall FreeLibrary ;- API Function -

;==============================================================================; Next we terminate our program (API=ExitProcess);------------------------------------------------------------------------------ExitPrg:push hInstance ;push our programm handle to exitcall ExitProcess ;- API Function -

;==============================================================================; end Main = end of our program code;------------------------------------------------------------------------------end Main ;end of our program code, entry point

;==============================================================================; To create the exe file use this commands with your Microsoft Assembler/Linker;------------------------------------------------------------------------------; ml.exe /c /coff td_win32asm_312.asm ;asm command; link.exe /subsystem:windows td_win32asm_312.obj ;link command;==============================================================================

Page 3