flat assembler
Message board for the users of flat assembler.

Index > Windows > DLLs less than 3K

Author
Thread Post new topic Reply to topic
iklin



Joined: 20 Mar 2004
Posts: 120
Location: Russia, Siberia
iklin
Greetings to all from Siberia!

First, sorry for my not so good English...
Second, I'm a beginner not only in fasm but in assembly at all.
But I'm trying to get some experience.

Third, I' trying to write some DLLs and confused with one little problem.

This code works fine!

Code:
format PE DLL
entry DLLstart

include 'import.inc'
include 'export.inc'
include 'stdcall.inc'

section '.code' code readable executable

proc DLLstart, hinstDLL, fdwReason, lpvReserved
   enter
       mov     eax, 1
      return

proc WriteText, text
  enter
       invoke  GetStdHandle, -11
   mov     ebx, eax
    mov     edi, [text]
 or      ecx, -1
     xor     al, al
      repne   scasb
       neg     ecx
 sub     ecx, 2
      invoke  WriteFile, ebx, [text], ecx, bytes_count, 0
 return

section '.data' data readable writeable

  bytes_count dd ?

section '.idata' import data readable writeable

      library kernel, 'kernel32.dll'

        import kernel,\
            GetStdHandle, 'GetStdHandle',\
           WriteFile, 'WriteFile'
         
section '.edata' export data readable

        export 'pedll2.dll',\
            WriteText, 'WriteText'

section '.reloc' fixups data readable discardable
    


But this code wan't works!

Code:
format PE DLL
entry DLLstart

include 'export.inc'
include 'stdcall.inc'

section '.code' code readable executable

proc DLLstart, hinstDLL, fdwReason, lpvReserved
      enter
       mov     eax, 1
      return

proc GetParamCount, argv
      enter
       push    ebx esi
     xor     ebx, ebx
    mov     esi, [argv]
.look_for_space:
 lodsb
       cmp     al, 0
       je      .end_of_line
        cmp     al, ' '
   jne     .look_for_space
     inc     ebx
 jmp     .look_for_space
.end_of_line:
        inc     ebx
 mov     eax, ebx
    pop     esi ebx
     return

section '.data' data readable writeable

section '.idata' import data readable writeable

section '.edata' export data readable

 export 'pedll2.dll',\
            GetParamCount, 'GetParamCount'

section '.reloc' fixups data readable discardable
    


And if I combine this 2 examples it works too.

Question:

Can somebody explain me what happens, what's wrong?
Why fasm produced DLL wan't work if it less than 3K?
Or I overlook something?

_________________
O, tempora! O, mores!
Post 21 Mar 2004, 07:04
View user's profile Send private message ICQ Number Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
You have to imort at least one function (I'm not sure but proably it has to be a function from kernel32.dll) in DLL under Win9x.
Post 21 Mar 2004, 07:19
View user's profile Send private message Visit poster's website Reply with quote
iklin



Joined: 20 Mar 2004
Posts: 120
Location: Russia, Siberia
iklin
Thanx, decard!
I tried and... voila! It's not necessarily from kernel32.dll. It may be any other. It only need to use this function somewhere in code.
But it strange for me. Why I can't write dll without any Win functions?
Post 21 Mar 2004, 07:48
View user's profile Send private message ICQ Number Reply with quote
decard



Joined: 11 Sep 2003
Posts: 1092
Location: Poland
decard
You have to use this function somewhere in your code because if you wont, it wont be actually imported (import macro imports only symbols that are used in source).
I also find this a bit strange. You have to use some function to make sure that kernel32.dll is mapped into the address space. (see this topic).

regards
Post 21 Mar 2004, 08:01
View user's profile Send private message Visit poster's website Reply with quote
f0dder



Joined: 19 Feb 2004
Posts: 3170
Location: Denmark
f0dder
Quote:

It's not necessarily from kernel32.dll. It may be any other.


The requirement is actually that kernel32.dll must be present in your process address space, to work on all win32 versions. This can happen by importing a dll that imports a dll that imports a dll [...] that imports kernel32.dll . However, just importing any DLL is not enough - try importing only from a "dummy.dll" that doesn't import anything - your app will fail to load on a bunch of windows versions, and it will fail silently (at least on win2k. You click the exe, and nothing happens - no error message, nothing. No instructions in the exe will be executed.)

It should be possible to have DLLs without any imports, as long as your main app imports from kernel32.

I would suggest not depending on "this DLL imports from kernel32 so I'll just import from this and skip kernel32", to avoid problems - you might as well import kernel32.ExitProcess anyway. Of course for 4k or 64k demos, it's sorta acceptable to import gdi32.Arc as your only import, it will get kernel32 imported on all current win32 (that I know of), and it's a very short import string.

The reason for the kernel32 stuff is that the initial thread in your process enters somewhere in kernel32, and your process entrypoint is called from this kernel32 function. Why no-imports works on XP is a bit weird, since the NT family has private DLL mappings. Perhaps some DLLs are force-loaded on XP?

Play it safe, import+call kernel32.ExitProcess in your main app.
Post 22 Mar 2004, 10:09
View user's profile Send private message Visit poster's website Reply with quote
iklin



Joined: 20 Mar 2004
Posts: 120
Location: Russia, Siberia
iklin
Thanx, decard! Thanx, f0dder!
As I see Win internals isn't clear enough for me... Sad
Post 24 Mar 2004, 18:45
View user's profile Send private message ICQ Number Reply with quote
Display posts from previous:
Post new topic Reply to topic

Jump to:  


< 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.