Old 10-17-2012   #1
Registered User
Newbie Member
Join Date: Jun 2010
Posts: 3
syntax is an unknown quantity at this point
Q: Unloading/ejecting DLL

Dear Gtagaming community members,
Yesterday I have started scripting for GTA with the help of "the scripthook library".

Currently I inject my DLL's with a DLL injector ( pick any ).
Now here is the question I hope you can help me with:
"How do I Unload/Eject a DLL?". This is going to help me improve development time because I would simply unload the DLL and inject a modified version of the DLL instead of rebooting the game.

After a lot of research, here is what I have now ( this does not work )

#include <windows.h>

#include "CustomFiberThread.h"

#include "../ScriptHook/ScriptHookManager.h"

static CustomFiberThread _customThread;

//this is a reference to this DLL's Base address (if im correct), which I need later on in order to unload 
HMODULE DllHandle;

BOOL APIENTRY DllMain( HMODULE hModule, DWORD  fdwReason, LPVOID lpReserved)


    switch (fdwReason)
        case DLL_PROCESS_ATTACH: 
			//this is a reference to this DLL's Base address (if im correct), which I need later on in order to unload 
			DllHandle = hModule;
			ScriptHookManager::RegisterThread( &_customThread );

        case DLL_PROCESS_DETACH:

    return TRUE;
#include "globals.h"
#include "CustomFiberThread.h"
#include "Scripting.h"
#include "../ScriptHook/Log.h"
#include <cassert>
#include <windows.h>

// Pull in all our scripting functions/types
using namespace Scripting;

    // Give your own name here!

// Some helper functions

Player CustomFiberThread::GetPlayer()
	Player playerIndex = ConvertIntToPlayerIndex(GetPlayerId());
	return playerIndex;

Scripting::Ped CustomFiberThread::GetPlayerPed()
	Ped ped;
	GetPlayerChar(GetPlayer(), &ped);
	return ped;

// The real script

void CustomFiberThread::RunScript()
	// This is a fiber thread, so we use an loop to run the contents of this script.
	// The thread will terminate when we return from this function.

	Vehicle vehicle;
	Blip blip;


		if ((GetAsyncKeyState(VK_F9) & 1) != 0)

			u32 netID;
			Ped ped = GetPlayerPed();
			GetCarCharIsUsing(ped, &vehicle);

			GetNetworkIdFromVehicle(vehicle, &netID);
			AddBlipForCar(vehicle, &blip);

		if ((GetAsyncKeyState(VK_F11) & 1) != 0)
                    //Here The Dll should get unloaded, but it does not.
//the DllHandle variable is declared and set in Main.cpp
//I hope you know a fix! :(
			extern HMODULE DllHandle;
			 FreeLibraryAndExitThread(DllHandle, 0);
		// Call Wait() so we can process other scripts/game code
		// You must call Wait(...) in your loop code for a fiber thread!


Code explanation:
Main.cpp declares a HMODULE object called DllHandle. This will be used later in our code to eject
CustomFiberThread.cpp listens to a Keypress on F11. Then gets the DllHandle (external var) and calls FreeLibrarbyAndExitThread to remove the Dll From the process.

With this code, the Game just freezes when i hit F11.

Do you have any experience on how I should attack this problem? I sincerely hope you are able to help me.
Youve got my gratitude in advance,

[Update 1]
I found a dll injector that also has the ability to free the dll from the process.
Still would like to know how to do this manually in code ^^

Last edited by syntax; 10-17-2012 at 09:19 AM..
syntax is offline   Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

All times are GMT -6. The time now is 03:18 PM.