News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

passing some parameters to the thread

Started by six_L, June 09, 2020, 04:14:19 AM

Previous topic - Next topic

six_L

please del the topic. Before a thread can be created, you must allocate memory. A thread can't allowed to request resources.
Quotethe result of outputting  is wrong.
i will hope the result is
Tid = 1001,Randm = 55
Tid = 1002,Randm = 125
Tid = 1003,Randm = 271
option casemap:none
option win64:7

include \UASM64\include\windows.inc

includelib \UASM64\Lib\user32.lib
includelib \UASM64\Lib\kernel32.lib

ICO_MAIN equ 1000
DLG_MAIN equ 100
IDC_S equ 101
IDC_M equ 102
IDC_EXIT equ 103
IDC_OUTPUT equ 104

THREAD_RUN_PARAMETERS STRUCT
Tid WORD ?
tRunFlag BOOL ?
tMax QWORD ?
tMin QWORD ?
THREAD_RUN_PARAMETERS ENDS

.data?
hInstance dq ?
hWinMain dq ?
threadId_1 dd ?
threadId_2 dd ?
threadId_3 dd ?

.data
OutBuffer db 8192 dup (0)
tParameters THREAD_RUN_PARAMETERS<0>
stCS CRITICAL_SECTION <0>
tszBuf db 128 dup (0)

.code

iRand proc _dqMin:QWORD, _dqMax:QWORD
local @dqRet:qword, @dqFreq:qword
   
invoke QueryPerformanceCounter,addr @dqFreq
mov rax, @dqFreq
mov rcx, 13
mul rcx
add rax, 7
mov rcx, _dqMax
sub rcx, _dqMin
inc rcx
xor rdx, rdx
div rcx
mov @dqRet, rdx
mov rax, @dqRet
add rax,_dqMin    ; rax = Rand_Number
ret

iRand endp

_memcpy proc uses rdi rsi pDest:qword, pSource:qword, SizeByte:qword
cld
mov     rcx, SizeByte
        mov     rsi, pSource
        mov     rdi, pDest
        mov     rax, rcx
        shr     rcx, 3
        rep movsq
        mov     rcx, rax
        and     rcx, 7
        rep movsb
        ret
_memcpy endp

iwork_thread proc USES rsi plParameters:THREAD_RUN_PARAMETERS
local @szBuf[128]:BYTE
LOCAL dqMin:QWORD
LOCAL dqMax:QWORD
LOCAL pm:THREAD_RUN_PARAMETERS

mov rsi,plParameters
invoke _memcpy,addr pm,rsi, sizeof THREAD_RUN_PARAMETERS
lea rsi,pm
.while THREAD_RUN_PARAMETERS.tRunFlag[rsi] == TRUE

invoke RtlZeroMemory,ADDR @szBuf, sizeof @szBuf
mov rdx,THREAD_RUN_PARAMETERS.tMin[rsi]
mov dqMin,rdx
mov rcx,THREAD_RUN_PARAMETERS.tMax[rsi]
mov dqMax,rcx
invoke  iRand,dqMin,dqMax
invoke wsprintf,ADDR @szBuf,CStr("Tid = %d,Randm = %d",13,10),THREAD_RUN_PARAMETERS.Tid[rsi],rax

invoke EnterCriticalSection,ADDR stCS

invoke lstrcat,addr OutBuffer,ADDR @szBuf
invoke SetDlgItemText,hWinMain,IDC_OUTPUT,addr OutBuffer

invoke LeaveCriticalSection,ADDR stCS
invoke Sleep,1000
.endw

ret

iwork_thread endp

xwork_thread proc USES rsi plParameters:THREAD_RUN_PARAMETERS
local @szBuf[128]:BYTE
LOCAL dqMin:QWORD
LOCAL dqMax:QWORD

mov rsi,plParameters
.while THREAD_RUN_PARAMETERS.tRunFlag[rsi] == TRUE

invoke RtlZeroMemory,ADDR @szBuf, sizeof @szBuf
mov rdx,THREAD_RUN_PARAMETERS.tMin[rsi]
mov dqMin,rdx
mov rcx,THREAD_RUN_PARAMETERS.tMax[rsi]
mov dqMax,rcx
invoke  iRand,dqMin,dqMax
invoke wsprintf,ADDR @szBuf,CStr("Tid = %d,Randm = %d",13,10),THREAD_RUN_PARAMETERS.Tid[rsi],rax

