Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 732447
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T07:08:36+00:00 2026-05-14T07:08:36+00:00

I have a few quick questions about the Microsoft Detours Library. I have used

  • 0

I have a few quick questions about the Microsoft Detours Library. I have used it before (successfully), but I just had a thought about this function:

LONG DetourUpdateThread(HANDLE hThread);

I read elsewhere that this function will actually suspend the thread until the transaction completes. This seems odd since most sample code calls:

DetourUpdateThread(GetCurrentThread());

Anyway, apparently this function “enlists” threads so that, when the transaction commits (and the detours are made), their instruction pointers are modified if they lie “within the rewritten code in either the target function or the trampoline function.”

My questions are:

When the transaction commits, is the current thread’s instruction pointer going to be within the DetourTransactionCommit function? If so, why should we bother enlisting it to be updated?

Also, if the enlisted threads are suspended, how can the current thread continue executing (given that most sample code calls DetourUpdateThread(GetCurrentThread());)?

Finally, could you suspend all threads for the current process, avoiding race conditions (considering that threads could be getting created and destroyed at any time)? Perhaps this is done when the transaction begins? This would allow us to enumerate threads more safely (as it seems less likely that new threads could be created), although what about CreateRemoteThread()?

Thanks,

Paul

For reference, here is an extract from the simple sample:

// DllMain function attaches and detaches the TimedSleep detour to the
// Sleep target function.  The Sleep target function is referred to
// through the TrueSleep target pointer.
BOOL WINAPI DllMain(HINSTANCE hinst, DWORD dwReason, LPVOID reserved)
{
    if (dwReason == DLL_PROCESS_ATTACH) {
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)TrueSleep, TimedSleep);
        DetourTransactionCommit();
    }
    else if (dwReason == DLL_PROCESS_DETACH) {
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourDetach(&(PVOID&)TrueSleep, TimedSleep);
        DetourTransactionCommit();
    }
    return TRUE;
}
  • 1 1 Answer
  • 2 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T07:08:36+00:00Added an answer on May 14, 2026 at 7:08 am

    How embaressing:
    I forgot that the source was available!

    DetourUpdateThread silently ignores the enlisting of the current thread. Otherwise, the given thread is suspended. I wonder why ALL code examples enlist the current thread anyway! This answers the first 2 questions.

    As for the 3rd question:
    I found another detouring library that attempts to suspend all threads by doing the following:

    1. Get snapshot of all threads

    2. Loop through the snapshot and suspend threads that we have not already suspended.

    3. If threads were suspended, then go back to 1 (we still keep track of threads that we have suspended). If no threads were suspended then we are done.

    I think the assumption is that if we can loop through all threads and they are all already suspended (i.e. from before we took the snapshot), then no more threads can have been created. Not so sure about CreateRemoteThread though!

    Edit: Re: CreateRemoteThread.

    “Only one thread in a process can be in a DLL initialization or detach routine at a time.”
    CreateRemoteThread “results in a call to the entry point of each DLL in the process”.
    http://msdn.microsoft.com/en-us/library/ms682437%28VS.85%29.aspx

    A new thread cannot start executing if you are in a DllMain function (as long as the new thread has not yet caused the calling the entry point of each DLL in the process).
    So if you apply your detours within a DllMain function, you may just be safe from the race condition of a new remote thread being created and having its instruction pointer within your rewritten target/trampoline function.

    Thanks,

    Paul

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a few quick questions about the iPhone software development. I did some
I just had a quick phone interview. The interviewer asked me a few questions
Just a few high-level, hopefully very quick questions: 1) If I have a class
I have few questions about ssis transction isolation levels. consider a scenario:I have an
I just have a quick question about how to generate id's on-the-fly for HTML
First some background on the questions. I have never used ravedb before and I'm
I have a quick question about HTML and CSS. I thought this would be
I have a few questions about web workers Does the worker have access to
I'm learning Spring Security and I have few quick questions respect UserDetailsService : 1-
i have few confusion regarding ORM(Object Relational Mapping) i am thinking about to use

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.