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

  • Home
  • SEARCH
  • 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 203675
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:23:59+00:00 2026-05-11T17:23:59+00:00

If I have a function foo() that windows has implemented in kernel32.dll and it

  • 0

If I have a function foo() that windows has implemented in kernel32.dll and it always returns true, can I have my program: “bar.exe” hook/detour that Windows function and make it return false for all processes instead?

So, if my svchost, for example, calls foo(), it will return false instead of true. The same action should be expected for all other processes currently running.

If so, how? I guess I’m looking for a system-wide hook or something.

  • 1 1 Answer
  • 0 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-11T17:23:59+00:00Added an answer on May 11, 2026 at 5:23 pm

    Take a look at Detours, it’s perfect for this sort of stuff.


    For system-wide hooking, read this article from MSDN.


    First, create a DLL which handles hooking the functions. This example below hooks the socket send and receive functions.

    #include <windows.h>
    #include <detours.h>
    
    #pragma comment( lib, "Ws2_32.lib" )
    #pragma comment( lib, "detours.lib" )
    #pragma comment( lib, "detoured.lib" )
    
    int ( WINAPI *Real_Send )( SOCKET s, const char *buf, int len, int flags ) = send;
    int ( WINAPI *Real_Recv )( SOCKET s, char *buf, int len, int flags ) = recv;  
    int WINAPI Mine_Send( SOCKET s, const char* buf, int len, int flags );
    int WINAPI Mine_Recv( SOCKET s, char *buf, int len, int flags );
    
    int WINAPI Mine_Send( SOCKET s, const char *buf, int len, int flags ) {
        // .. do stuff ..
    
        return Real_Send( s, buf, len, flags );
    }
    
    int WINAPI Mine_Recv( SOCKET s, char *buf, int len, int flags ) {
        // .. do stuff ..
    
        return Real_Recv( s, buf, len, flags );
    }
    
    BOOL WINAPI DllMain( HINSTANCE, DWORD dwReason, LPVOID ) {
        switch ( dwReason ) {
            case DLL_PROCESS_ATTACH:       
                DetourTransactionBegin();
                DetourUpdateThread( GetCurrentThread() );
                DetourAttach( &(PVOID &)Real_Send, Mine_Send );
                DetourAttach( &(PVOID &)Real_Recv, Mine_Recv );
                DetourTransactionCommit();
                break;
    
            case DLL_PROCESS_DETACH:
                DetourTransactionBegin();
                DetourUpdateThread( GetCurrentThread() );
                DetourDetach( &(PVOID &)Real_Send, Mine_Send );
                DetourDetach( &(PVOID &)Real_Recv, Mine_Recv );
                DetourTransactionCommit(); 
            break;
        }
    
        return TRUE;
    }
    

    Then, create a program to inject the DLL into the target application.

    #include <cstdio>
    #include <windows.h>
    #include <tlhelp32.h>
    
    void EnableDebugPriv() {
        HANDLE hToken;
        LUID luid;
        TOKEN_PRIVILEGES tkp;
    
        OpenProcessToken( GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, &hToken );
    
        LookupPrivilegeValue( NULL, SE_DEBUG_NAME, &luid );
    
        tkp.PrivilegeCount = 1;
        tkp.Privileges[0].Luid = luid;
        tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
    
        AdjustTokenPrivileges( hToken, false, &tkp, sizeof( tkp ), NULL, NULL );
    
        CloseHandle( hToken ); 
    }
    
    int main( int, char *[] ) {
        PROCESSENTRY32 entry;
        entry.dwSize = sizeof( PROCESSENTRY32 );
    
        HANDLE snapshot = CreateToolhelp32Snapshot( TH32CS_SNAPPROCESS, NULL );
    
        if ( Process32First( snapshot, &entry ) == TRUE ) {
            while ( Process32Next( snapshot, &entry ) == TRUE ) {
                if ( stricmp( entry.szExeFile, "target.exe" ) == 0 ) {
                    EnableDebugPriv();
    
                    char dirPath[MAX_PATH];
                    char fullPath[MAX_PATH];
    
                    GetCurrentDirectory( MAX_PATH, dirPath );
    
                    sprintf_s( fullPath, MAX_PATH, "%s\\DllToInject.dll", dirPath );
    
                    HANDLE hProcess = OpenProcess( PROCESS_CREATE_THREAD | PROCESS_VM_OPERATION | PROCESS_VM_WRITE, FALSE, entry.th32ProcessID );
                    LPVOID libAddr = (LPVOID)GetProcAddress( GetModuleHandle( "kernel32.dll" ), "LoadLibraryA" );
                    LPVOID llParam = (LPVOID)VirtualAllocEx( hProcess, NULL, strlen( fullPath ), MEM_RESERVE | MEM_COMMIT, PAGE_READWRITE );
    
                    WriteProcessMemory( hProcess, llParam, fullPath, strlen( fullPath ), NULL );
                    CreateRemoteThread( hProcess, NULL, NULL, (LPTHREAD_START_ROUTINE)libAddr, llParam, NULL, NULL );
                    CloseHandle( hProcess );
                }
            }
        }
    
        CloseHandle( snapshot );
    
        return 0;
    }
    

    This should be more than enough to get you started!

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

Sidebar

Ask A Question

Stats

  • Questions 215k
  • Answers 216k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Profile your application - if you are experiencing performance bottlenecks… May 12, 2026 at 11:04 pm
  • Editorial Team
    Editorial Team added an answer http://www.cs.bell-labs.com/cm/cs/pearls/ May 12, 2026 at 11:04 pm
  • Editorial Team
    Editorial Team added an answer This SO question should answer it. In short: The fn… May 12, 2026 at 11:04 pm

Related Questions

I have an ASP.net application that uses some common business object that we have
I'm trying to control the titles of my xterm windows and my cleverness has
I'm writing a C/C++ DLL and want to export certain functions which I've done
When debugging a VB6 application, I have noticed that the VB6 IDE keeps any

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.