flat assembler
Message board for the users of flat assembler.

Index > Windows > CreateThread with STARTUPINFO

Goto page 1, 2, 3  Next
Author
Thread Post new topic Reply to topic
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 22 Nov 2010, 13:37
Hi everyone, I'm trying to call MessageBox somewhere at screen address for example 800/600 coords. How can I do that ? STARTUPINFO got that field but I don't know how to use CreateProcess with MessageBox, only CreateThread works fine but dunno how to make it read from STARTUPINFO structure. Any help ?
Post 22 Nov 2010, 13:37
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Nov 2010, 14:06
MessageBox is just an API (wrapper to CreateWindow), not an executable. Since this wrapper doesn't provide functionality you need (setting window position), you need to use CreateWindow directly.
Post 22 Nov 2010, 14:06
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 22 Nov 2010, 14:17
vid
Can I create invisible window ? and just point msgbox to point in that place ? or how people are doing that ? :/
Post 22 Nov 2010, 14:17
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Nov 2010, 15:27
Window painted by MessageBox is just a plain window, as you can create with CreateWindow API. MessageBox function just supplies all the extra arguments, window procedure, etc. If MessageBox (wrapper over CreateWindow) doesn't do what you need, use CreateWindow directly. Go to MSDN, read description of that API, look up some "Hello world" examples of using it, etc. etc.

Maybe there is some simpler way, but I am not aware of it.
Post 22 Nov 2010, 15:27
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 22 Nov 2010, 17:58
vid
Thanks, I'll try that. Smile
Post 22 Nov 2010, 17:58
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 22 Nov 2010, 19:58
You may also have easier job using dialog instead of generic window - saves you part of your job, and there should be plenty of examples of dialog with static text and button(s).
Post 22 Nov 2010, 19:58
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 295
Alphonso 23 Nov 2010, 12:33
Just a quick play,
Code:
format PE GUI 4.0
include 'win32a.inc'

;-------------------------------------------
section '.text' code readable executable
;===========================================
        invoke  GetCurrentThreadId                              ; set up hook
        invoke  SetWindowsHookEx,WH_CBT,CBTProc,0,eax           ;
        cmp     eax,0
        jz      WhatWentWrong?
        mov     [hHook],eax
        invoke  MessageBox,0,Mess,Title,MB_TOPMOST              ; positionable msgbox
        invoke  UnhookWindowsHookEx,[hHook]                     ; clean up

  WhatWentWrong?:
        invoke  ExitProcess,0
;-------------------------------------------
proc    CBTProc, nCode,wparam,lparam
        push    ebx esi edi
        cmp     [nCode],HCBT_ACTIVATE
        jne     finish
        invoke  SetWindowPos,[wparam],0,20,20,0,0,\             ; Xoffset,Yoffset,0,0
                SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
  finish:
        xor     eax,eax                                         ; 0 = All ok
        pop     edi esi ebx
        ret
endp
;-------------------------------------------
section '.data' data readable writeable
;===========================================
  Title                 db 'Hello',0
  Mess                  db 'Put me where',10
                        db 'you want Smile',0
align 8
  hHook                 dd ?
;-------------------------------------------
section '.idata' import data readable writeable
;===========================================

     library kernel32,'KERNEL32.DLL',\
             user32,'USER32.DLL'

             include 'api\kernel32.inc'
             include 'api\user32.inc'
    
Post 23 Nov 2010, 12:33
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 23 Nov 2010, 14:40
Alphonso
Okay but can't it be done without hooking ? Cause I don't understand what Hook means and what the point of that.
Post 23 Nov 2010, 14:40
View user's profile Send private message Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 295
Alphonso 23 Nov 2010, 17:14
It was just an example of showing messagebox placement. I'm probably not the best person to explain but basically Windows sends a message that it's going to activate the messagebox and by "hooking" we become part of that message chain. If everyone in the chain passes the message as ok then it gets activated. Before we pass on the message however we have decided we want to reposition where the message box will appear by using the setwindowpos call.

