_beginthreadex and DllMain 
Author Message
 _beginthreadex and DllMain

Hopefully im not posting too off topic:

Currently, Im calling _beginthreadex in DllMain in response to
DLL_PROCESS_ATTACH ...

the handle returned is valid, ie. not -1.

However, my thread function never executes ....
Here's a snippet ....

HANDLE    g_hTestThread = 0;
HANDLE    g_hInstance = 0;
Gather_Information  GatherInfo;

unsigned int __stdcall ThreadFunc (LPVOID)
{
 return MessageBox(0, "Thread Func", 0, MB_OK);

Quote:
}

int WINAPI DllMain(HANDLE hInstance, DWORD dwReason, LPVOID lpReserved)
{
 int nReturn = FALSE;
 switch(dwReason)
 {

  case DLL_PROCESS_ATTACH:
  {
   MessageBox(0, "In", 0, MB_OK);
   g_hInstance = hInstance;
   // try to spawn thread here.
   g_hTestThread = (HANDLE) _beginthreadex(0, 0, ThreadFunc, 0, 0, 0);
   if(-1 == g_hTestThread)
    Destroy();
   else
   {
    Gather();
    Dispatch();
    Destroy();
   }

   break;
  }
 }

 return nReturn;

Quote:
}

Any ideas?
TIA,
Joshua Emele


Mon, 22 Sep 2003 05:02:24 GMT  
 _beginthreadex and DllMain
Hi,

not sure if this link helps, but seems to match somehow on your problem

http://support.microsoft.com/support/kb/articles/Q177/9/17.ASP

I think, your problem has to do with the fact, that DllMain is called
for every thread and process created in the processes the dll in question
is loaded into. So, while DllMain is running and calling _beginthread,
DllMain is called again. But DllMain is not reentrant.

Rene

Quote:

>Currently, Im calling _beginthreadex in DllMain in response to
>DLL_PROCESS_ATTACH ...

>the handle returned is valid, ie. not -1.

>However, my thread function never executes ....
>Here's a snippet ....

>HANDLE    g_hTestThread = 0;
>HANDLE    g_hInstance = 0;
>Gather_Information  GatherInfo;

>unsigned int __stdcall ThreadFunc (LPVOID)
>{
> return MessageBox(0, "Thread Func", 0, MB_OK);
>}

>int WINAPI DllMain(HANDLE hInstance, DWORD dwReason, LPVOID lpReserved)
>{
> int nReturn = FALSE;
> switch(dwReason)
> {

>  case DLL_PROCESS_ATTACH:
>  {
>   MessageBox(0, "In", 0, MB_OK);
>   g_hInstance = hInstance;
>   // try to spawn thread here.
>   g_hTestThread = (HANDLE) _beginthreadex(0, 0, ThreadFunc, 0, 0, 0);
>   if(-1 == g_hTestThread)
>    Destroy();
>   else
>   {
>    Gather();
>    Dispatch();
>    Destroy();
>   }

>   break;
>  }
> }

> return nReturn;
>}

>Any ideas?
>TIA,
>Joshua Emele

--
Rene Nyffenegger
rene dot nyffenegger at adp-gmbh dot ch


Tue, 23 Sep 2003 06:01:26 GMT  
 _beginthreadex and DllMain
When you create thread in DllMain, it is reported as created with valid
handle, but the actuall execution of the thread is postponed after your code
(any of remaining threads) leave from DllMain.
The reason is, all DllMain calls are synchronized for all threads (even when
you explicitly set to ignore threads by calling ::DisableThreadLibraryCalls
in DLL_PROCESS_ATTACH).

--
Richard Musil



Sat, 27 Sep 2003 23:43:15 GMT  
 _beginthreadex and DllMain
_beginthreadex returns 0 on failure.  _beginthread returns -1 on error.  You
should be checking against 0.

--
To reply by email remove NOSPAM from my address

Quote:
> Hopefully im not posting too off topic:

> Currently, Im calling _beginthreadex in DllMain in response to
> DLL_PROCESS_ATTACH ...

> the handle returned is valid, ie. not -1.

> However, my thread function never executes ....
> Here's a snippet ....

> HANDLE    g_hTestThread = 0;
> HANDLE    g_hInstance = 0;
> Gather_Information  GatherInfo;

> unsigned int __stdcall ThreadFunc (LPVOID)
> {
>  return MessageBox(0, "Thread Func", 0, MB_OK);
> }

> int WINAPI DllMain(HANDLE hInstance, DWORD dwReason, LPVOID lpReserved)
> {
>  int nReturn = FALSE;
>  switch(dwReason)
>  {

>   case DLL_PROCESS_ATTACH:
>   {
>    MessageBox(0, "In", 0, MB_OK);
>    g_hInstance = hInstance;
>    // try to spawn thread here.
>    g_hTestThread = (HANDLE) _beginthreadex(0, 0, ThreadFunc, 0, 0, 0);
>    if(-1 == g_hTestThread)
>     Destroy();
>    else
>    {
>     Gather();
>     Dispatch();
>     Destroy();
>    }

>    break;
>   }
>  }

>  return nReturn;
> }

> Any ideas?
> TIA,
> Joshua Emele



Wed, 01 Oct 2003 01:24:47 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. _beginthreadex declaration help needed.

2. COM _beginthreadex or CreateThread

3. CreateThread vs _beginthread and _beginthreadex

4. Classes and _beginthreadex

5. Casting function in _beginthreadex()?

6. beginthreadex and member functions

7. _beginthreadex : undeclared indentifier

8. How Soon Does a BeginThreadEx start?

9. _beginthreadex start_address question

10. beginthreadex problem in dll

11. beginthread and beginthreadex

12. _beginthreadex thread never resumed

 

 
Powered by phpBB® Forum Software