-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Proposal: Introduce DetourUpdateAllOtherThreads #233
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1524,6 +1524,28 @@ struct DetourThread | |
| { | ||
| DetourThread * pNext; | ||
| HANDLE hThread; | ||
| BOOL fCloseThreadHandleOnDestroy; | ||
|
|
||
| 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 | ||
|
|
@@ -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; | ||
|
|
||
|
|
@@ -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"); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)) { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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, | ||
|
|
||
There was a problem hiding this comment.
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.