News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

_beginthreadex is hiding ....

Started by James Ladd, March 05, 2005, 05:00:50 AM

Previous topic - Next topic

James Ladd

Can someone please help me work out where the _beginthreadex function is ?
I need to call it from my MASM program and I can't seem to find it.
The leading underscore is not a typo.

Your help will be appreciated.

Rgs, striker.

MichaelW

eschew obfuscation

James Ladd

Ok.
And if I want to call _beginthreadex from my MASM prog, what do I need to link/include ?

MichaelW

#3
The include file and import library that Hutch posted recently seem to work OK.

http://www.masmforum.com/simple/index.php?topic=480.0

Not sure I did this all correctly, but it appears to work.

; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .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\masm32.inc
    include \masm32\include\user32.inc
    include \masm32\include\kernel32.inc
    include \masm32\include\msvcrt.inc

    includelib \masm32\lib\masm32.lib
    includelib \masm32\lib\user32.lib
    includelib \masm32\lib\kernel32.lib
    includelib \masm32\lib\msvcrt.lib

    include \masm32\macros\macros.asm

    newthread PROTO C :DWORD
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    .data
        hThread  dd 0
        threadID dd 0
        junk     db "junk",0
    .code
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
start:
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
    invoke _beginthreadex,NULL,
                          0,
                          ADDR newthread,
                          ADDR junk,
                          CREATE_SUSPENDED,
                          ADDR threadID
    mov   hThread,eax
    .IF eax == 0
      print chr$("_beginthreadex error",13,10)
    .ENDIF   
    REPEAT 2
      print chr$("original thread",13,10)
      invoke Sleep,2000
    ENDM   
    invoke ResumeThread,hThread
    print chr$("ResumeThread",13,10)
    REPEAT 4
      print chr$("original thread",13,10)
      invoke Sleep,2000
    ENDM
    print chr$("original thread",13,10)
    invoke CloseHandle,hThread
    .IF eax == 0
        print chr$(13,10,"CloseHandle error")
    .ENDIF
    invoke Sleep,1000
    mov   eax,input("Press enter to exit...")
    exit

newthread proc C arg:DWORD
    invoke Sleep,1000
    REPEAT 4
      print chr$("new thread ")
      print arg
      print chr$(13,10)
      invoke Sleep,2000
    ENDM
    ret
newthread endp
; ««««««««««««««««««««««««««««««««««««««««««««««««««««««««««
end start


eschew obfuscation

James Ladd

#4
Thanks for the reply, but im not sure how your example can work.
The definition of _beginthreadex in the library and inc from hutch does not match the one you use,
nor the one defined by Ms http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_crt__beginthread.2c_._beginthreadex.asp
I also need to send a single DWORD parameter to the thread function.
I also get a lot of errors from the msvcrt include file.

Here is a snippet from my code

    invoke _beginthreadex, NULL, 0, addr mythread, data,
                           CREATE_SUSPENDED, addr tid
    .if eax == INVALID_HANDLE_VALUE
        ; error: failed to create a thread.
        xor eax, eax
        mov reason, 10
        ret
    .endif


Here is the declaration of the thread func

align 4
mythread proc mydata:PDWORD
:
mythread endp


More help would be appreciated. edit - BUT IS NOT NEEDED. THE EXAMPLE ABOVE WORKS. STILL A LITTLE WORRIED ABOUT THE DIFFERENCE IN DECLARATION THOUGH.

Rgs, striker.

MichaelW

#5
The function returns a valid handle, and the thread does work.

original thread
original thread
ResumeThread
original thread
new thread junk
original thread
new thread junk
original thread
new thread junk
original thread
new thread junk
original thread
Press enter to exit...

I updated the code above to test the return value and pass a string to the new thread. Is your code perhaps passing the data rather than the address of the data?

The prototype Hutch used is:

_beginthreadex PROTO C :VARARG

Per the MASM 6.0 Programmer's Guide:

"If VARARG is an option in a procedure, INVOKE can pass arguments in addition to those in the parameter list without generating an error or warning."

I was assuming the use of VARARG was simply a shortcut to save time. IMO the prototypes should match the actual function parameters, but I don't see it as extremely important because you must know what the parameters are to successfully use a function.

I was thinking the includes that Vortex posted to the old Win32ASM Community message board listed all of the parameters, but I checked just now and they too use a single VARARG.

eschew obfuscation

James Ladd

MichaelW,
I appreciate your response and I have the code working now.
After it worked I went back and changed the proto for _beginthreadex to what it should be as per the doco.
I appreciate that both approaches work but like you I prefer the proto to be correct.

I then went and looked at CreateThread and I am now using that instead as it doesnt require me to
link with msvcrt or make a proto. The code still works. So Ill leave it at that.

Thanks again for your help.

Rgs, striker