By creating your own window or dialog box as Vid suggested you'll be able to do much more than using a simple messagebox so if hooking is confusing don't worry about it for now, it will become clearer later on. Then you can tell me how it works hehe Smile
Post 23 Nov 2010, 17:14
View user's profile Send private message Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 23 Nov 2010, 17:40
Alphonso
And is there way to do that with CreateThread ? Cause I wan't tu show up 2 messageboxes same time.
Post 23 Nov 2010, 17:40
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 24 Nov 2010, 00:05
Do the same thing in each thread.
Post 24 Nov 2010, 00:05
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Alphonso



Joined: 16 Jan 2007
Posts: 295
Alphonso 24 Nov 2010, 05:39
Vid speaks much wisdom. Must... listen.. to him. Wink

Using msgboxes is IMHO ermm, not very elegant. The example was supposed by be a sort of proof of concept. If you follow Vid's earlier advice I think you'll have something much nicer with good structure. But since you asked...
Code:
format PE GUI 4.0
entry start
include 'win32a.inc'

  Xspan         = 180   ; x spacing of msbox
  Yspan         = 180   ; y spacing
  XNum          = 5     ; number of boxes per row
  Freds         = 17    ; number of threads/boxes (no more than 63 for WFMO)
  XOrigin       = 20    ; X origin offset on display
  YOrigin       = 20    ; Y origin offset on display

;----------------------------------------
section '.text' code readable executable
;----------------------------------------

  start:        mov     ebx,Freds
                mov     esi,XOrigin                                     ; Top left display offsets
                mov     edi,YOrigin                                     ;

  @@:           mov     [ebx*4+XOffset-4],esi                           ; Create messagebox positions
                mov     [ebx*4+YOffset-4],edi                           ;

                lea     eax,[ebx*4+ThreadID-4]                          ; ThreadID pointer
                invoke  CreateThread,0,0,CpuThread,ebx,0,eax
                mov     [ebx*4+hThread-4],eax

                add     esi,Xspan                                       ; add some different positions
                cmp     esi,Xspan*XNum+XOrigin                          ; Next box position
                jb      .SameRow
                mov     esi,XOrigin                                     ; New row
                add     edi,Yspan
  .SameRow:     dec     ebx
                jnz     @b

                mov     esi,10                                          ; number of counts till timed out
  @@:           invoke  WaitForMultipleObjects,Freds,hThread,1,1000     ; count interval 1000ms (1 second)
                dec     esi
                jz      @f                                              ; reached our 10 second timeout
                cmp     eax,102h                                        ; 1000ms passed
                je      @b
  @@:
                mov     ebx,Freds
  @@:           invoke  UnhookWindowsHookEx,[ebx*4+hHook-4]             ; clean up hooks
                dec     ebx
                jnz     @b

                mov     edi,MessAllDone
                cmp     esi,0
                jnz     ThatsAllFolks

                mov     ebx,Freds                                       ; close boxes if timed out
  @@:           invoke  TerminateThread,[ebx*4+hThread-4],0
                dec     ebx
                jnz     @b
                mov     edi,MessTimeout

  ThatsAllFolks:
                invoke  MessageBox,0,edi,Title,MB_SYSTEMMODAL
  exit:         invoke  ExitProcess,0

;----------------------------------------
align 16
  proc          CpuThread,ThreadNo
                push    ebx esi edi

                mov     ebx,[ThreadNo]
                invoke  GetCurrentThreadId                              ; set up hook
                invoke  SetWindowsHookEx,WH_CBT,CBTProc,0,eax           ;
                mov     [ebx*4+hHook-4],eax

                sub     esp,64                                          ; some buffer area for message text
                mov     esi,esp
                cinvoke wsprintf,esi,wsformat,[ThreadNo]
                invoke  MessageBox,0,esi,Title,0
                add     esp,64

                pop     edi esi ebx
                ret
  endp

