Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.
Path: blob/master/external/source/byakugan/detours/modules.cpp
Views: 11778
//////////////////////////////////////////////////////////////////////////////1//2// Module Enumeration Functions (modules.cpp of detours.lib)3//4// Microsoft Research Detours Package, Version 2.1.5//6// Copyright (c) Microsoft Corporation. All rights reserved.7//8// Module enumeration functions.9//1011#include <windows.h>12#if (_MSC_VER < 1310)13#else14#include <strsafe.h>15#endif1617//#define DETOUR_DEBUG 118#define DETOURS_INTERNAL19#include "detours.h"2021//////////////////////////////////////////////////////////////////////////////22//23#ifndef _STRSAFE_H_INCLUDED_24static inline HRESULT StringCchLengthA(const char* psz, size_t cchMax, size_t* pcch)25{26HRESULT hr = S_OK;27size_t cchMaxPrev = cchMax;2829if (cchMax > 2147483647)30{31return ERROR_INVALID_PARAMETER;32}3334while (cchMax && (*psz != '\0'))35{36psz++;37cchMax--;38}3940if (cchMax == 0)41{42// the string is longer than cchMax43hr = ERROR_INVALID_PARAMETER;44}4546if (SUCCEEDED(hr) && pcch)47{48*pcch = cchMaxPrev - cchMax;49}5051return hr;52}535455static inline HRESULT StringCchCopyA(char* pszDest, size_t cchDest, const char* pszSrc)56{57HRESULT hr = S_OK;5859if (cchDest == 0)60{61// can not null terminate a zero-byte dest buffer62hr = ERROR_INVALID_PARAMETER;63}64else65{66while (cchDest && (*pszSrc != '\0'))67{68*pszDest++ = *pszSrc++;69cchDest--;70}7172if (cchDest == 0)73{74// we are going to truncate pszDest75pszDest--;76hr = ERROR_INVALID_PARAMETER;77}7879*pszDest= '\0';80}8182return hr;83}8485static inline HRESULT StringCchCatA(char* pszDest, size_t cchDest, const char* pszSrc)86{87HRESULT hr;88size_t cchDestCurrent;8990if (cchDest > 2147483647)91{92return ERROR_INVALID_PARAMETER;93}9495hr = StringCchLengthA(pszDest, cchDest, &cchDestCurrent);9697if (SUCCEEDED(hr))98{99hr = StringCchCopyA(pszDest + cchDestCurrent,100cchDest - cchDestCurrent,101pszSrc);102}103104return hr;105}106107#endif108109//////////////////////////////////////////////////////////////////////////////110//111const GUID DETOUR_EXE_RESTORE_GUID = {1120x2ed7a3ff, 0x3339, 0x4a8d,113{ 0x80, 0x5c, 0xd4, 0x98, 0x15, 0x3f, 0xc2, 0x8f }};114115//////////////////////////////////////////////////////////////////////////////116//117PDETOUR_SYM_INFO DetourLoadImageHlp(VOID)118{119static DETOUR_SYM_INFO symInfo;120static PDETOUR_SYM_INFO pSymInfo = NULL;121static BOOL failed = false;122123if (failed) {124return NULL;125}126if (pSymInfo != NULL) {127return pSymInfo;128}129130ZeroMemory(&symInfo, sizeof(symInfo));131// Create a real handle to the process.132#if 0133DuplicateHandle(GetCurrentProcess(),134GetCurrentProcess(),135GetCurrentProcess(),136&symInfo.hProcess,1370,138FALSE,139DUPLICATE_SAME_ACCESS);140#else141symInfo.hProcess = GetCurrentProcess();142#endif143144symInfo.hDbgHelp = LoadLibraryA("dbghelp.dll");145if (symInfo.hDbgHelp == NULL) {146abort:147failed = true;148if (symInfo.hDbgHelp != NULL) {149FreeLibrary(symInfo.hDbgHelp);150}151symInfo.pfImagehlpApiVersionEx = NULL;152symInfo.pfSymInitialize = NULL;153symInfo.pfSymSetOptions = NULL;154symInfo.pfSymGetOptions = NULL;155symInfo.pfSymLoadModule64 = NULL;156symInfo.pfSymGetModuleInfo64 = NULL;157symInfo.pfSymFromName = NULL;158return NULL;159}160161symInfo.pfImagehlpApiVersionEx162= (PF_ImagehlpApiVersionEx)GetProcAddress(symInfo.hDbgHelp,163"ImagehlpApiVersionEx");164symInfo.pfSymInitialize165= (PF_SymInitialize)GetProcAddress(symInfo.hDbgHelp, "SymInitialize");166symInfo.pfSymSetOptions167= (PF_SymSetOptions)GetProcAddress(symInfo.hDbgHelp, "SymSetOptions");168symInfo.pfSymGetOptions169= (PF_SymGetOptions)GetProcAddress(symInfo.hDbgHelp, "SymGetOptions");170symInfo.pfSymLoadModule64171= (PF_SymLoadModule64)GetProcAddress(symInfo.hDbgHelp, "SymLoadModule64");172symInfo.pfSymGetModuleInfo64173= (PF_SymGetModuleInfo64)GetProcAddress(symInfo.hDbgHelp, "SymGetModuleInfo64");174symInfo.pfSymFromName175= (PF_SymFromName)GetProcAddress(symInfo.hDbgHelp, "SymFromName");176177API_VERSION av;178ZeroMemory(&av, sizeof(av));179av.MajorVersion = API_VERSION_NUMBER;180181if (symInfo.pfImagehlpApiVersionEx == NULL ||182symInfo.pfSymInitialize == NULL ||183symInfo.pfSymLoadModule64 == NULL ||184symInfo.pfSymGetModuleInfo64 == NULL ||185symInfo.pfSymFromName == NULL) {186goto abort;187}188189symInfo.pfImagehlpApiVersionEx(&av);190if (av.MajorVersion < API_VERSION_NUMBER) {191goto abort;192}193194if (!symInfo.pfSymInitialize(symInfo.hProcess, NULL, FALSE)) {195// We won't retry the initialize if it fails.196goto abort;197}198199if (symInfo.pfSymGetOptions != NULL && symInfo.pfSymSetOptions != NULL) {200DWORD dw = symInfo.pfSymGetOptions();201202dw &= ~(SYMOPT_CASE_INSENSITIVE |203SYMOPT_UNDNAME |204SYMOPT_DEFERRED_LOADS |2050);206dw |= (207#if defined(SYMOPT_EXACT_SYMBOLS)208SYMOPT_EXACT_SYMBOLS |209#endif210#if defined(SYMOPT_NO_UNQUALIFIED_LOADS)211SYMOPT_NO_UNQUALIFIED_LOADS |212#endif213SYMOPT_DEFERRED_LOADS |214#if defined(SYMOPT_FAIL_CRITICAL_ERRORS)215SYMOPT_FAIL_CRITICAL_ERRORS |216#endif217#if defined(SYMOPT_INCLUDE_32BIT_MODULES)218SYMOPT_INCLUDE_32BIT_MODULES |219#endif2200);221symInfo.pfSymSetOptions(dw);222}223224pSymInfo = &symInfo;225return pSymInfo;226}227228PVOID WINAPI DetourFindFunction(PCSTR pszModule, PCSTR pszFunction)229{230/////////////////////////////////////////////// First, try GetProcAddress.231//232HMODULE hModule = LoadLibraryA(pszModule);233if (hModule == NULL) {234return NULL;235}236237PBYTE pbCode = (PBYTE)GetProcAddress(hModule, pszFunction);238if (pbCode) {239return pbCode;240}241242////////////////////////////////////////////////////// Then try ImageHelp.243//244DETOUR_TRACE(("DetourFindFunction(%s, %s)\n", pszModule, pszFunction));245PDETOUR_SYM_INFO pSymInfo = DetourLoadImageHlp();246if (pSymInfo == NULL) {247DETOUR_TRACE(("DetourLoadImageHlp failed: %d\n",248GetLastError()));249return NULL;250}251252if (pSymInfo->pfSymLoadModule64(pSymInfo->hProcess, NULL,253(PCHAR)pszModule, NULL,254(DWORD64)hModule, 0) == 0) {255DETOUR_TRACE(("SymLoadModule64(%p) failed: %d\n",256pSymInfo->hProcess, GetLastError()));257// We don't stop because some version of dbghelp fail secondary calls.258//return NULL;259}260261HRESULT hrRet;262CHAR szFullName[512];263IMAGEHLP_MODULE64 modinfo;264ZeroMemory(&modinfo, sizeof(modinfo));265modinfo.SizeOfStruct = sizeof(modinfo);266if (!pSymInfo->pfSymGetModuleInfo64(pSymInfo->hProcess, (DWORD64)hModule, &modinfo)) {267DETOUR_TRACE(("SymGetModuleInfo64(%p, %p) failed: %d\n",268pSymInfo->hProcess, hModule, GetLastError()));269return NULL;270}271272hrRet = StringCchCopyA(szFullName, sizeof(szFullName)/sizeof(CHAR), modinfo.ModuleName);273if (FAILED(hrRet)) {274DETOUR_TRACE(("StringCchCopyA failed: %08x\n", hrRet));275return NULL;276}277hrRet = StringCchCatA(szFullName, sizeof(szFullName)/sizeof(CHAR), "!");278if (FAILED(hrRet)) {279DETOUR_TRACE(("StringCchCatA failed: %08x\n", hrRet));280return NULL;281}282hrRet = StringCchCatA(szFullName, sizeof(szFullName)/sizeof(CHAR), pszFunction);283if (FAILED(hrRet)) {284DETOUR_TRACE(("StringCchCatA failed: %08x\n", hrRet));285return NULL;286}287288struct CFullSymbol : SYMBOL_INFO {289CHAR szRestOfName[512];290} symbol;291ZeroMemory(&symbol, sizeof(symbol));292//symbol.ModBase = (ULONG64)hModule;293symbol.SizeOfStruct = sizeof(SYMBOL_INFO);294#ifdef DBHLPAPI295symbol.MaxNameLen = sizeof(symbol.szRestOfName)/sizeof(symbol.szRestOfName[0]);296#else297symbol.MaxNameLength = sizeof(symbol.szRestOfName)/sizeof(symbol.szRestOfName[0]);298#endif299300if (!pSymInfo->pfSymFromName(pSymInfo->hProcess, szFullName, &symbol)) {301DETOUR_TRACE(("SymFromName(%s) failed: %d\n", szFullName, GetLastError()));302return NULL;303}304305#ifdef DETOURS_IA64306// On the IA64, we get a raw code pointer from the symbol engine307// and have to convert it to a wrapped [code pointer, global pointer].308//309PPLABEL_DESCRIPTOR pldEntry = (PPLABEL_DESCRIPTOR)DetourGetEntryPoint(hModule);310PPLABEL_DESCRIPTOR pldSymbol = new PLABEL_DESCRIPTOR;311312pldSymbol->EntryPoint = symbol.Address;313pldSymbol->GlobalPointer = pldEntry->GlobalPointer;314return (PBYTE)pldSymbol;315#else316return (PBYTE)symbol.Address;317#endif318}319320//////////////////////////////////////////////////// Module Image Functions.321//322HMODULE WINAPI DetourEnumerateModules(HMODULE hModuleLast)323{324PBYTE pbLast;325326if (hModuleLast == NULL) {327pbLast = (PBYTE)0x10000;328}329else {330pbLast = (PBYTE)hModuleLast + 0x10000;331}332333MEMORY_BASIC_INFORMATION mbi;334ZeroMemory(&mbi, sizeof(mbi));335336// Find the next memory region that contains a mapped PE image.337//338for (;; pbLast = (PBYTE)mbi.BaseAddress + mbi.RegionSize) {339if (VirtualQuery((PVOID)pbLast, &mbi, sizeof(mbi)) <= 0) {340break;341}342343// Skip uncommitted regions and guard pages.344//345if ((mbi.State != MEM_COMMIT) || (mbi.Protect & PAGE_GUARD)) {346continue;347}348349__try {350PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)pbLast;351if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {352continue;353}354355PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader +356pDosHeader->e_lfanew);357if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) {358continue;359}360361return (HMODULE)pDosHeader;362}363__except(EXCEPTION_EXECUTE_HANDLER) {364return NULL;365}366}367return NULL;368}369370PVOID WINAPI DetourGetEntryPoint(HMODULE hModule)371{372PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;373if (hModule == NULL) {374pDosHeader = (PIMAGE_DOS_HEADER)GetModuleHandle(NULL);375}376377__try {378if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {379SetLastError(ERROR_BAD_EXE_FORMAT);380return NULL;381}382383PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader +384pDosHeader->e_lfanew);385if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) {386SetLastError(ERROR_INVALID_EXE_SIGNATURE);387return NULL;388}389if (pNtHeader->FileHeader.SizeOfOptionalHeader == 0) {390SetLastError(ERROR_EXE_MARKED_INVALID);391return NULL;392}393SetLastError(NO_ERROR);394return ((PBYTE)pDosHeader) +395pNtHeader->OptionalHeader.AddressOfEntryPoint;396}397__except(EXCEPTION_EXECUTE_HANDLER) {398SetLastError(ERROR_EXE_MARKED_INVALID);399return NULL;400}401}402403ULONG WINAPI DetourGetModuleSize(HMODULE hModule)404{405PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;406if (hModule == NULL) {407pDosHeader = (PIMAGE_DOS_HEADER)GetModuleHandle(NULL);408}409410__try {411if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {412SetLastError(ERROR_BAD_EXE_FORMAT);413return NULL;414}415416PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader +417pDosHeader->e_lfanew);418if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) {419SetLastError(ERROR_INVALID_EXE_SIGNATURE);420return NULL;421}422if (pNtHeader->FileHeader.SizeOfOptionalHeader == 0) {423SetLastError(ERROR_EXE_MARKED_INVALID);424return NULL;425}426SetLastError(NO_ERROR);427428return (pNtHeader->OptionalHeader.SizeOfImage);429}430__except(EXCEPTION_EXECUTE_HANDLER) {431SetLastError(ERROR_EXE_MARKED_INVALID);432return NULL;433}434}435436static inline PBYTE RvaAdjust(PIMAGE_DOS_HEADER pDosHeader, DWORD raddr)437{438if (raddr != NULL) {439return ((PBYTE)pDosHeader) + raddr;440}441return NULL;442}443444BOOL WINAPI DetourEnumerateExports(HMODULE hModule,445PVOID pContext,446PF_DETOUR_ENUMERATE_EXPORT_CALLBACK pfExport)447{448PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;449if (hModule == NULL) {450pDosHeader = (PIMAGE_DOS_HEADER)GetModuleHandle(NULL);451}452453__try {454if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {455SetLastError(ERROR_BAD_EXE_FORMAT);456return NULL;457}458459PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader +460pDosHeader->e_lfanew);461if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) {462SetLastError(ERROR_INVALID_EXE_SIGNATURE);463return FALSE;464}465if (pNtHeader->FileHeader.SizeOfOptionalHeader == 0) {466SetLastError(ERROR_EXE_MARKED_INVALID);467return FALSE;468}469470PIMAGE_EXPORT_DIRECTORY pExportDir471= (PIMAGE_EXPORT_DIRECTORY)472RvaAdjust(pDosHeader,473pNtHeader->OptionalHeader474.DataDirectory[IMAGE_DIRECTORY_ENTRY_EXPORT].VirtualAddress);475476if (pExportDir == NULL) {477SetLastError(ERROR_EXE_MARKED_INVALID);478return FALSE;479}480481PDWORD pdwFunctions = (PDWORD)RvaAdjust(pDosHeader, pExportDir->AddressOfFunctions);482PDWORD pdwNames = (PDWORD)RvaAdjust(pDosHeader, pExportDir->AddressOfNames);483PWORD pwOrdinals = (PWORD)RvaAdjust(pDosHeader, pExportDir->AddressOfNameOrdinals);484485for (DWORD nFunc = 0; nFunc < pExportDir->NumberOfFunctions; nFunc++) {486PBYTE pbCode = (pdwFunctions != NULL)487? (PBYTE)RvaAdjust(pDosHeader, pdwFunctions[nFunc]) : NULL;488PCHAR pszName = NULL;489for (DWORD n = 0; n < pExportDir->NumberOfNames; n++) {490if (pwOrdinals[n] == nFunc) {491pszName = (pdwNames != NULL)492? (PCHAR)RvaAdjust(pDosHeader, pdwNames[n]) : NULL;493break;494}495}496ULONG nOrdinal = pExportDir->Base + nFunc;497498if (!pfExport(pContext, nOrdinal, pszName, pbCode)) {499break;500}501}502SetLastError(NO_ERROR);503return TRUE;504}505__except(EXCEPTION_EXECUTE_HANDLER) {506SetLastError(ERROR_EXE_MARKED_INVALID);507return NULL;508}509}510511static PDETOUR_LOADED_BINARY WINAPI GetPayloadSectionFromModule(HMODULE hModule)512{513PIMAGE_DOS_HEADER pDosHeader = (PIMAGE_DOS_HEADER)hModule;514if (hModule == NULL) {515pDosHeader = (PIMAGE_DOS_HEADER)GetModuleHandle(NULL);516}517518__try {519if (pDosHeader->e_magic != IMAGE_DOS_SIGNATURE) {520SetLastError(ERROR_BAD_EXE_FORMAT);521return NULL;522}523524PIMAGE_NT_HEADERS pNtHeader = (PIMAGE_NT_HEADERS)((PBYTE)pDosHeader +525pDosHeader->e_lfanew);526if (pNtHeader->Signature != IMAGE_NT_SIGNATURE) {527SetLastError(ERROR_INVALID_EXE_SIGNATURE);528return NULL;529}530if (pNtHeader->FileHeader.SizeOfOptionalHeader == 0) {531SetLastError(ERROR_EXE_MARKED_INVALID);532return NULL;533}534535PIMAGE_SECTION_HEADER pSectionHeaders536= (PIMAGE_SECTION_HEADER)((PBYTE)pNtHeader537+ sizeof(pNtHeader->Signature)538+ sizeof(pNtHeader->FileHeader)539+ pNtHeader->FileHeader.SizeOfOptionalHeader);540541for (DWORD n = 0; n < pNtHeader->FileHeader.NumberOfSections; n++) {542if (strcmp((PCHAR)pSectionHeaders[n].Name, ".detour") == 0) {543if (pSectionHeaders[n].VirtualAddress == 0 ||544pSectionHeaders[n].SizeOfRawData == 0) {545546break;547}548549PBYTE pbData = (PBYTE)pDosHeader + pSectionHeaders[n].VirtualAddress;550DETOUR_SECTION_HEADER *pHeader = (DETOUR_SECTION_HEADER *)pbData;551if (pHeader->cbHeaderSize < sizeof(DETOUR_SECTION_HEADER) ||552pHeader->nSignature != DETOUR_SECTION_HEADER_SIGNATURE) {553554break;555}556557if (pHeader->nDataOffset == 0) {558pHeader->nDataOffset = pHeader->cbHeaderSize;559}560SetLastError(NO_ERROR);561return (PBYTE)pHeader;562}563}564SetLastError(ERROR_EXE_MARKED_INVALID);565return NULL;566}567__except(EXCEPTION_EXECUTE_HANDLER) {568SetLastError(ERROR_EXE_MARKED_INVALID);569return NULL;570}571}572573DWORD WINAPI DetourGetSizeOfPayloads(HMODULE hModule)574{575PDETOUR_LOADED_BINARY pBinary = GetPayloadSectionFromModule(hModule);576if (pBinary == NULL) {577// Error set by GetPayloadSectonFromModule.578return 0;579}580581__try {582DETOUR_SECTION_HEADER *pHeader = (DETOUR_SECTION_HEADER *)pBinary;583if (pHeader->cbHeaderSize < sizeof(DETOUR_SECTION_HEADER) ||584pHeader->nSignature != DETOUR_SECTION_HEADER_SIGNATURE) {585586SetLastError(ERROR_INVALID_HANDLE);587return 0;588}589SetLastError(NO_ERROR);590return pHeader->cbDataSize;591}592__except(EXCEPTION_EXECUTE_HANDLER) {593SetLastError(ERROR_INVALID_HANDLE);594return 0;595}596}597598PVOID WINAPI DetourFindPayload(HMODULE hModule, REFGUID rguid, DWORD * pcbData)599{600PBYTE pbData = NULL;601if (pcbData) {602*pcbData = 0;603}604605PDETOUR_LOADED_BINARY pBinary = GetPayloadSectionFromModule(hModule);606if (pBinary == NULL) {607// Error set by GetPayloadSectonFromModule.608return NULL;609}610611__try {612DETOUR_SECTION_HEADER *pHeader = (DETOUR_SECTION_HEADER *)pBinary;613if (pHeader->cbHeaderSize < sizeof(DETOUR_SECTION_HEADER) ||614pHeader->nSignature != DETOUR_SECTION_HEADER_SIGNATURE) {615616SetLastError(ERROR_INVALID_EXE_SIGNATURE);617return NULL;618}619620PBYTE pbBeg = ((PBYTE)pHeader) + pHeader->nDataOffset;621PBYTE pbEnd = ((PBYTE)pHeader) + pHeader->cbDataSize;622623for (pbData = pbBeg; pbData < pbEnd;) {624DETOUR_SECTION_RECORD *pSection = (DETOUR_SECTION_RECORD *)pbData;625626if (pSection->guid.Data1 == rguid.Data1 &&627pSection->guid.Data2 == rguid.Data2 &&628pSection->guid.Data3 == rguid.Data3 &&629pSection->guid.Data4[0] == rguid.Data4[0] &&630pSection->guid.Data4[1] == rguid.Data4[1] &&631pSection->guid.Data4[2] == rguid.Data4[2] &&632pSection->guid.Data4[3] == rguid.Data4[3] &&633pSection->guid.Data4[4] == rguid.Data4[4] &&634pSection->guid.Data4[5] == rguid.Data4[5] &&635pSection->guid.Data4[6] == rguid.Data4[6] &&636pSection->guid.Data4[7] == rguid.Data4[7]) {637638if (pcbData) {639*pcbData = pSection->cbBytes - sizeof(*pSection);640SetLastError(NO_ERROR);641return (PBYTE)(pSection + 1);642}643}644645pbData = (PBYTE)pSection + pSection->cbBytes;646}647SetLastError(ERROR_INVALID_HANDLE);648return NULL;649}650__except(EXCEPTION_EXECUTE_HANDLER) {651SetLastError(ERROR_INVALID_HANDLE);652return NULL;653}654}655656BOOL WINAPI DetourRestoreAfterWithEx(PVOID pvData, DWORD cbData)657{658PDETOUR_EXE_RESTORE pder = (PDETOUR_EXE_RESTORE)pvData;659660if (pder->cb != sizeof(*pder) || pder->cb > cbData) {661SetLastError(ERROR_BAD_EXE_FORMAT);662return FALSE;663}664665DWORD dwPermIdh;666DWORD dwPermInh;667DWORD dwPermClr;668DWORD dwOld;669BOOL fSucceeded = FALSE;670671if (!VirtualProtect(pder->pidh, sizeof(pder->idh),672PAGE_EXECUTE_READWRITE, &dwPermIdh)) {673goto end0;674}675if (!VirtualProtect(pder->pinh, sizeof(pder->inh),676PAGE_EXECUTE_READWRITE, &dwPermInh)) {677goto end1;678}679if (pder->pclrFlags != NULL) {680if (!VirtualProtect(pder->pclrFlags, sizeof(pder->clrFlags),681PAGE_EXECUTE_READWRITE, &dwPermClr)) {682goto end2;683}684}685686CopyMemory(pder->pidh, &pder->idh, sizeof(pder->idh));687CopyMemory(pder->pinh, &pder->inh, sizeof(pder->inh));688689if (pder->pclrFlags != NULL) {690CopyMemory(pder->pclrFlags, &pder->clrFlags, sizeof(pder->clrFlags));691}692fSucceeded = TRUE;693694if (pder->pclrFlags != NULL) {695VirtualProtect(pder->pclrFlags, sizeof(pder->clrFlags), dwPermIdh, &dwOld);696}697end2:698VirtualProtect(pder->pinh, sizeof(pder->inh), dwPermInh, &dwOld);699end1:700VirtualProtect(pder->pidh, sizeof(pder->idh), dwPermIdh, &dwOld);701end0:702return fSucceeded;703}704705BOOL WINAPI DetourRestoreAfterWith()706{707for (HMODULE hMod = NULL; (hMod = DetourEnumerateModules(hMod)) != NULL;) {708PVOID pvData;709DWORD cbData;710711pvData = DetourFindPayload(hMod, DETOUR_EXE_RESTORE_GUID, &cbData);712713if (pvData == NULL || cbData == 0) {714continue;715}716717return DetourRestoreAfterWithEx(pvData, cbData);718}719SetLastError(ERROR_MOD_NOT_FOUND);720return FALSE;721}722723// End of File724725726