c++winapidll-injection

Is there a way to print base address (hex) not as characters but as readable 0x00000 format in a messagebox


void PrintBaseAddr() {
    while (true) {
        if (GetAsyncKeyState(VK_F6) & 0x80000) {
            HMODULE BaseAddr = GetModuleHandleA(NULL);
            BaseAddr += 0x351333;


            MessageBoxA(NULL, (LPCSTR)BaseAddr, "Base Address", MB_OK);
        }
    }
}

so this is my code and the problem is, that BaseAddr is printed like this: output

how can i cast it to something like 0xABC345FF?


Solution

  • MessageBox expects to receive a string. Since you want to pass an address and render it as a hexadecimal number, you need to create a string holding the hexadecimal number first.

    void PrintBaseAddr() {
        while (true) {
            if (GetAsyncKeyState(VK_F6) & 0x80000) {
                HMODULE BaseAddr = GetModuleHandleA(NULL);
                BaseAddr += 0x351333;
    
                std::ostringsream buffer;
                buffer << (void *)BaseAddr;
    
                MessageBoxA(NULL, buffer.str().c_str(), "Base Address", MB_OK);
            }
        }
    }
    

    If you're using MessageBox quite a bit, it's sometimes useful to create a manipulator to handle this:

    class MessageBox {
        std::string title;
        int type;
    public:
        MessageBox(std::string const &title, int type = MB_OK) 
            : title(title), type(type) 
        { }
    
        friend std::ostream &operator<<(std::ostream &os, MessageBox const &m) {
            auto &oss = dynamic_cast<std::ostringstream &>(os);
    
            MessageBox(NULL, oss.str().c_str(), m.title, m.type);
            return os;
        }
    };
    

    You'd use this something like:

    std::ostringstream os;
    os << (void *)BaseAddress << MessageBox("Base Address");
    

    The good point here is that you get all the usual stream capabilities, so if you need to print out some random class you can't deal with directly (but provides an overloaded << operator) you can use it in a MessageBox.