winAPI hooking trampoline does not work properly

I’am trying to hook a winAPI function and using a trampoline function but it doesnot work.
can some one spot the problem/
my code is using a patch with a jmp instruction to a trampoline function
the trampoline function have the original bytes of the winAPI function and then a jmp instruction to the original function after the patch.
my problem is that when runining the program i do get the output
“Hooked MessageBoxA called!” that mean the function does hooked but i dont see the msg box


#include <windows.h>
#include <iostream>

struct HookedFunction {
    FARPROC functionOriginalAddress;
    BYTE functionOriginalBytes[12];
    SIZE_T bytesRead;
    SIZE_T bytesWritten;
    int modifyTimes;
};

HookedFunction* MessageBoxAStruct;
LPVOID trampolineAddress;

// Define the hooked MessageBoxA function
int WINAPI HookedMessageBoxA(HWND hWnd, LPCSTR lpText, LPCSTR lpCaption, UINT uType) {
    // Custom behavior
    std::cout << "Hooked MessageBoxA called!" << std::endl;

    // Call the original MessageBoxA using the trampoline
    auto trampoline = (decltype(&MessageBoxA))trampolineAddress;
    return trampoline(hWnd, lpText, lpCaption, uType);
}

void HookMessageBoxA() {
    printf("hooking...n");

    MessageBoxAStruct = new HookedFunction;
    MessageBoxAStruct->bytesRead = 0;
    MessageBoxAStruct->bytesWritten = 0;

    // Get the handle to user32.dll (if it's already loaded)
    HMODULE library = GetModuleHandle(L"user32.dll");
    if (library == NULL) {
        std::cout << "Handle error - couldn't get the address: " << GetLastError() << std::endl;
        return;
    }

    if (MessageBoxAStruct->modifyTimes != 1)
    {
        printf("hooking...n");

        FARPROC MessageBoxAAddress = GetProcAddress(library, "MessageBoxA");
        // Check if the address is valid
        if (MessageBoxAAddress == NULL) {
            std::cout << "Failed to get address of MessageBoxA: " << GetLastError() << std::endl;
            return;
        }
        MessageBoxAStruct->functionOriginalAddress = MessageBoxAAddress;
        // Save the first 12 bytes of the original MessageBoxA function - will need for unhooking
        if (!ReadProcessMemory(GetCurrentProcess(), MessageBoxAAddress, MessageBoxAStruct->functionOriginalBytes, sizeof(MessageBoxAStruct->functionOriginalBytes), &MessageBoxAStruct->bytesRead)) {
            std::cout << "Failed to read original bytes of MessageBoxA: " << GetLastError() << std::endl;
            return;
        }

        MessageBoxAStruct->modifyTimes = 1;

        // Allocate memory for the trampoline function
        trampolineAddress = VirtualAlloc(NULL, sizeof(MessageBoxAStruct->functionOriginalBytes) + 14, MEM_COMMIT | MEM_RESERVE, PAGE_EXECUTE_READWRITE);
        if (trampolineAddress == NULL) {
            std::cout << "Failed to allocate memory for the trampoline: " << GetLastError() << std::endl;
            return;
        }

        // Copy original bytes to the trampoline
        if (!WriteProcessMemory(GetCurrentProcess(), trampolineAddress, MessageBoxAStruct->functionOriginalBytes, sizeof(MessageBoxAStruct->functionOriginalBytes), &MessageBoxAStruct->bytesWritten)) {
            std::cout << "Failed to write trampoline: " << GetLastError() << std::endl;
            return;
        }

        // Add a jump from the trampoline back to the original function's next instruction
        DWORD64 jumpBackAddress = (DWORD64)MessageBoxAAddress + sizeof(MessageBoxAStruct->functionOriginalBytes);
        unsigned char jumpBackPatch[] = {
            0x48, 0xb8,              // mov rax,
            0x00, 0x00, 0x00, 0x00,  // <address to jump back to>
            0x00, 0x00, 0x00, 0x00,  // <address to jump back to>
            0xFF, 0xE0               // jmp rax
        };
        *(DWORD64*)&jumpBackPatch[2] = jumpBackAddress;

        if (!WriteProcessMemory(GetCurrentProcess(), (LPVOID)((DWORD64)trampolineAddress + sizeof(MessageBoxAStruct->functionOriginalBytes)), jumpBackPatch, sizeof(jumpBackPatch), &MessageBoxAStruct->bytesWritten)) {
            std::cout << "Failed to write jump back in trampoline: " << GetLastError() << std::endl;
            return;
        }
    }

    // Create a patch "mov rax, <address of new MessageBoxA>; jmp rax"
    unsigned char patch[] = {
        0x48, 0xb8,             // mov rax,
        0x00, 0x00, 0x00, 0x00, // <address of hook>
        0x00, 0x00, 0x00, 0x00, // <address of hook>
        0xFF, 0xE0              // jmp rax
    };
    *(DWORD64*)&patch[2] = (DWORD64)HookedMessageBoxA;



    // Change memory protection to allow writing
    DWORD oldProtect;
    if (!VirtualProtect(MessageBoxAStruct->functionOriginalAddress, sizeof(patch), PAGE_EXECUTE_READWRITE, &oldProtect)) {
        std::cout << "Failed to change memory protection: " << GetLastError() << std::endl;
        return;
    }

    if (!WriteProcessMemory(GetCurrentProcess(), (LPVOID)MessageBoxAStruct->functionOriginalAddress, patch, sizeof(patch), &MessageBoxAStruct->bytesWritten)) {
        std::cout << "Failed to write patch: " << GetLastError() << std::endl;
        return;
    }

    // Restore original memory protection
    if (!VirtualProtect(MessageBoxAStruct->functionOriginalAddress, sizeof(patch), oldProtect, &oldProtect)) {
        std::cout << "Failed to restore memory protection: " << GetLastError() << std::endl;
        return;
    }

    printf("Hooked successfullyn");
    std::cout << "finished hooking...nn" << std::endl;
}

int main() {
    HookMessageBoxA();
    // Example call to the hooked function
    MessageBoxA(NULL, "Test", "Test", MB_OK);

    return 0;
}

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật