ThreadProc does not run when CreateThread() is called within a DLL.

P

phark52

My main app calls LoadLibrary() to load a DLL, which calls
CreateThread(). This does NOT return NULL and I get a thread ID.
However, ThreadProc never gets executed when this code is in the DLL.
It works fine in the standalone EXE source.

I put example code below.

#define MB(msg) MessageBox(0, msg, "", MB_OK|MB_ICONINFORMATION);

DWORD APIENTRY tproc() {
for(;;)
MB("hi from tproc");
return 0;
}

BOOL APIENTRY DllMain (HINSTANCE hInst, DWORD reason, LPVOID reserved)
{
HANDLE h;
DWORD id;
MB("Creating thread...");
h=CreateThread(0, 0, tproc, 0, 0, &id);
if (!h) { MB("Thread creation failed."); exit(1); }
// tproc should run now and display msg boxes over and over..
for(;;);
exit(0);
}


There is something in MSDN.. can someone translate this for me? I don't
understand. I thought initializing happens in DllMain. How do I tell
Windows that initialization is done?

Quote:
Do not create a thread while impersonating another user. The call will
succeed, however the newly created thread will have reduced access
rights to itself when calling GetCurrentThread. The access rights
granted are derived from the access rights that the impersonated user
has to the process. Some access rights including
THREAD_SET_THREAD_TOKEN and THREAD_GET_CONTEXT may not be present,
leading to unexpected failures.

* During process startup and DLL initialization routines, new threads
can be created, but they do not begin execution until DLL
initialization is done for the process.
* Only one thread in a process can be in a DLL initialization or detach
routine at a time.
* ExitProcess does not return until no threads are in their DLL
initialization or detach routines.
 
C

CBFalconer

My main app calls LoadLibrary() to load a DLL, which calls
CreateThread(). This does NOT return NULL and I get a thread ID.
However, ThreadProc never gets executed when this code is in the DLL.
It works fine in the standalone EXE source.

I see no mention of LoadLibrary, DLL, CreateThread, etc. in the C
standard. I.E. this is off-topic in c.l.c. Follow-ups set.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top