CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    Jan 2009
    Posts
    10

    Talking sample source MessageBox API Hook

    i am trying Hook API,are you help me???,source or code

  2. #2
    Join Date
    Dec 2008
    Posts
    114

    Re: sample source MessageBox API Hook

    Click on "Search" on Google or Google Groups.
    Hundreds of complete samples... (C, C++, asm Win32, etc)

  3. #3
    Join Date
    Feb 2003
    Location
    Iasi - Romania
    Posts
    8,244

    Re: sample source MessageBox API Hook

    It's not very clear what exactly you want to accomplish.
    However, I hope this article from Codeguru can be a good starting point: Fancy Custom MessageBox.
    Ovidiu
    "When in Rome, do as Romans do."
    My latest articles: https://codexpertro.wordpress.com/

  4. #4
    Join Date
    Apr 2004
    Posts
    102

    Re: sample source MessageBox API Hook

    Here is a sample of hooking MessageBox using Microsoft's Microsoft Detours

    Code:
    #pragma comment(lib, "detours.lib")
    #pragma comment(lib, "detoured.lib")
    #pragma comment(lib, "user32.lib")
    
    #include <windows.h>
    #include <stdio.h>
    #include "detours.h"
    
    #define ID_BUTTONMESSAGE  200
    
    INT (WINAPI * trueMessageBox)(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) = MessageBox;
    
    INT WINAPI hookedMessageBox(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType)
    {
        LPCSTR lpNewCaption = "You've been hijacked";
        int iReturn = trueMessageBox(hWnd, lpText, lpNewCaption, uType);
        return  iReturn;
    }
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM) ;
    
    VOID SetupDetour(void)
    {
        DWORD dwError;
        DetourRestoreAfterWith();
        DetourTransactionBegin();
        DetourUpdateThread(GetCurrentThread());
        DetourAttach(&(PVOID&)trueMessageBox, hookedMessageBox);
        dwError = DetourTransactionCommit();
        if (dwError == NO_ERROR)
            MessageBox(NULL, "MessageBox hook installed successfully", "HOOK", MB_OK);
        else
            MessageBox(NULL, "MessageBox hook installation failed", "Error", MB_OK);
    }
    
    INT WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
                        PSTR szCmdLine, int iCmdShow)
    {
        static TCHAR szAppName[] = TEXT ("MessageBox Hook") ;
        HWND         hwnd ;
        MSG          msg ;
        WNDCLASS     wndclass ;
    
        wndclass.style         = CS_HREDRAW | CS_VREDRAW ;
        wndclass.lpfnWndProc   = WndProc ;
        wndclass.cbClsExtra    = 0 ;
        wndclass.cbWndExtra    = 0 ;
        wndclass.hInstance     = hInstance ;
        wndclass.hIcon         = LoadIcon (NULL, IDI_APPLICATION) ;
        wndclass.hCursor       = LoadCursor (NULL, IDC_ARROW) ;
        wndclass.hbrBackground = (HBRUSH) GetSysColorBrush(COLOR_3DFACE) ;
        wndclass.lpszMenuName  = NULL ;
        wndclass.lpszClassName = szAppName ;
    
        if (!RegisterClass (&wndclass))
        {
            MessageBox (NULL, TEXT ("This program requires Windows NT!"), 
                szAppName, MB_ICONERROR) ;
            return 0 ;
        }
        hwnd = CreateWindow (szAppName,                  
            TEXT ("MessageBox Hook"), 
            WS_OVERLAPPEDWINDOW,        
            CW_USEDEFAULT,              
            CW_USEDEFAULT,              
            700,              
            300,             
            NULL,                       
            NULL,                       
            hInstance,                  
            NULL) ;                     
    
        ShowWindow (hwnd, iCmdShow) ;
        UpdateWindow (hwnd) ;
    
        while (GetMessage (&msg, NULL, 0, 0))
        {
            TranslateMessage (&msg) ;
            DispatchMessage (&msg) ;
        }
        return msg.wParam ;
    }
    
    LRESULT CALLBACK WndProc (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
    {
        switch (message)
        {
            case WM_CREATE:
                CreateWindow( "button", "Display MessageBox",    
                    WS_VISIBLE | WS_CHILD ,
                    200, 50, 180, 25,        
                    hwnd, (HMENU) ID_BUTTONMESSAGE, NULL, NULL);
                SetupDetour();
                return 0 ;
            case WM_COMMAND:
                switch(LOWORD(wParam))
                {
                    case ID_BUTTONMESSAGE:
                        MessageBox(hwnd, "This is a test of the hook, note caption", "Test", MB_OK);
                       break;
                }
                break;;
            case WM_DESTROY:
                PostQuitMessage (0) ;
                return 0 ;
        }
        return DefWindowProc (hwnd, message, wParam, lParam) ;
    }

  5. #5
    Join Date
    Sep 2007
    Location
    poland|wrocław
    Posts
    47

    Re: sample source MessageBox API Hook


  6. #6
    Join Date
    Jan 2012
    Posts
    1

    Re: sample source MessageBox API Hook

    @BobS0327 Hi I managed to get the above code working, though I would like to know if there is a way to make this work across different processes e.g. notepad.exe?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured