Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/detours.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1524,6 +1524,28 @@ struct DetourThread
{
DetourThread * pNext;
HANDLE hThread;
BOOL fCloseThreadHandleOnDestroy;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we're giving this class ownership semantics, delete the copy constructor and assignment operator.


DetourThread()
{
pNext = NULL;
hThread = NULL;
fCloseThreadHandleOnDestroy = FALSE;
}

DetourThread(const DetourThread&) = delete;
DetourThread& operator= (const DetourThread&) = delete;

~DetourThread()
{
if (hThread) {
if (fCloseThreadHandleOnDestroy) {
CloseHandle(hThread);
}

hThread = NULL;
}
}
};

struct DetourOperation
Expand Down Expand Up @@ -1946,6 +1968,11 @@ typedef ULONG_PTR DETOURS_EIP_TYPE;
}

LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread)
{
return DetourUpdateThreadEx(hThread, FALSE);
}

LONG WINAPI DetourUpdateThreadEx(_In_ HANDLE hThread, _In_ BOOL fCloseThreadHandleOnDestroy)
{
LONG error;

Expand Down Expand Up @@ -1980,12 +2007,57 @@ LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread)
}

t->hThread = hThread;
t->fCloseThreadHandleOnDestroy = fCloseThreadHandleOnDestroy;
t->pNext = s_pPendingThreads;
s_pPendingThreads = t;

return NO_ERROR;
}

#ifndef NT_SUCCESS
#define NT_SUCCESS(Status) (((NTSTATUS)(Status)) >= 0)
#endif

#define STATUS_NO_MORE_ENTRIES 0x8000001A

typedef NTSTATUS(NTAPI *_NtGetNextThread)(
_In_ HANDLE ProcessHandle,
_In_ HANDLE ThreadHandle,
_In_ ACCESS_MASK DesiredAccess,
_In_ ULONG HandleAttributes,
_In_ ULONG Flags,
_Out_ PHANDLE NewThreadHandle
);

LONG WINAPI DetourUpdateAllOtherThreads()
{
_NtGetNextThread NtGetNextThread = (_NtGetNextThread)GetProcAddress(GetModuleHandle(TEXT("ntdll.dll")), "NtGetNextThread");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Try avoiding undocumented APIs if possible: Windows 8.1 and up have PssCaptureSnapshot that you can dynamically query for. Using NtGetNextThread should be fine on older platforms.

if (!NtGetNextThread) {
DETOUR_TRACE("Failed to determine NtGetNextThread address.\r\n");
return GetLastError();
}

DWORD currentThreadId = GetCurrentThreadId();

HANDLE hThread = NULL;
for (;;) {
NTSTATUS status = NtGetNextThread(GetCurrentProcess(), hThread, THREAD_QUERY_LIMITED_INFORMATION | THREAD_SUSPEND_RESUME, 0, 0, &hThread);

if (!NT_SUCCESS(status)) {
if (status != STATUS_NO_MORE_ENTRIES) {
DETOUR_TRACE("Failed to enumerate process threads.\r\n");
return ERROR_FUNCTION_FAILED;
}

return NO_ERROR;
}

if (currentThreadId != GetThreadId(hThread)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GetThreadId was introduced in Vista but Detours supports XP. This won't compile. See #80.

DetourUpdateThreadEx(hThread, TRUE);
}
}
}

///////////////////////////////////////////////////////////// Transacted APIs.
//
LONG WINAPI DetourAttach(_Inout_ PVOID *ppPointer,
Expand Down
2 changes: 2 additions & 0 deletions src/detours.h
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,8 @@ LONG WINAPI DetourTransactionCommit(VOID);
LONG WINAPI DetourTransactionCommitEx(_Out_opt_ PVOID **pppFailedPointer);

LONG WINAPI DetourUpdateThread(_In_ HANDLE hThread);
LONG WINAPI DetourUpdateThreadEx(_In_ HANDLE hThread, _In_ BOOL fCloseThreadHandleOnDestroy);
LONG WINAPI DetourUpdateAllOtherThreads();

LONG WINAPI DetourAttach(_Inout_ PVOID *ppPointer,
_In_ PVOID pDetour);
Expand Down