Skip to content

Commit 4cfe39f

Browse files
committed
introduces DetourUpdateAllOtherThreads
1 parent 3326512 commit 4cfe39f

2 files changed

Lines changed: 74 additions & 0 deletions

File tree

src/detours.cpp

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1524,6 +1524,28 @@ struct DetourThread
15241524
{
15251525
DetourThread * pNext;
15261526
HANDLE hThread;
1527+
BOOL fCloseThreadHandleOnDestroy;
1528+
1529+
DetourThread()
1530+
{
1531+
pNext = NULL;
1532+
hThread = NULL;
1533+
fCloseThreadHandleOnDestroy = FALSE;
1534+
}
1535+
1536+
DetourThread(const DetourThread&) = delete;
1537+
DetourThread& operator= (const DetourThread&) = delete;
1538+
1539+
~DetourThread()
1540+
{
1541+
if (hThread) {
1542+
if (fCloseThreadHandleOnDestroy) {
1543+
CloseHandle(hThread);
1544+
}
1545+
1546+
hThread = NULL;
1547+
}
1548+
}
15271549
};
15281550

15291551
struct DetourOperation
@@ -1946,6 +1968,11 @@ typedef ULONG_PTR DETOURS_EIP_TYPE;
19461968
}
19471969

19481970
LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread)
1971+
{
1972+
return DetourUpdateThreadEx(hThread, FALSE);
1973+
}
1974+
1975+
LONG WINAPI DetourUpdateThreadEx(_In_ HANDLE hThread, _In_ BOOL fCloseThreadHandleOnDestroy)
19491976
{
19501977
LONG error;
19511978

@@ -1980,12 +2007,57 @@ LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread)
19802007
}
19812008

19822009
t->hThread = hThread;
2010+
t->fCloseThreadHandleOnDestroy = fCloseThreadHandleOnDestroy;
19832011
t->pNext = s_pPendingThreads;
19842012
s_pPendingThreads = t;
19852013

19862014
return NO_ERROR;
19872015
}
19882016

2017+
#ifndef NT_SUCCESS
2018+
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
2019+
#endif
2020+
2021+
#define STATUS_NO_MORE_ENTRIES 0x8000001A
2022+
2023+
typedef NTSTATUS(NTAPI *_NtGetNextThread)(
2024+
_In_ HANDLE ProcessHandle,
2025+
_In_ HANDLE ThreadHandle,
2026+
_In_ ACCESS_MASK DesiredAccess,
2027+
_In_ ULONG HandleAttributes,
2028+
_In_ ULONG Flags,
2029+
_Out_ PHANDLE NewThreadHandle
2030+
);
2031+
2032+
LONG WINAPI DetourUpdateAllOtherThreads()
2033+
{
2034+
_NtGetNextThread NtGetNextThread = (_NtGetNextThread)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), "NtGetNextThread");
2035+
if (!NtGetNextThread) {
2036+
DETOUR_TRACE("Failed to determine NtGetNextThread address.\r\n");
2037+
return GetLastError();
2038+
}
2039+
2040+
DWORD currentThreadId = GetCurrentThreadId();
2041+
2042+
HANDLE hThread = NULL;
2043+
for (;;) {
2044+
NTSTATUS status = NtGetNextThread(GetCurrentProcess(), hThread, THREAD_QUERY_LIMITED_INFORMATION | THREAD_SUSPEND_RESUME, 0, 0, &hThread);
2045+
2046+
if (!NT_SUCCESS(status)) {
2047+
if (status != STATUS_NO_MORE_ENTRIES) {
2048+
DETOUR_TRACE("Failed to enumerate process threads.\r\n");
2049+
return ERROR_FUNCTION_FAILED;
2050+
}
2051+
2052+
return NO_ERROR;
2053+
}
2054+
2055+
if (currentThreadId != GetThreadId(hThread)) {
2056+
DetourUpdateThreadEx(hThread, TRUE);
2057+
}
2058+
}
2059+
}
2060+
19892061
///////////////////////////////////////////////////////////// Transacted APIs.
19902062
//
19912063
LONG WINAPI DetourAttach(_Inout_ PVOID *ppPointer,

src/detours.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -558,6 +558,8 @@ LONG WINAPI DetourTransactionCommit(VOID);
558558
LONG WINAPI DetourTransactionCommitEx(_Out_opt_ PVOID **pppFailedPointer);
559559

560560
LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread);
561+
LONG WINAPI DetourUpdateThreadEx(_In_ HANDLE hThread, _In_ BOOL fCloseThreadHandleOnDestroy);
562+
LONG WINAPI DetourUpdateAllOtherThreads();
561563

562564
LONG WINAPI DetourAttach(_Inout_ PVOID *ppPointer,
563565
_In_ PVOID pDetour);

0 commit comments

Comments
 (0)