CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jan 2009
    Posts
    3

    DLL EntryPoint - Problem

    Hi!
    For some reason, my dll entryPoint doesn't enter to "DLL_THREAD_DETACH"
    and because of that i cant close the threads i opened.

    i am using these functions (taken from a class):
    Code:
    void myThread::startThread()
    {
    	this->stopFlag = CreateEvent(NULL,false,false,L"myEvent");
    	CreateThread(NULL,NULL,&myThread::threadProc,this,0,NULL);
    }
    void myThread::endThread()
    {
    	SetEvent(this->stopFlag);
    	CloseHandle(this->stopFlag);
    	CloseHandle(this->threadHandle);
    }
    void myThread::startMonitor()
    {
    	while(true)
    	{
    		Sleep(2000);
    		if (WAIT_OBJECT_0 == WaitForSingleObject(this->stopFlag,0))
    			break;
    	}
    	
    }
    DWORD WINAPI myThread::threadProc(LPVOID param)
    {
    	myThread* tmp = (myThread*)param;
    	tmp->startMonitor();
    	return 0;
    }
    
    and the entryPoint:
    
    myThread _monitor;
    BOOL APIENTRY DllMain( HMODULE hModule,
                           DWORD  ul_reason_for_call,
                           LPVOID lpReserved
    					 )
    {
    	switch (ul_reason_for_call)
    	{
    	case DLL_PROCESS_ATTACH:
    		_monitor.startThread();
    		break;
    	case DLL_THREAD_DETACH:
    		_monitor.endThread();
    		break;
    	case DLL_PROCESS_DETACH:
    		break;
    	}
    	return TRUE;
    }
    i cant get to the "_monitor.endThread()" function.

    tnx for the help and sorry for the lame english.
    Last edited by Andreas Masur; January 10th, 2009 at 10:00 AM. Reason: Added code tags...

  2. #2
    Join Date
    Nov 2003
    Posts
    1,902

    Re: DLL EntryPoint - Problem

    Please use code tags when posting code as described here: http://www.codeguru.com/forum/misc.php?do=bbcode#code

    You can edit your original post and add the code tags. You'll want to re-paste the code also since all indentation has been stripped.

    gg

  3. #3
    Join Date
    Jan 2009
    Posts
    8

    Re: DLL EntryPoint - Problem

    Hello,

    DLL_THREAD_DETACH is called when a thread is exiting in a clean manner. Do you have such a thread in your process you know it's doing this ?
    DLL_THREAD_DETACH is not called when a thread is terminated.

    I would suggest also to remove the Sleep(2000) line and move the 2000 timeout inside WaitForSingleObject() call, it's more efficient.

    Also, after SetEvent(this->stopFlag), I would check that the thread has exited before closing the event handle. (for example with a WaitForSingleObject(this->threadHandle, sometimeout))
    This is becouse the thread might be executing the Sleep(2000) while the event handle is closed, therefore WaitForSingleObject(this->stopFlag,0) would never return WAIT_OBJECT_0 and you'll never break the while() loop.

  4. #4
    Join Date
    Jan 2009
    Posts
    3

    Re: DLL EntryPoint - Problem

    The function which contains the while block runs in a separate thread.
    my goal is to break the while loop when the dll is detach.
    i want to close the thread by myself and not by the os(which will cause terminate).

    Code:
    void myThread::startMonitor()
    {
    	while(true)
    	{
    		//Do something every 2 seconds
    		if (WAIT_OBJECT_0 == WaitForSingleObject(this->stopFlag,2000))
    			break; //break the while and close the thread
    	}
    	
    }
    what i am trying to do here is to set the stopFlag (event) to Single when the process is detach.
    what i'm doing wrong?

    tnx for the help!

  5. #5
    Join Date
    Jan 2009
    Posts
    8

    Re: DLL EntryPoint - Problem

    The layout of your code is mostly Ok.
    However, if you want to cleanly stop the thread when the dll is unloaded, you should use DLL_PROCESS_DETATCH. So I would move this line _monitor.endThread() in DLL_PROCESS_DETACH. Remains valid the previous post about waiting for the thread's handle after you set the event to signaled, like so:

    void myThread::endThread()
    {
    SetEvent(this->stopFlag);
    WaitForSingleObject(this->threadHandle, sometimeout);
    CloseHandle(this->stopFlag);
    CloseHandle(this->threadHandle);
    }

    The purpose of that WaitFor... is to give some time to the thread before the stopFlag is closed.
    You could also use GetThreadExitCode() (or something like this), but in this case I don't think is worth it.

  6. #6
    Join Date
    Nov 2003
    Posts
    1,902

    Re: DLL EntryPoint - Problem

    >> my goal is to break the while loop when the dll is detach
    That would be DLL_PROCESS_DETACH. However, threads can not actually start or exit while in the context of DllMain. So you can signal the exit event, but there's no point in waiting on the thread handle because it will never become signaled - until your out of DllMain - but after a DLL_PROCESS_DETACH due to process termination, the thread's already been killed anyway.

    http://www.microsoft.com/whdc/driver..._bestprac.mspx

    gg

  7. #7
    Join Date
    Jan 2009
    Posts
    8

    Re: DLL EntryPoint - Problem

    Well, in light of the latest info provided by Codeplug, you'd probably be better off by adding some Initialize/Uninitialize functions to your dll, do thread signaling/waiting in Uninitialize() which you call before you exit the process.

  8. #8
    Join Date
    Jan 2009
    Posts
    3

    Re: DLL EntryPoint - Problem

    I'm stuck.

    I dont understand, how can i notify my theads something if they are
    already got killed.
    i need to close these threads when the dll is about to unload.
    when DLL_PROCESS_DETACH got called the threads are already dead.
    and for some reason DLL_THREAD_DETACH doesn't get called at all.

    what am i supposed to do?

    Thanks!

  9. #9
    Join Date
    Jan 2009
    Posts
    8

    Re: DLL EntryPoint - Problem

    Add 2 functions to your dll and export them.
    Initialize { monitor.startThread(); } and
    Uninitialize () {monitor.stopThread(); }.

    Call Initialize() from the process after the dll is loaded. Call Uninitialize() before you unload the dll.
    Thus you avoid DLL_ATACH/DETACH, and cleanly exit the thread.

  10. #10
    Join Date
    Nov 2003
    Posts
    1,902

    Re: DLL EntryPoint - Problem

    >> when DLL_PROCESS_DETACH got called the threads are already dead.
    That's only when the process is terminating. It could be the result of an explicit call to FreeLibrary() - in which case your thread would still be running.

    >> what am i supposed to do?
    Ideally, you would have a MyDll_Init() and MyDll_Shutdown() - which clients of the DLL would need to call.

    gg

  11. #11
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: DLL EntryPoint - Problem

    Quote Originally Posted by alon111 View Post
    what am i supposed to do?
    Control your dll load/unload. I can't believe the whole situation: you just allow your dll come and go. Instead you have to make a contract: the client app must initialize the dll before using it, and unitialize before unloading it. Explicitly.

    Well, in case you can't... Just see the sample.
    Attached Files Attached Files
    Best regards,
    Igor

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured