MCPcopy Create free account
hub / github.com/JochenKalmbach/StackWalker / PreventSetUnhandledExceptionFilter

Function PreventSetUnhandledExceptionFilter

Main/StackWalker/main.cpp:89–124  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

87
88#if defined(_M_X64) || defined(_M_IX86)
89static BOOL PreventSetUnhandledExceptionFilter()
90{
91 HMODULE hKernel32 = LoadLibrary(_T("kernel32.dll"));
92 if (hKernel32 == NULL)
93 return FALSE;
94 void* pOrgEntry = GetProcAddress(hKernel32, "SetUnhandledExceptionFilter");
95 if (pOrgEntry == NULL)
96 return FALSE;
97
98#ifdef _M_IX86
99 // Code for x86:
100 // 33 C0 xor eax,eax
101 // C2 04 00 ret 4
102 unsigned char szExecute[] = {0x33, 0xC0, 0xC2, 0x04, 0x00};
103#elif _M_X64
104 // 33 C0 xor eax,eax
105 // C3 ret
106 unsigned char szExecute[] = {0x33, 0xC0, 0xC3};
107#else
108#error "The following code only works for x86 and x64!"
109#endif
110
111 DWORD dwOldProtect = 0;
112 BOOL bProt = VirtualProtect(pOrgEntry, sizeof(szExecute), PAGE_EXECUTE_READWRITE, &dwOldProtect);
113
114 SIZE_T bytesWritten = 0;
115 BOOL bRet = WriteProcessMemory(GetCurrentProcess(), pOrgEntry, szExecute, sizeof(szExecute),
116 &bytesWritten);
117
118 if ((bProt != FALSE) && (dwOldProtect != PAGE_EXECUTE_READWRITE))
119 {
120 DWORD dwBuf;
121 VirtualProtect(pOrgEntry, sizeof(szExecute), dwOldProtect, &dwBuf);
122 }
123 return bRet;
124}
125#else
126#pragma message("This code works only for x86 and x64!")
127#endif

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected