flat assembler
Message board for the users of flat assembler.

Index > Macroinstructions > Convenient import macros

Goto page Previous  1, 2
Author
Thread Post new topic Reply to topic
bitRAKE



Joined: 21 Jul 2003
Posts: 2913
Location: [RSP+8*5]
bitRAKE
Grom PE wrote:
The last question would be: is it possible to get rid of 'importend'? =)
How about?:
Code:
macro import [library,api] {
  common
    local part0,part1,first

    macro part0 lib0,[api0] \{
      \common
        lib0\#_str: db \`lib0
      \forward
        if rva $ mod 2 = 0
          db 0
        end if
        ; When align is right, one byte from previous import name
        ; is used as byte for next import's hint.
        api0\#_str = $-1
        db 0, \`api0
      \common
        db 0
    \}

    first = 1
    macro part1 lib1,[api1] \{
      \common
        if first = 1
          align 4
          first = 0
        else
          dd 0
        end if
        lib1\#_import:
      \forward
        api1 dd rva api1\#_str
    \}

  forward
    part0 library,api

  forward
    part1 library,api

  common
    data import
  forward
    dd 0, 0, 0, rva library#_str, rva library#_import
  common
    rd 5
    end data
}    
For example, the import section of the MINIPAD example would be:
Code:
  import \
    KERNEL32,<GetModuleHandleA,ExitProcess>,\
    USER32,<RegisterClassA,CreateWindowExA,DefWindowProcA,SetWindowLongA,\
            RedrawWindow,GetMessageA,TranslateMessage,DispatchMessageA,\
            SendMessageA,LoadCursorA,LoadIconA,LoadMenuA,GetClientRect,\
            MoveWindow,SetFocus,MessageBoxA,PostQuitMessage>,\
    GDI32,<CreateFontA,DeleteObject>    

_________________
¯\(°_o)/¯ unlicense.org
Post 15 Feb 2009, 21:30
View user's profile Send private message Visit poster's website Reply with quote
Grom PE



Joined: 13 Mar 2008
Posts: 114
Location: i@grompe.org.ru
Grom PE
bitRAKE, very nice, thanks!
Post 16 Feb 2009, 00:18
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2913
Location: [RSP+8*5]
bitRAKE
I'm wonder if there is a way to take it further - more encapsulated, more featureful? Have yet to test any of this - just brain storming:

First, make all labels local to import - in order that no possible name collisions could occur.
Code:
forward
  local library#_import,library#_str
  irp name,api { local name#_str }    
Next up is support for wide character switch. If (defined (x)A and defined (x)W) and not defined (x) then If UNICODE define (x) equ (x)W else define (x) equ (x)A.

Finally, support for 64-bit. Detecting long mode with a virtual block and change DD to DQ (equ/restore) seems easy enough for 64-bit support.

Edit: almost forgot - support for by ordinal import.
I don't know - never use it.
Post 16 Feb 2009, 02:11
View user's profile Send private message Visit poster's website Reply with quote
kempis



Joined: 12 Jun 2008
Posts: 49
kempis
Suggestion for bitRAKE's idea:
Code:
import     kernel32.dll,<\
 ExitProcess,\  ; normal import
     GetModuleHandle:AW,\       ; support for unicode and ansi version
      _my_tick_func:"GetTickCount",\   ; different import func name
        _unknown:33>     ; ordinal import    
Post 17 Feb 2009, 13:31
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 2913
Location: [RSP+8*5]
bitRAKE
I had a problem implementing the 64-bit version of this macro: windows don't like the NOP instructions used by FASM for ALIGN when the import data is in a code section. Don't ask how long this took to discover. This problem might also happen in 32-bit, so I post the new adaptation.
Code:
macro import [lib,api] {
     common
              local part0,part1,first

         macro part0 lib0,[api0] \{
                    \common
                            lib0\#_STR db \`lib0
                      \forward
                           rb 2 - RVA $ AND 1
                          label api0\#_STR at $-2
                            db \`api0
                  \common
                            db 0
                \}

                first = 7
           macro part1 lib1,[api1] \{
                    \common
                            rb (8 - RVA $ AND 7) AND first
                              first = 15
                          label lib1\#_TAB
                   \forward
                           api1 dq RVA api1\#_STR
             \}

        forward         part0 lib,api
       forward         part1 lib,api
       common          data import
 forward         dd 0,0,0,RVA lib#_STR,RVA lib#_TAB
  common          rd 5
                        end data
}    
...confirmed to work in WinXP 64-bit.

Edit: or, maybe it was some other problem. Confused

_________________
¯\(°_o)/¯ unlicense.org
Post 06 Jul 2009, 04:26
View user's profile Send private message Visit poster's website Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  
Goto page Previous  1, 2

< Last Thread | Next Thread >
Forum Rules:
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Copyright © 1999-2020, Tomasz Grysztar.

Powered by rwasa.