[Injection] Unable to join threads menu

Shout-Out

User Tag List

Results 1 to 2 of 2
  1. #1
    Maeco's Avatar Member
    Reputation
    2
    Join Date
    May 2009
    Posts
    14
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    [Injection] Unable to join threads

    Hello guys,

    I got some trouble with ejecting my injected library. I use what I believe is the standard way of injection, by the use of CreateRemoteThread.
    That part works well, however when I want to eject, my module locks at a synchronization function (either SignalObjectAndWait or WaitForSingleObject).

    Here is the injected library entry point code:
    Code:
    HANDLE Thread	= NULL;
    HANDLE Quit	= NULL;
    
    // Entry point
    BOOL WINAPI DllMain(HINSTANCE instance, DWORD reason, LPVOID reserved)
    {
    	switch (reason)
    	{
    	case DLL_PROCESS_ATTACH:
    
    		// Create a manual reset event, used for exitting the thread
    		if ((Quit = CreateEvent(
    			NULL,
    			TRUE,
    			FALSE,
    			NULL)) == NULL)
    		{
    			DisplayError(L"Create event");
    			return FALSE;
    		}
    
    		// Create a thread that process the client requests
    		if ((Thread = CreateThread(
    			NULL,									// Default security attributes
    			NULL,									// Default stack size
    			Process,							// Thread procedure address
    			NULL,									// No argument(s) are passed
    			NULL,									// Run immediatly after creation
    			&ThreadID)) == NULL)
    		{
    			CloseHandle(Quit);
    			DisplayError(L"Create thread");
    			return FALSE;
    		}
    
    		break;
    
    	case DLL_PROCESS_DETACH:
    
    		// Signal the event and wait for the thread to return
    		SignalObjectAndWait(Quit, Thread, INFINITE, FALSE); // <== function does not return
    
    		// Close the thread
    		CloseHandle(Thread);
    
    		// Close the event
    		CloseHandle(Quit);
    
    		break;
    	}
    	
    	return TRUE;
    }
    Here is the thread entry point function:
    Code:
    extern HANDLE Quit;
    
    DWORD CALLBACK Process(LPVOID argument)
    {
    	HANDLE pipe = NULL;
    
    	while (WaitForSingleObject(Quit, 0) != WAIT_OBJECT_0)
    	{
    
    	}
    
    	return TRUE;
    }
    When I debug step through the Process-thread (after the Quit-event state has set) I see my that the Thread is exiting the while-loop and returns. However, the SignalObjectAndWait function in the DllMain function never returns after my thread has completed. Even if I replace the SignalObjectAndWait function call with the following code:

    Code:
    SetEvent(Quit);
    WaitForSingleObject(Thread, INFINITE);
    my program remains waiting till the Process-thread returns.

    Can anyone tell what I am doing wrong?

    Regards,

    Maeco

    [Injection] Unable to join threads
  2. #2
    Maeco's Avatar Member
    Reputation
    2
    Join Date
    May 2009
    Posts
    14
    Thanks G/R
    0/0
    Trade Feedback
    0 (0%)
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dang, I should have done more research on this on MSDN. It appears that you must not call a synchronisation function during DllMain, because you are creating a deadlock.

    When you create a thread during the DLL_PROCESS_ATTACH, the thread won't be executing until after the DllMain function has returned, I guess some synchronization is also done during the DLL_PROCESS_DETACH call.

    I am not sure if I should join my thread(s) during DLL_PROCESS_DETACH or just let them return and close their handle (prematurely).

Similar Threads

  1. Injecting code to main thread
    By kajko in forum WoW Memory Editing
    Replies: 4
    Last Post: 05-11-2015, 05:48 PM
  2. Unable to bump thread
    By nonanon in forum Report Bugs
    Replies: 6
    Last Post: 07-19-2013, 11:39 AM
  3. Executing injected code on main thread
    By mozartmclaus in forum Diablo 3 Memory Editing
    Replies: 0
    Last Post: 05-23-2012, 03:04 PM
  4. Edit Bug (Edit then Unable to find Thread)
    By pigchop in forum Report Bugs
    Replies: 2
    Last Post: 05-17-2011, 04:51 AM
All times are GMT -5. The time now is 11:40 PM. Powered by vBulletin® Version 4.2.3
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved. User Alert System provided by Advanced User Tagging (Pro) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Google Authenticator verification provided by Two-Factor Authentication (Free) - vBulletin Mods & Addons Copyright © 2025 DragonByte Technologies Ltd.
Digital Point modules: Sphinx-based search