求救:window c编程高手的请进
//main.c#define _UNICODE
#define UNICODE
#include<windows.h>
const int BREAK_POINT1 = 0x00405120;
const int BREAK_POINT2 = 0x00401000;
const int PATCH_POSITION=0x00401004;
int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
PSTR szCmdLine, int iCmdShow)
{
CONTEXT stCT; //线程环境
char buf[2]={0x0cc,0x60};
STARTUPINFO startupinfo;
PROCESS_INFORMATION processInfo;
DEBUG_EVENT devent;
GetStartupInfo(&startupinfo);
BOOL fOk=CreateProcess(
TEXT("D:\\masm32\\Resource\\Chapter13\\Patch2\\test.exe"),NULL,NULL,NULL,NULL,
DEBUG_PROCESS | DEBUG_ONLY_THIS_PROCESS,NULL,NULL, &startupinfo,&processInfo);
if(!fOk)
{
MessageBox(NULL,TEXT("装载文件失败."),NULL,MB_OK);
ExitProcess(0);
}
while(TRUE)
{
WaitForDebugEvent(&devent,INFINITE);//等待调试事件
if(devent.dwDebugEventCode==EXIT_PROCESS_DEBUG_EVENT)
break;
if(devent.dwDebugEventCode==CREATE_PROCESS_DEBUG_EVENT)
WriteProcessMemory(processInfo.hProcess,(LPVOID)BREAK_POINT1,buf,1,NULL);//写入一个
//0xCC(int 3的机器码)
else if(devent.dwDebugEventCode==EXCEPTION_DEBUG_EVENT)
{
if(devent.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_BREAKPOINT)
{
stCT.ContextFlags=CONTEXT_FULL;
GetThreadContext(processInfo.hThread,&stCT);
if(stCT.Eip==BREAK_POINT1+1)
{
--stCT.Eip;
WriteProcessMemory(processInfo.hProcess,(LPVOID)BREAK_POINT1,buf+1,1,NULL);
stCT.EFlags=stCT.EFlags|0x100;//regFlag的单步标志被置为1
SetThreadContext(processInfo.hThread,&stCT);
}
else if(devent.u.Exception.ExceptionRecord.ExceptionCode==EXCEPTION_SINGLE_STEP)
{
stCT.ContextFlags=CONTEXT_FULL;
SuspendThread(processInfo.hThread);
GetThreadContext(processInfo.hThread,&stCT);
ResumeThread(processInfo.hThread);
if(stCT.Eip==BREAK_POINT2)
{
*buf=0x90;
*(buf+1)=0x90;
WriteProcessMemory(processInfo.hProcess,(LPVOID)PATCH_POSITION,
buf,1,NULL);
}
else
{
stCT.EFlags=stCT.EFlags|0x100;//regFlag的单步标志被置为1
SuspendThread(processInfo.hThread);
SetThreadContext(processInfo.hThread,&stCT);
ResumeThread(processInfo.hThread);
}
}
}
}
else if(devent.dwDebugEventCode==LOAD_DLL_DEBUG_EVENT)
{
break;
}
else if(devent.dwDebugEventCode==EXIT_THREAD_DEBUG_EVENT)
{
break;
}
ContinueDebugEvent(devent.dwProcessId,devent.dwThreadId,DBG_CONTINUE);
}
CloseHandle(processInfo.hThread);
CloseHandle(processInfo.hProcess);
ExitProcess(0);
}
这是一个关于补丁的程序。要打补丁的程序是
程序的错误是在 WriteProcessMemory(processInfo.hProcess,(LPVOID)BREAK_POINT1,buf,1,NULL);//写入一个
//0xCC(int 3的机器码)
但我不知道为什么会错。请高手指点。