I'm running into an access violation that leads to a pretty clean crash of my program. Now i'm passing all the info properly to create thread except i'm leaving everything null/0 and then i specify the offset of the function that will become threaded and the offset to a dword for the threadid. I then close the handle that's returned. The problem shows up right when I use SendMessage in the threads function. So i threw it into ollydbg and before it crashes i get a access violation. What I'm wondering is if there's some security attributes or something else that I need to specify for my thread to beable to use sendmessage and not die.
Posted on 2005-04-14 16:38:32 by lst
What are you using SendMessage for?  SendMessage needs a handle, how is your thread getting this handle?  Are you passing it as a parameter?
Posted on 2005-04-14 17:27:43 by gorshing
First i tried storing the handle in a global dword, then i tried passing it as the parameter. Both methods failed for me.
Posted on 2005-04-14 17:48:19 by lst
This is an example that works. I did it using a template (fast mode). Copy, paste, and build all. It will work. Check it with your code.

.486                      ; create 32 bit code
.model flat, stdcall      ; 32 bit memory model
option casemap :none      ; case sensitive
    include \masm32\include\windows.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\debug.inc
    include \masm32\include\comdlg32.inc
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\debug.lib
    includelib \masm32\lib\comdlg32.lib
    include \masm32\macros\macros.asm
    FUNC MACRO parameters:VARARG
        invoke parameters
        EXITM <eax>
    include \masm32\include\dialogs.inc
    dlgproc      PROTO :DWORD,:DWORD,:DWORD,:DWORD
    ThreadProc    PROTO :DWORD
    ; message sended by thread (to window) when it is starting
    WM_THREAD_START equ WM_USER + 0100h
    ; message sended by thread (to window) when it is ending
    WM_THREAD_STOP  equ WM_USER + 0101h
    hInstance  dd ?
    hThread    dd ?
    mov hInstance, FUNC(GetModuleHandle,NULL)
    call main
    invoke ExitProcess,eax
; ------------------------------------------------------------------------------
; Main procedure (creates main window (dialog))
; ------------------------------------------------------------------------------
main proc
    Dialog  "Bare Bones Dialog", \          ; caption
            "MS Sans Serif",10, \          ; font,pointsize
            WS_OVERLAPPED or \              ; styles for
            WS_SYSMENU or DS_CENTER, \      ; dialog window
            2, \                            ; number of controls
            50,50,150,80, \                ; x y co-ordinates
            1024                            ; memory buffer size
    DlgButton "Cancel",WS_TABSTOP,48,40,50,15,IDCANCEL
    DlgStatic "Bare Bones Dialog Written In MASM32", \
    CallModalDialog hInstance,0,dlgproc,NULL
main endp

; ------------------------------------------------------------------------------
; Dialog procedure (that controls the main dialog window)
; ------------------------------------------------------------------------------
dlgproc proc hWin:DWORD, uMsg:DWORD, wParam:DWORD, lParam:DWORD
    local tmp:dword

    .if uMsg == WM_INITDIALOG
        invoke LoadIcon, NULL, IDI_ASTERISK
        invoke SendMessage, hWin, WM_SETICON, 1, eax
        ; -----------------
        ; create our thread
        ; -----------------
        invoke CreateThread, NULL, 0, addr ThreadProc, hWin, NULL, addr tmp
        mov hThread, eax
        .if eax == NULL
            ; thread handle will be not used, so close it
            invoke CloseHandle, hThread
    .elseif uMsg == WM_THREAD_START
        invoke MessageBox, hWin, CADD("Starting..."), CADD("Thread message"), MB_OK
    .elseif uMsg == WM_THREAD_STOP
        invoke MessageBox, hWin, CADD("Stoping..."), CADD("Thread message"), MB_OK
    .elseif uMsg == WM_COMMAND
        .if wParam == IDCANCEL
            jmp quit_dialog
    .elseif uMsg == WM_CLOSE


        invoke EndDialog,hWin,0
    xor eax, eax
    dlgproc endp
; ------------------------------------------------------------------------------   
; The thread procedure created by the main dialog window
; param: window handle that will be used with SendMessage
; ------------------------------------------------------------------------------   
ThreadProc proc param:dword

    invoke SendMessage, param, WM_THREAD_START, 0, 0    ; starting
    invoke SendMessage, param, WM_THREAD_STOP, 0, 0    ; ending

;    Using PostMessage() instead of SendMessage() will no block the thread
;    invoke PostMessage, param, WM_THREAD_START, 0, 0    ; started
;    invoke PostMessage, param, WM_THREAD_STOP, 0, 0    ; stopped

ThreadProc endp

end start

Hope it help.


PS: there are another ways for communicating threads.
Posted on 2005-04-14 18:56:05 by Kecol
Thanks for the code sample i've debugged my code a bit more and it appears that it is crashing when something modifies the stack, so i'm going to just change the buffers and dwords to global rather than local.

edit: Ok i looked into it deeper and it definately a stack issue with my thread i made it larger than the default and the loop ran fine. I guess i'll have to move away from using the stack like that for local shit and instead start allocating my own memory.
Posted on 2005-04-14 19:26:16 by lst
It could be a guard page problem - if you're going to use a large amount of stack memory, you should pre-touch the pages...
Posted on 2005-04-15 03:02:11 by f0dder