MCPcopy Create free account
hub / github.com/acl-dev/acl / DetourFindFunction

Function DetourFindFunction

lib_fiber/cpp/src/detours/modules.cpp:143–245  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

141}
142
143PVOID WINAPI DetourFindFunction(_In_ LPCSTR pszModule,
144 _In_ LPCSTR pszFunction)
145{
146 if (pszFunction == NULL) {
147 SetLastError(ERROR_INVALID_PARAMETER);
148 return NULL;
149 }
150
151 /////////////////////////////////////////////// First, try GetProcAddress.
152 //
153#pragma prefast(suppress:28752, "We don't do the unicode conversion for LoadLibraryExA.")
154 HMODULE hModule = LoadLibraryExA(pszModule, NULL, 0);
155 if (hModule == NULL) {
156 return NULL;
157 }
158
159 PBYTE pbCode = (PBYTE)GetProcAddress(hModule, pszFunction);
160 if (pbCode) {
161 return pbCode;
162 }
163
164 ////////////////////////////////////////////////////// Then try ImageHelp.
165 //
166 DETOUR_TRACE(("DetourFindFunction(%hs, %hs)\n", pszModule, pszFunction));
167 PDETOUR_SYM_INFO pSymInfo = DetourLoadImageHlp();
168 if (pSymInfo == NULL) {
169 DETOUR_TRACE(("DetourLoadImageHlp failed: %lu\n",
170 GetLastError()));
171 return NULL;
172 }
173
174 if (pSymInfo->pfSymLoadModule64(pSymInfo->hProcess, NULL,
175 (PCHAR)pszModule, NULL,
176 (DWORD64)hModule, 0) == 0) {
177 if (ERROR_SUCCESS != GetLastError()) {
178 DETOUR_TRACE(("SymLoadModule64(%p) failed: %lu\n",
179 pSymInfo->hProcess, GetLastError()));
180 return NULL;
181 }
182 }
183
184 HRESULT hrRet;
185 CHAR szFullName[512];
186 IMAGEHLP_MODULE64 modinfo;
187 ZeroMemory(&modinfo, sizeof(modinfo));
188 modinfo.SizeOfStruct = sizeof(modinfo);
189 if (!pSymInfo->pfSymGetModuleInfo64(pSymInfo->hProcess, (DWORD64)hModule, &modinfo)) {
190 DETOUR_TRACE(("SymGetModuleInfo64(%p, %p) failed: %lu\n",
191 pSymInfo->hProcess, hModule, GetLastError()));
192 return NULL;
193 }
194
195 hrRet = StringCchCopyA(szFullName, sizeof(szFullName)/sizeof(CHAR), modinfo.ModuleName);
196 if (FAILED(hrRet)) {
197 DETOUR_TRACE(("StringCchCopyA failed: %08lx\n", hrRet));
198 return NULL;
199 }
200 hrRet = StringCchCatA(szFullName, sizeof(szFullName)/sizeof(CHAR), "!");

Callers

nothing calls this directly

Calls 3

StringCchCopyAFunction · 0.85
StringCchCatAFunction · 0.85
DetourGetEntryPointFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…