Author Topic: How to use CreateThread and CreateProcess APIs in nasm  (Read 9236 times)

Offline zarzor

  • Jr. Member
  • *
  • Posts: 2
How to use CreateThread and CreateProcess APIs in nasm
« on: December 06, 2014, 11:03:32 PM »
HI

I want to know how to use CreateThread and CreateProcess APIs in nasm.

lets say that i want to open "calc.exe" by CreateProcess API in a new Thread .

any examples ??

Thanx

Offline Mathi

  • Jr. Member
  • *
  • Posts: 82
  • Country: in
    • Win32NASM
Re: How to use CreateThread and CreateProcess APIs in nasm
« Reply #1 on: December 08, 2014, 11:03:22 AM »
The following example uses CreateProcess to launch calc.exe.
The call to CreateProcess in this program itself is non-blocking. so the message box doesn't wait for the calc application to close.

Code: [Select]
;;
;;File Name:  launch.asm
;;
;;Assemble with
;; nasm -fwin32 launch.asm
;;
;;Link with
;; GoLink.exe /entry _main -o launch.obj kernel32.dll user32.dll

;;
;; The following definitions were taken from nagoa+.inc
;;

%define TRUE 1
%define FALSE 0
%define NULL 0
%define CREATE_NO_WINDOW 8000000h
%define MB_OK 0h

STRUC STARTUPINFO
.cb RESD 1
.lpReserved RESD 1
.lpDesktop RESD 1
.lpTitle RESD 1
.dwX RESD 1
.dwY RESD 1
.dwXSize RESD 1
.dwYSize RESD 1
.dwXCountChars RESD 1
.dwYCountChars RESD 1
.dwFillAttribute RESD 1
.dwFlags RESD 1
.wShowWindow RESW 1
.cbReserved2 RESW 1
.lpReserved2 RESD 1
.hStdInput RESD 1
.hStdOutput RESD 1
.hStdError RESD 1
ENDSTRUC

STRUC PROCESS_INFORMATION
.hProcess RESD 1
.hThread RESD 1
.dwProcessId RESD 1
.dwThreadId RESD 1
ENDSTRUC

extern CreateProcessA
extern ExitProcess
extern MessageBoxA

global _main

segment .data USE32

startinfo:                   ;;;;initialize STARTUPINFO hopefully zeroed!
istruc STARTUPINFO

iend


processinfo:                  ;;;;initialize PROCESS_INFORMATION hopefully zeroed!
istruc PROCESS_INFORMATION

iend

command: db "C:\\WINDOWS\\system32\\calc.exe",0
titlemsg: db "hello",0
infomsg: db "Calculator application launched.",0

segment .code USE32
_main:               ;;entry point

push dword processinfo
push dword startinfo
push dword NULL
push dword NULL
push dword CREATE_NO_WINDOW
push dword FALSE
push dword NULL
push dword NULL
push dword command
push dword NULL
call CreateProcessA           ;; launch calc.exe

push dword MB_OK
push dword titlemsg
push dword infomsg
push dword 0
call MessageBoxA              ;;show a message box.

push dword 0
call ExitProcess              ;; exit out

Offline zarzor

  • Jr. Member
  • *
  • Posts: 2
Re: How to use CreateThread and CreateProcess APIs in nasm
« Reply #2 on: December 10, 2014, 02:25:06 AM »
Thanks Mathi  :)

It really helps

now i need to know how to use CreateThread and if i want to pass argument to its function

as in c++ i can do this

Code: [Select]

HANDL hThread = CreateThread(NULL,0,ThreadFunction,pThreadArg,0,0);


I tried this but didn't work for me:

Code: [Select]

push dword NULL
push dword NULL
push dword pThreadArg
push dword ThreadFunction
push dword NULL
push dword NULL
call CreateThread
         

in API guide tells that it should returns an exit code  :o

How to do that?

Thank you again
 
« Last Edit: December 10, 2014, 02:29:17 AM by zarzor »

Offline Mathi

  • Jr. Member
  • *
  • Posts: 82
  • Country: in
    • Win32NASM
Re: How to use CreateThread and CreateProcess APIs in nasm
« Reply #3 on: December 22, 2014, 07:21:01 AM »
The way you have called CreateThread seems to be OK.
The issue might be elsewhere.

Can you post a minimal version of your program and explain what you are trying to accomplish?


Offline gammac

  • Jr. Member
  • *
  • Posts: 71
  • Country: 00
Re: How to use CreateThread and CreateProcess APIs in nasm
« Reply #4 on: December 22, 2014, 10:33:25 AM »
in API guide tells that it should returns an exit code  :o

How to do that?
a
o.O
eax holds the return value

EDIT: Or did you mean the exit code of your thread? Then call ExitThread with the exit code.
« Last Edit: December 22, 2014, 10:25:16 PM by gammac »
Please comment your code! It helps to help you.