flat assembler
Message board for the users of flat assembler.

Index > Windows > DLL x64 CreateThread issue

Author
Thread Post new topic Reply to topic
nanasemaru



Joined: 01 Nov 2015
Posts: 3
nanasemaru
Apparently i need some spoonfeeding since i cant get this going:
Code:
format PE64 DLL
entry etest
include 'win64a.inc'
section '.text' code readable executable
;........................................................

ethread:
     invoke MessageBox,0,str1,str2,0
     invoke ExitProcess,0

etest:
     invoke CreateThread,0,0,ethread,0,0,0
     mov rax,1h
     ret

;........................................................
section '.bss' data readable writeable
     str1 db 'Hello!',0
     str2 db 'Test Thread',0
;........................................................
section '.idata' import data readable writeable
     library kernel32,'KERNEL32.DLL',user32,'USER32.DLL'
     include 'api/kernel32.inc'
     include 'api/user32.inc'
;........................................................
section '.reloc' fixups data readable discardable
     if $=$$
     dd 0,8
     end if 
    

basically i want to create a worker thread once the dll is attached
(Win 7 x64)
Post 04 Nov 2015, 18:41
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
nanasemaru
Each thread goes through the dll entry point during its initialization process. In your case this leads to creation of yet another thread. What you wrote is therefore an infinite recursion. You can find the correct prototype of the dll entry point in the fasm package in "examples\dll\errormsg.asm". Create the new thread only if fdwReason is DLL_PROCESS_ATTACH .

Btw. the thread entry point has also a different prototype. If you don't consider that the stack will get unbalanced, unless you do smth. uncommon such as ExitProcess in your case.

_________________
Faith is a superposition of knowledge and fallacy
Post 04 Nov 2015, 18:58
View user's profile Send private message Reply with quote
nanasemaru



Joined: 01 Nov 2015
Posts: 3
nanasemaru
Thx for the input Smile
Changed it but its still not creating the thread
Code:

format PE64 DLL
entry etest
include 'win64a.inc'
section '.text' code readable executable
;........................................................

proc ethread
     invoke MessageBoxA,0,str1,str2,0
     invoke ExitProcess,0
endp

proc etest hinstDLL,fdwReason,lpvReserved
     cmp dword[fdwReason],1
     jne @f
     invoke CreateThread,0,0,ethread,0,0,0
     @@:
     mov rax,1
     ret
endp

;........................................................
section '.bss' data readable writeable
     str1 db 'Hallo',0
     str2 db 'Test Thread',0
;........................................................
section '.idata' import data readable writeable
     library kernel32,'KERNEL32.DLL',user32,'USER32.DLL'
     include 'api/kernel32.inc'
     include 'api/user32.inc'
;........................................................
section '.reloc' fixups data readable discardable
     if $=$$
     dd 0,8
     end if  
    

(it compiles fine and it gets loaded)
Post 04 Nov 2015, 19:15
View user's profile Send private message Reply with quote
l_inc



Joined: 23 Oct 2009
Posts: 881
l_inc
nanasemaru
Well, the prototype of a thread entry is proc ethread lpParameter, but that's not the problem. The problem is that you can't access the first four arguments on the stack unless you stored those from the registers (see the last paragraph in the section 1.4 Procedures (64-bit)) . So your fdwReason value is invalid. You should also remove the dword cast.

_________________
Faith is a superposition of knowledge and fallacy
Post 04 Nov 2015, 20:04
View user's profile Send private message Reply with quote
nanasemaru



Joined: 01 Nov 2015
Posts: 3
nanasemaru
Perfect \ :v / thank you l_inc - now it works Very Happy
(Only used inline fasm before :>)
Code:
     cmp rdx,1;<<
     jne @f
     invoke CreateThread,0,0,ethread,0,0,0
     @@:
     mov rax,1 
    
Post 04 Nov 2015, 20:24
View user's profile Send private message 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. Also on GitHub, YouTube, Twitter.

Website powered by rwasa.