简体   繁体   中英

DLL CreateThread, DisableThreadLibraryCalls and _beginthreadex

I'm trying to understand what the difference is between CreateThread and _beginthreadex and why calling DisableThreadLibraryCalls only prevents threads installed with _beginthreadex from executing.

I have a project which is a DLL, an old DLL, this is a win32 dll and I am tasked with porting it to win64. One thing that tripped me up was a call in DLLMain to DisableThreadLibraryCalls .

My threads were installed with _beginthreadex , the body of the threads was never being executed cause of the call to DisableThreadLibraryCalls . Once I removed this, the threads were working correctly.

Now I've found that other threads in the same DLL are started with CreateThread , I then thought was the call to DisableThreadLibraryCalls there to prevent these threads from executing so I put it back and found that the threads created with CreateThread are executed regardless of if DisableThreadLibraryCall s is present or not but threads created with _beginthreadex are disabled.

Why? I can't find anything on:

https://msdn.microsoft.com/en-us/library/windows/desktop/ms682579(v=vs.85).aspx

That describes why this is happening.

CreateThread() is a Windows native API for creating threads while _beginthread() and _beginthreadex() are part of C runtime library and are intended to make it easier to manage thread creation, but they still internally have to call CreateThread() .

Your own answer is wrong because there is no explicit checking for enable/disable involved in C runtime.

Instead, C runtime library uses DLL_THREAD_ATTACH and DLL_THREAD_DETACH callbacks to manage thread local storage so it should not be surprising that disabling those callbacks by calling DisableThreadLibraryCalls() is preventing C runtime thread management functions from working correctly.

I have found references online that state that _beginthread and _beginthreadex interally call CreateThread so perhaps the checking disable/enable status is only performed in the top level routines which does echo my findings and to me suggests missing logic in CreateThread .

DisableThreadLibaryCalls has no effect on threads created with CreateThread directly.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM