WIN32 program Hook SetLastError, output error description to console

Source: Internet
Author: User
Tags exit win32

The Windows mode application (GUI) enables the console by:

Steps Method
1 Startup/Shutdown console AllocConsole ()

Freeconsole ()

2 redirect input/output Freopen ("conin$", "R", stdin)

Freopen ("conout$", "w", stdout)

Freopen ("conout$", "W", stderr)

3 Console input/output #include <conio.h>

#include <stdio.h>

printf (...)

scanf (...)

System ("Pause")

Two, the hook API function simple method is:

1. In debug mode, the function name value is the address of the instruction "jmp function body". The instruction format is "e9----" and the accompanying parameter is a four-byte transfer offset. Therefore, the function name Value + * (dword*) (DWORD) function name value + 1) is the function body entry address. "Use go To Disassembly" function calculates the function body entrance stack instruction length, obtains the actual entry address is "function name Value + * (dword*) ((DWORD) function name value + 1) + The entrance stack instruction length";

2. In release mode, the function name value is directly the entry address of the function body. Using the function of "go to disassembly" to compute the instruction length of function body except exit instruction, the function exit address is "function name + instruction Length", and API function is this mode.

3. Use ":: WriteProcessMemory (:: GetCurrentProcess (), API function export Address ...)" method to hook up the following calls on an API function:

Serial number Description Instructions Parameter values
1 Call hook function e8---- hook function Body Actual entry address
2 Exit c2-- Total length of function parameters to restore the state of the stack

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.