@@ -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
15291551struct DetourOperation
@@ -1946,6 +1968,11 @@ typedef ULONG_PTR DETOURS_EIP_TYPE;
19461968}
19471969
19481970LONG 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//
19912063LONG WINAPI DetourAttach (_Inout_ PVOID *ppPointer,
0 commit comments