;----------------------------------------
align 16
  proc          CBTProc,nCode,wparam,lparam
                push    ebx esi edi
                cmp     [nCode],HCBT_ACTIVATE
                jne     finish
                invoke  GetCurrentThreadId
                mov     ebx,Freds
  @@:
                cmp     [ebx*4+ThreadID-4],eax                          ; enum from which thread
                je      .FoundThreadNum
                dec     ebx
                jnz     @b
                jmp     finish                                          ; should not happen :/
  .FoundThreadNum:
                invoke  SetWindowPos,[wparam],0,\
                        [ebx*4+XOffset-4],[ebx*4+YOffset-4],\           ; Xoffset,Yoffset,0,0
                        0,0,SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
  finish:
                xor     eax,eax                                         ; probably should be nice and chain, oh well
                pop     edi esi ebx
                ret
endp

;----------------------------------------
section '.data' data readable writeable
;----------------------------------------
  Title              db 'Message Box',0
  MessAllDone        db 'All done.',0
  MessTimeout        db '10 Second Timeout',0
  wsformat           db 'Thread No. %u',0
align 4
  Timeout            dd ?
  ThreadID           rd Freds
  hThread            rd Freds
  hHook              rd Freds
  XOffset            rd Freds
  YOffset            rd Freds
  Buff               rb 100

;----------------------------------------
section '.idata' import data readable writeable
;----------------------------------------

     library kernel32,'KERNEL32.DLL',\
             user32,'USER32.DLL'

             include 'api\kernel32.inc'
             include 'api\user32.inc'    
A bit rough and needs polishing. See if you can click all the messageboxes closed before the 10 seconds is up. Razz

EDIT: Fix stack error, thanks bitRAKE for pointing that out, and strange Sleep call.


Last edited by Alphonso on 24 Nov 2010, 15:17; edited 3 times in total
Post 24 Nov 2010, 05:39
View user's profile Send private message Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4082
Location: vpcmpistri
bitRAKE 24 Nov 2010, 07:27
Haha! Very Happy Time to play a trick on someone. Laughing
Post 24 Nov 2010, 07:27
View user's profile Send private message Visit poster's website Reply with quote
bitRAKE



Joined: 21 Jul 2003
Posts: 4082
Location: vpcmpistri
bitRAKE 24 Nov 2010, 08:02
Code:
mov     esi,esp
sub     esp,64 ; some buffer area for message text
cinvoke wsprintf,esi,wsformat,[ThreadNo]
invoke  MessageBox,0,esi,Title,MB_SYSTEMMODAL
mov     esp,esi    
...doesn't work as intended because the stack moves backward - ESI is the end of 64 byte buffer.
Code:
        add     esp,-128 ; some buffer area for message text
        mov     esi,esp
        cinvoke wsprintf,esi,wsformat,[ThreadNo]
        invoke  MessageBox,0,esi,Title,MB_SYSTEMMODAL
        mov     esp,esi
        sub     esp,-128    
...quick fix, because I need to fill the screen with holiday greetings...
Post 24 Nov 2010, 08:02
View user's profile Send private message Visit poster's website Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 24 Nov 2010, 10:29
Alphonso
Well.. Thank you for code but it's hard for me.. I should more learn about Win32API-s and then post questions like this.

Thank you all. Smile
Post 24 Nov 2010, 10:29
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 24 Nov 2010, 10:40
That's definitively a good idea. There are plenty of tutorials which teach basics of WinAPI GUI programming, specifically with regard to generic windows and dialogs - exactly what you need.
Post 24 Nov 2010, 10:40
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 24 Nov 2010, 21:29
Hey, I've added some functions by my guess.. But I have questions and problems here. I've copied hook example but code fails. Can someone fix what I'm doing wrong ? Thanks.
Code:
format PE GUI 4.0
include 'win32a.inc'
entry main

