microsoft / microsoft/Detours

Can I safely unload DLL after the detach hook?

Open
#364 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
C++
Stars
6.4k
Forks
1.2k
PR merge metrics
No merged PRs in 30d

Description

Currently, I inject a DLL into a running process. Inside the DLL, I use the Detours Hook API, and it works very well. Now I want to unload this DLL without interrupting the process, so I exported an unload function from the DLL for remote invocation. This function performs the following calls:

DetourTransactionBegin();
DetourUpdateThread(GetCurrentThread());
// some DetourDetach calls
DetourTransactionCommit();
FreeLibraryAndExitThread();

After I call this function, the DLL is indeed unloaded, but shortly afterward the target process crashes. It seems that the target process is still trying to access the DLL code that has already been unloaded.

What should I do to safely unload the DLL without affecting the target process?

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the DetourTransactionBegin, DetourUpdateThread, DetourDetach, DetourTransactionCommit, and FreeLibraryAndExitThread entry points, along with the Detours documentation on thread and DLL lifetimes. Reproduce the unload sequence and determine what conditions leave target execution dependent on the DLL; done means the DLL can be unloaded without a later process crash.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
devtools, operating-systems, reverse-engineering
Issue type
Bug
Difficulty
5/5
Estimated time
Over a week
Activity status
Quiet
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.