Cheat Engine Forum Index Cheat Engine
The Official Site of Cheat Engine
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 


Unload another dll with a dll?

 
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming
View previous topic :: View next topic  
Author Message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Sun Jan 04, 2009 1:19 pm    Post subject: Unload another dll with a dll? Reply with quote

Hey,

I'm trying to unload a dll in another process.
For that, I'm injecting this dll:

Code:
var
  tid: Cardinal;
  Hcepe: Cardinal;


begin
  Sleep(5000);
  //ShowMessage('lol');

    Hcepe := GetModuleHandle('cepe.dll');
  if Hcepe<>0 then
  begin
    DisableThreadLibraryCalls(Hcepe);
    FreeLibraryAndExitThread(Hcepe, 0);

    DisableThreadLibraryCalls(hInstance);
    FreeLibraryAndExitThread(hInstance, 0);
 end;
end.


But this crashs the whole application, why?
Back to top
View user's profile Send private message
DeletedUser14087
I post too much
Reputation: 2

Joined: 21 Jun 2006
Posts: 3069

PostPosted: Sun Jan 04, 2009 1:48 pm    Post subject: Reply with quote

Ok reakw0n, a description of my sample:

1)It will take a snapshot (with a Camera by canon) of all loaded modules of the injected target

2)It will loop module by module until it will meet the module you're looking for (look at const)

3)When the module is found, it will unload it

Code:
library ModuleUnloader;

uses
  Windows, //Windows Functions
  Tlhelp32; //Tool Help Functions

const //#define
  Module_To_Be_Seeked = 'cepe.dll'; //the module you're seeking for

type
 LPVOID = Pointer; //Define LPVOID as Pointer (Since Delphi doesn't support this shit)

 var
   hSnapShot: THandle; //Temp variable to store snapshot handle
   lpme: MODULEENTRY32; //Declare structure

{$R *.res}

function DllMain(hLibModule: HMODULE; dwReason: DWORD; lpvReserved: LPVOID): BOOL; //DllMain callback function declaration
begin
 case dwReason of //Switch/Case statement
 DLL_PROCESS_ATTACH: //This block of code will execute when the .dll is attached
  begin
   lpme.dwSize := sizeof( lpme ); //MSDN:  If you do not initialize dwSize, Module32First fails.
   DisableThreadLibraryCalls(hLibModule); //Disable the DLL_THREAD_ATTACH and DLL_THREAD_DETACH notifications
   hSnapShot := Tlhelp32.CreateToolhelp32Snapshot( TH32CS_SNAPMODULE, GetCurrentProcessId() ); //Take a snapshot of current process and store handle
   if Module32First(hSnapShot, lpme) <> False then //Check if the first module has been obtained successfully
    begin
      if lpme.szModule = Module_To_Be_Seeked then //check if the first obtained module is cepe.dll
       begin
        FreeLibraryAndExitThread( lpme.hModule, 0 );
        CloseHandle( lpme.hModule );
       end;
      while Module32Next( hSnapShot, lpme ) do //if not, it will keep looping until it finished looping all modules that are loaded on the process
      if lpme.szModule = Module_To_Be_Seeked then
       begin
        FreeLibraryAndExitThread( lpme.hModule, 0 );
        CloseHandle( lpme.hModule ); //for the sake of safety
       end;
    end;
  end;
 DLL_PROCESS_DETACH:
  begin
    CloseHandle( hsnapShot ); //When the .dll is detached (unloaded), we close the handle of temp Snapshot (avoid memory leaks)
  end;
 end;
Result := True;
end;

begin //Define entry-point for DllMain callback
DllProc := @DllMain;
DllProc(DLL_PROCESS_ATTACH);
end.
Back to top
View user's profile Send private message
Dark Byte
Site Admin
Reputation: 472

Joined: 09 May 2003
Posts: 25890
Location: The netherlands

PostPosted: Sun Jan 04, 2009 3:57 pm    Post subject: Reply with quote

try this: (assuming you already unhooked the hooked apis)

Code:

  while (freelibrary(hcepe)) do ; //free till the instance count is 0
  FreeLibraryAndExitThread(hInstance, 0); //exit yourself

_________________
Do not ask me about online cheats. I don't know any and wont help finding them.

Like my help? Join me on Patreon so i can keep helping
Back to top
View user's profile Send private message MSN Messenger
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Mon Jan 05, 2009 12:10 pm    Post subject: Reply with quote

Code:
    DisableThreadLibraryCalls(Hcepe);
    FreeLibraryAndExitThread(Hcepe, 0);

I think FreeLibraryAndExitThread is only meant for a dll that wants to unload itself, not unload another dll, since it'll terminate the calling thread. So do like Dark Byte said: free it using FreeLibrary, and it should automatically terminate.
Back to top
View user's profile Send private message
Reak
I post too much
Reputation: 0

Joined: 15 May 2007
Posts: 3496

PostPosted: Mon Jan 05, 2009 7:29 pm    Post subject: Reply with quote

Ok thank you all, but I fixed it with something else since this never really worked.
Back to top
View user's profile Send private message
dnsi0
I post too much
Reputation: 0

Joined: 04 Jan 2007
Posts: 2674

PostPosted: Tue Jan 06, 2009 7:36 pm    Post subject: Reply with quote

I think it crashed because you didn't unhook the apis that CEPE hooked. I made a program that hooks function but if u close the window, the app crashes because the app still has the jmp func. SO it jumps into inaccessible code so it fails.
Back to top
View user's profile Send private message
DeletedUser14087
I post too much
Reputation: 2

Joined: 21 Jun 2006
Posts: 3069

PostPosted: Wed Jan 07, 2009 5:31 am    Post subject: Reply with quote

hey, instead of FreeLibAndExThr, use FreeLibrary().
Back to top
View user's profile Send private message
Zand
Master Cheater
Reputation: 0

Joined: 21 Jul 2006
Posts: 424

PostPosted: Wed Jan 07, 2009 11:38 am    Post subject: Reply with quote

You can hack it up, making it possible for the DLL to unload itself - using something along the lines of

Code:
 _asm{
      push hDll
      push ExitThreadAddress
      jmp dword ptr [FreeLibrary]
   }



Credits to Darawk
Back to top
View user's profile Send private message
tombana
Master Cheater
Reputation: 2

Joined: 14 Jun 2007
Posts: 456
Location: The Netherlands

PostPosted: Wed Jan 07, 2009 11:53 am    Post subject: Reply with quote

Zand wrote:
You can hack it up, making it possible for the DLL to unload itself - using something along the lines of

Code:
 _asm{
      push hDll
      push ExitThreadAddress
      jmp dword ptr [FreeLibrary]
   }



Credits to Darawk

That will execute FreeLibrary with ExitThread as the return address, so that's exactly what FreeLibraryAndExitThread does, but I don't think it's the solution here since it's not it's own library. If the dll has a thread he should probably terminate it first and then call FreeLibrary till FreeLibrary returns 0.
Back to top
View user's profile Send private message
Display posts from previous:   
Post new topic   Reply to topic    Cheat Engine Forum Index -> General programming All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You can download files in this forum


Powered by phpBB © 2001, 2005 phpBB Group

CE Wiki   IRC (#CEF)   Twitter
Third party websites