News:

Download Pelles C here: http://www.smorgasbordet.com/pellesc/

Main Menu

Simple DLL

Started by Vortex, May 29, 2011, 10:47:54 AM

Previous topic - Next topic

Vortex

Here is a simple DLL demo :


; Console functions from the Masm32 package

include     ConsFuncs.inc

.code

LibMain     PROC instance:DWORD,reason:DWORD,unused:DWORD

   mov     eax,1
   ret

LibMain     ENDP

StrLen      PROC item:DWORD

   mov     eax,item
   push    ebx
   lea     edx,[eax+3]

@@:    

   mov     ebx,[eax]
   add     eax, 4
   lea     ecx,[ebx-01010101h]
   not     ebx
   and     ecx,ebx
   and     ecx, 80808080h
   jz      @B

   test    ecx,00008080h
   jnz     @F
   shr     ecx,16
   add     eax,2

@@:

   shl     cl,1
   sbb     eax,edx
   pop     ebx
   ret

StrLen      ENDP

locate      PROC x:DWORD,y:DWORD

   LOCAL   hOutPut  :DWORD

   invoke  GetStdHandle,STD_OUTPUT_HANDLE
   mov     hOutPut, eax

   mov     ecx, x
   mov     eax, y
   shl     eax, 16
   mov     ax, cx

   invoke  SetConsoleCursorPosition,hOutPut,eax
   ret

locate      ENDP

StdOut      PROC lpszText:DWORD

   LOCAL   hOutPut  :DWORD
   LOCAL   bWritten :DWORD
   LOCAL   sl       :DWORD

   invoke  GetStdHandle,STD_OUTPUT_HANDLE
   mov     hOutPut, eax

   invoke  StrLen,lpszText
   mov     sl, eax

   invoke  WriteFile,hOutPut,lpszText,sl,ADDR bWritten,NULL

   mov     eax, bWritten
   ret

StdOut      ENDP

ClearScreen PROC

   LOCAL   hOutPut:DWORD
   LOCAL   noc    :DWORD
   LOCAL   cnt    :DWORD
   LOCAL   sbi    :CONSOLE_SCREEN_BUFFER_INFO

   invoke  GetStdHandle,STD_OUTPUT_HANDLE
   mov     hOutPut, eax

   invoke  GetConsoleScreenBufferInfo,hOutPut,ADDR sbi

   mov     eax, sbi.dwSize

   push    ax
   rol     eax, 16
   mov     cx, ax
   pop     ax
   mul     cx
   cwde
   mov     cnt, eax

   invoke  FillConsoleOutputCharacter,hOutPut,32,cnt,NULL,ADDR noc

   invoke  locate,0,0
   ret
   
ClearScreen ENDP

END         LibMain
Code it... That's all...

Vortex

A modified version with dynamic run-time linking :


include     Demo.inc

.data

DllName     db 'ConsFuncs.dll',0
func1       db 'ClearScreen',0
func2       db 'StdOut',0
message     db 'Hello world!',13,10
            db 'This is a console application.',13,10,0

.data?

hModule     dd ?

.code

start:

    invoke  LoadLibrary,ADDR DllName
    mov     hModule,eax
    invoke  GetProcAddress,eax,ADDR func1
    call    eax ; call ClearScreen

    invoke  GetProcAddress,hModule,ADDR func2
    push    OFFSET message
    call    eax ; call StdOut

    invoke  FreeLibrary,hModule

    invoke  ExitProcess,0

END start
Code it... That's all...