flat assembler
Message board for the users of flat assembler.

Index > Windows > CreateThread with Parameters

Author
Thread Post new topic Reply to topic
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 13 May 2010, 15:19
ive a proc with one parameter, i need to use CreateThread to Create a thread for that proc and pass a struct as a parameter, i used to do this in delphi, but i dunno how to in fasm :/

is it even possible?
Post 13 May 2010, 15:19
View user's profile Send private message Reply with quote
cod3b453



Joined: 25 Aug 2004
Posts: 618
cod3b453 13 May 2010, 16:13
Something like this should do it (sorry can't test on this machine):
Code:
; Data section
       struct MY_STRUCT
              a dd ?
              b dd ?
       ends

        mystruct MY_STRUCT
        threadID dd ?

; Code section
        ;...
        invoke CreateThread,NULL,NULL,myproc,mystruct,NULL,threadID ; threadID can be NULL
        ;...

proc myproc,lpParam
        push esi

        mov esi,[lpParam]
        mov eax,[esi+MY_STRUCT.b]
        ;...

        pop esi
        ret
endp    

Hope that helps
Post 13 May 2010, 16:13
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 13 May 2010, 16:18
thanks Very Happy
will try that now
Post 13 May 2010, 16:18
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 29 Jun 2010, 16:33
im sorry to popup an old thread, but, the thread is created, but it crashes when using the code to read parameters Sad
is threre any other way to read parameters?
Post 29 Jun 2010, 16:33
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1389
Location: Piraeus, Greece
Picnic 29 Jun 2010, 17:15
hi, i did a quick run:

Code:
        mov dword [mystruct.a], 20
        mov dword [mystruct.b], 30
        invoke CreateThread,NULL,NULL,myproc,mystruct,NULL,threadID ; threadID can be NULL
    


Code:
        mov esi,[lpParam]
        mov eax,[esi+MY_STRUCT.a]
        add eax,[esi+MY_STRUCT.b]
        
        ; eax = 50
    


cod3b453's example looks good to me, better to post some of your code Nameless.
Post 29 Jun 2010, 17:15
View user's profile Send private message Visit poster's website Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 29 Jun 2010, 20:45
Code:
struct MY_STRUCT
     szFilePath              dw MAX_PATH dup (?)
ends

proc Test, lpParam

 locals
  Buffer rb 1024
 endl

 push esi
 mov      esi, [lpParam]
 invoke       lstrcpy, addr Buffer, [esi+MY_STRUCT.szFilePath]
 invoke     MessageBox, 0, addr Buffer, "s test", MB_OK
 pop esi
 ret
endp
    
Post 29 Jun 2010, 20:45
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1389
Location: Piraeus, Greece
Picnic 29 Jun 2010, 21:13
Code:
 struct MY_STRUCT
  szFilePath              db MAX_PATH dup (?)  
 ends 
    


Use LEA instruction to transfer the offset.
Code:
 mov esi, [lpParam]
 lea edi, [esi+MY_STRUCT.szFilePath]
 invoke lstrcpy, addr Buffer, edi
 invoke MessageBox, 0, addr Buffer, "s test", MB_OK
    
Post 29 Jun 2010, 21:13
View user's profile Send private message Visit poster's website Reply with quote
LocoDelAssembly
Your code has a bug


Joined: 06 May 2005
Posts: 4624
Location: Argentina
LocoDelAssembly 29 Jun 2010, 21:14
Protecting ultra expensive intellectual property Nameless? The code you provided cannot possibly be a fragment of your real code because you could never been able to compile a proc called "test".

This will work...
Code:
include 'win32wxp.inc'

struct MY_STRUCT
        szFilePath              du MAX_PATH dup (?)
ends

.code
start:
 invoke CreateThread,NULL,NULL,TestProc,mystruct,NULL,threadID ; threadID can be NULL
 invoke Sleep, 5000
 invoke ExitProcess, 0

proc TestProc, lpParam
 locals
  Buffer rb 1024
 endl

 push esi
 mov    esi, [lpParam]
 invoke lstrcpy, addr Buffer, addr esi+MY_STRUCT.szFilePath
 invoke MessageBox, 0, addr Buffer, "s test", MB_OK
 pop esi
 ret
endp

.data
  mystruct MY_STRUCT <"Hello world", 0>
  threadID dd ?
.end start     
Post 29 Jun 2010, 21:14
View user's profile Send private message Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 29 Jun 2010, 21:28
@Picnic: thanks alot Very Happy. worked like a charm, but can u tell me why u used lea?
@LocoDeAssemblt: the original function name is in my native language Very Happy
Post 29 Jun 2010, 21:28
View user's profile Send private message Reply with quote
Picnic



Joined: 05 May 2007
Posts: 1389
Location: Piraeus, Greece
Picnic 29 Jun 2010, 21:48
consider a structure mystruct of type MY_STRUCT with various members, lpParam holds mystruct first member offset address.
Code:
struct MY_STRUCT
      a dd ?
      b dd ?
      szFilePath db MAX_PATH dup (?) 
ends 


mov esi, [lpParam]    ; now esi holds mystruct first member offset address

; dword [esi] --> value of a
; dword [esi+MY_STRUCT.a] --> value of a
; dword [esi+MY_STRUCT.b] --> value of b
; lea edi, [esi+MY_STRUCT.szFilePath] --> edi address of szFilePath
; addr esi+MY_STRUCT.szFilePath --> address of szFilePath
    
Post 29 Jun 2010, 21:48
View user's profile Send private message Visit poster's website Reply with quote
Nameless



Joined: 30 Apr 2010
Posts: 95
Nameless 29 Jun 2010, 22:09
thanks alot Very Happy
either things are getting easier by the time, or ur good at explaining
or worse xD, my questions are getting dumber Very Happy
Post 29 Jun 2010, 22:09
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-2024, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.