char* tracker_getdllname(uintptr_t caller) { DllTrackInfo *track = tracker_get_dlltrackinfo(caller); if(track) return track->pDll->GetFileName(); return (char*)""; }
extern "C" inline void tracker_library_track(uintptr_t caller, HMODULE hHandle) { DllTrackInfo* pInfo = tracker_get_dlltrackinfo(caller); if (pInfo && hHandle) { pInfo->dllList.push_back(hHandle); } }
extern "C" void tracker_socket_free(uintptr_t caller, SOCKET socket) { DllTrackInfo* pInfo = tracker_get_dlltrackinfo(caller); if (pInfo) { pInfo->socketList.remove(socket); } }
extern "C" void tracker_socket_track(uintptr_t caller, SOCKET socket) { DllTrackInfo* pInfo = tracker_get_dlltrackinfo(caller); if (pInfo) { pInfo->socketList.push_back(socket); } }
extern "C" HMODULE __stdcall track_LoadLibraryA(LPCSTR file) { uintptr_t loc = (uintptr_t)_ReturnAddress(); DllTrackInfo* pInfo = tracker_get_dlltrackinfo(loc); char* path = NULL; if (pInfo) path = pInfo->pDll->GetFileName(); HMODULE hHandle = dllLoadLibraryExtended(file, path); tracker_library_track(loc, hHandle); return hHandle; }
extern "C" void tracker_file_track(uintptr_t caller, uintptr_t handle, TrackedFileType type, const char* sFile) { DllTrackInfo* pInfo = tracker_get_dlltrackinfo(caller); if (pInfo) { CSingleLock lock(g_trackerLock); TrackedFile* file = new TrackedFile; file->handle = handle; file->type = type; file->name = strdup(sFile); pInfo->fileList.push_back(file); } }
extern "C" HMODULE __stdcall track_LoadLibraryExA(const char* lpLibFileName, HANDLE hFile, DWORD dwFlags) { uintptr_t loc = (uintptr_t)_ReturnAddress(); DllTrackInfo* pInfo = tracker_get_dlltrackinfo(loc); const char* path = NULL; if (pInfo) path = pInfo->pDll->GetFileName(); HMODULE hHandle = dllLoadLibraryExExtended(lpLibFileName, hFile, dwFlags, path); tracker_library_track(loc, hHandle); return hHandle; }
extern "C" inline void tracker_library_free(uintptr_t caller, HMODULE hHandle) { DllTrackInfo* pInfo = tracker_get_dlltrackinfo(caller); if (pInfo && hHandle) { for (DllListIter it = pInfo->dllList.begin(); it != pInfo->dllList.end(); ++it) { if (*it == hHandle) { pInfo->dllList.erase(it); break; } } } }
extern "C" void tracker_file_free(uintptr_t caller, uintptr_t handle, TrackedFileType type) { DllTrackInfo* pInfo = tracker_get_dlltrackinfo(caller); if (pInfo) { CSingleLock lock(g_trackerLock); for (FileListIter it = pInfo->fileList.begin(); it != pInfo->fileList.end(); ++it) { TrackedFile* file = *it; if (file->handle == handle && file->type == type) { free(file->name); delete file; pInfo->fileList.erase(it); return; } } } CLog::Log(LOGWARNING, "unable to remove tracked file from tracker"); }
extern "C" FARPROC __stdcall dllGetProcAddress(HMODULE hModule, LPCSTR function) { uintptr_t loc = (uintptr_t)_ReturnAddress(); void* address = NULL; LibraryLoader* dll = DllLoaderContainer::GetModule(hModule); if( !dll ) { CLog::Log(LOGERROR, "%s - Invalid hModule specified",__FUNCTION__); return NULL; } /* how can somebody get the stupid idea to create such a stupid function */ /* where you never know if the given pointer is a pointer or a value */ if( HIGH_WORD(function) == 0 && LOW_WORD(function) < 1000) { if( dll->ResolveOrdinal(LOW_WORD(function), &address) ) { CLog::Log(LOGDEBUG, "%s(%p(%s), %d) => %p", __FUNCTION__, hModule, dll->GetName(), LOW_WORD(function), address); } else if( dll->IsSystemDll() ) { char ordinal[5]; sprintf(ordinal, "%d", LOW_WORD(function)); address = (void*)create_dummy_function(dll->GetName(), ordinal); /* add to tracklist if we are tracking this source dll */ DllTrackInfo* track = tracker_get_dlltrackinfo(loc); if( track ) tracker_dll_data_track(track->pDll, (uintptr_t)address); CLog::Log(LOGDEBUG, "%s - created dummy function %s!%s",__FUNCTION__, dll->GetName(), ordinal); } else { address = NULL; CLog::Log(LOGDEBUG, "%s(%p(%s), '%s') => %p",__FUNCTION__ , hModule, dll->GetName(), function, address); } } else { if( dll->ResolveExport(function, &address) ) { CLog::Log(LOGDEBUG, "%s(%p(%s), '%s') => %p",__FUNCTION__ , hModule, dll->GetName(), function, address); } else { DllTrackInfo* track = tracker_get_dlltrackinfo(loc); /* some dll's require us to always return a function or it will fail, other's */ /* decide functionallity depending on if the functions exist and may fail */ if( dll->IsSystemDll() && track && stricmp(track->pDll->GetName(), "CoreAVCDecoder.ax") == 0 ) { address = (void*)create_dummy_function(dll->GetName(), function); tracker_dll_data_track(track->pDll, (uintptr_t)address); CLog::Log(LOGDEBUG, "%s - created dummy function %s!%s", __FUNCTION__, dll->GetName(), function); } else { address = NULL; CLog::Log(LOGDEBUG, "%s(%p(%s), '%s') => %p", __FUNCTION__, hModule, dll->GetName(), function, address); } } } return (FARPROC)address; }