;------------------------------------------- 
section '.text' code readable executable 
;=========================================== 
proc startHook
        invoke  GetCurrentThreadId                              ; set up hook
        invoke  SetWindowsHookEx,WH_CBT,CBTProc,0,eax           ;
        ;cmp     eax,0
        ;jz      WhatWentWrong?
        mov     [hHook],eax 
        invoke  MessageBox,0,Mess,Title,MB_TOPMOST              ; positionable msgbox 
        invoke  UnhookWindowsHookEx,[hHook]                     ; clean up 
endp
 ; WhatWentWrong?:
  ;      invoke  ExitProcess,0
;-------------------------------------------
proc main
     invoke CreateThread,NULL,0,MSGProc,NULL,NULL,tid
     invoke SetWindowsHookEx,WH_CBT,CBTProc,0,tid
     invoke UnhookWindowsHookEx,eax
     ret
endp
proc MSGProc
     invoke MessageBox,0,Mess,Title,MB_OK
     ret
endp
proc    CBTProc, nCode,wparam,lparam
       ; push    ebx esi edi
        cmp     [nCode],HCBT_ACTIVATE
        jne     finish
        invoke  SetWindowPos,[wparam],0,20,20,0,0,\             ; Xoffset,Yoffset,0,0
                SWP_NOSIZE Or SWP_NOZORDER Or SWP_NOACTIVATE
  finish: 
        xor     eax,eax                                         ; 0 = All ok 
        ;pop     edi esi ebx
        ret 
endp 
;------------------------------------------- 
section '.data' data readable writeable 
;=========================================== 
  Title                 db 'Hello',0 
  Mess                  db 'Put me where',10 
                        db 'you want ',0 
;align 8
  hHook                 dd ?
  tid                   dd ?
;------------------------------------------- 
section '.idata' import data readable writeable 
;===========================================

     library kernel32,'KERNEL32.DLL',\ 
             user32,'USER32.DLL' 

             include 'api\kernel32.inc' 
             include 'api\user32.inc'    
Post 24 Nov 2010, 21:29
View user's profile Send private message Reply with quote
vid
Verbosity in development


Joined: 05 Sep 2003
Posts: 7105
Location: Slovakia
vid 24 Nov 2010, 23:53
Overflowz: I suggest you to get OllyDBg and learn to use it. It's really simple, just open the file you want to trace, and then keep hitting F7 (step into) or F8 (step over). This way you will learn asm, see result of every API call (and compare to expected result), and usually be able to figure problem very quickly. Learning to debug/trace code is a must-do for every programmer, and pays back greatly.
Post 24 Nov 2010, 23:53
View user's profile Send private message Visit poster's website AIM Address MSN Messenger ICQ Number Reply with quote
Overflowz



Joined: 03 Sep 2010
Posts: 1046
Overflowz 25 Nov 2010, 11:35
People, PLEASE STOP ASKING ME FOR OllyDBG! I have already it and using but I don't understand still! And I'm posting after that. I know how to use OllyDBG BUT I Don't understand and posting here.
Post 25 Nov 2010, 11:35
View user's profile Send private message Reply with quote
revolution
When all else fails, read the source


Joined: 24 Aug 2004
Posts: 20461
Location: In your JS exploiting you and your system
revolution 25 Nov 2010, 11:55
Overflowz: If you refuse to learn how to use a debugger then you will be forever lost with assembly.

People are suggesting you to use a debugger because it is often the only way to move forward with assembly.

While the posters here have been extremely patient with you (for the most part) I suggest that that that can't last forever. Newbies are encouraged and helped here, as a general rule, but there is also an expectation that they can grow from there and begin to help themselves solve problems. At some point you will have start diagnosing your own problems. Not only will it be faster for you solve things it will also give you control over your own coding.

I'm sure you have heard the phrase: "Give a man a fish and you feed him for a day. Teach a man to fish and you feed him for life". Well, we are trying to teach you to fish.

Learn OllyDbg.
Post 25 Nov 2010, 11:55
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 1, 2, 3  Next

< 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-2025, Tomasz Grysztar. Also on GitHub, YouTube.

Website powered by rwasa.