invoke EnterCriticalSection,ADDR stCS

invoke lstrcat,addr OutBuffer,ADDR @szBuf
invoke SetDlgItemText,hWinMain,IDC_OUTPUT,addr OutBuffer

invoke LeaveCriticalSection,ADDR stCS
invoke Sleep,1000
.endw

ret

xwork_thread endp

_ProcDlgMain proc hWnd:qword,wMsg:dword,wParam:qword,lParam:qword
LOCAL ThreadId:DWORD

mov eax,wMsg
.if eax == WM_INITDIALOG
push hWnd
pop hWinMain
invoke LoadIcon,hInstance,ICO_MAIN
invoke SendMessage,hWnd,WM_SETICON,ICON_BIG,eax

invoke InitializeCriticalSection,ADDR stCS
invoke RtlZeroMemory,ADDR OutBuffer, sizeof OutBuffer

.elseif eax == WM_COMMAND
mov rax,wParam
.if ax == IDC_EXIT
invoke SendMessage,hWnd,WM_CLOSE,NULL,NULL
.elseif ax == IDC_S

invoke RtlZeroMemory,ADDR tParameters, sizeof tParameters

mov tParameters.Tid,1001
mov tParameters.tRunFlag,TRUE
mov tParameters.tMax,100
mov tParameters.tMin,1
invoke CreateThread,NULL,NULL, offset iwork_thread,offset tParameters, 0, offset threadId_1
invoke CloseHandle, rax

mov tParameters.Tid,1002
mov tParameters.tRunFlag,TRUE
mov tParameters.tMax,200
mov tParameters.tMin,101
invoke CreateThread,NULL,NULL, offset iwork_thread, offset tParameters, 0,offset threadId_2
invoke CloseHandle, rax

mov tParameters.Tid,1003
mov tParameters.tRunFlag,TRUE
mov tParameters.tMax,300
mov tParameters.tMin,201
invoke CreateThread,NULL,NULL, offset iwork_thread, offset tParameters, 0, offset threadId_3
invoke CloseHandle, rax

.elseif ax == IDC_M
invoke RtlZeroMemory,ADDR tszBuf, sizeof tszBuf
invoke wsprintf,ADDR tszBuf,CStr("1_tid = %d, 2_tid = %d, 3_tid = %d"),threadId_1,threadId_2,threadId_3
invoke MessageBox, 0, ADDR tszBuf,CStr("Thread_Id"), MB_OK
.endif
.elseif eax == WM_CLOSE
invoke DeleteCriticalSection,ADDR stCS
invoke EndDialog,hWnd,NULL
.else
mov rax,FALSE
ret
.endif
mov rax,TRUE
ret

_ProcDlgMain endp

WinMainCRTStartup Proc
invoke GetModuleHandle,NULL
mov hInstance,rax

invoke DialogBoxParam,hInstance,DLG_MAIN,NULL,offset _ProcDlgMain,NULL
invoke ExitProcess,NULL
WinMainCRTStartup Endp

end

#include <\UASM64\include\resource.h>

#define ICO_MAIN 1000
#define DLG_MAIN 100
#define IDC_S 101
#define IDC_M 102
#define IDC_EXIT 103
#define IDC_OUTPUT 104
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
ICO_MAIN ICON "ICON.ico"
//>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
DLG_MAIN DIALOG 0, 0, 337, 144
STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_VISIBLE | WS_CAPTION | WS_SYSMENU | WS_THICKFRAME
CAPTION "Test_Thread"
FONT 10, "Calibri"
{
EDITTEXT IDC_OUTPUT, 2, 2, 332, 125,ES_MULTILINE | ES_AUTOVSCROLL | ES_READONLY | WS_BORDER | WS_TABSTOP | WS_VSCROLL
PUSHBUTTON "StartT", IDC_S,2, 128, 40, 14
PUSHBUTTON "MdfyT", IDC_M,43, 128, 40, 14
PUSHBUTTON "Exit", IDC_EXIT, 294, 128, 40, 14, WS_TABSTOP
}
Say you, Say me, Say the codes together for ever.