分类: C/C++
2008-03-17 11:30:22
一、窗口模式应用程序(GUI)启用控制台的方法为:
步骤 | 方法 |
1 启动/关闭控制台 | AllocConsole() FreeConsole() |
2 重定向输入/输出 | freopen("CONIN$","r",stdin) freopen("CONOUT$","w",stdout) freopen("CONOUT$","w",stderr) |
3 控制台输入/输出 | #include #include printf(...) scanf(...) system("pause") |
序号 | 说明 | 指令 | 参数值 |
1 | 调用挂钩函数 | E8 □□□□ | 挂钩函数体实际入口地址 |
2 | 退出 | C2 □□ | 函数参数总长度,用于恢复栈的状态 |
#include(全文完)#include void hook_SetLastError()//为简化调用挂钩函数时的栈操作,挂钩函数无参数和返回值。 { if (::GetLastError()) { LPVOID lpMsgBuf = 0; if (::FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_IGNORE_INSERTS, 0, ::GetLastError(), LANG_USER_DEFAULT, (LPTSTR) &lpMsgBuf, 0, 0)) { ::printf("ERROR: %d %s", ::GetLastError(), (LPCSTR)lpMsgBuf); ::LocalFree(lpMsgBuf); } } } int __stdcall WinMain(HINSTANCE, HINSTANCE, LPSTR, int) { unsigned char setup_SetLastError[8] = {0xE8, 0, 0, 0, 0, 0xC2, 4, 0}; #ifdef _DEBUG *(unsigned int*)(setup_SetLastError + 1) = (unsigned int)hook_SetLastError + *(unsigned int*)((unsigned char*)hook_SetLastError + 1) - (unsigned int)SetLastError - 18; #else *(unsigned int*)(setup_SetLastError + 1) = (unsigned int)hook_SetLastError - (unsigned int)SetLastError - 23; #endif ::WriteProcessMemory(::GetCurrentProcess(), (LPVOID)((unsigned int)::SetLastError + 18), setup_SetLastError, 8, new SIZE_T); ::AllocConsole(); ::freopen("CONIN$", "r", stdin); ::freopen("CONOUT$", "w", stdout); //此处添加自己的代码 ::WriteProcessMemory(::GetCurrentProcess(), (LPVOID)((unsigned int)::SetLastError + 18), setup_SetLastError + 5, 3, new SIZE_T); ::system("pause"); return 0; }