/** * @brief Returns a class instance from an instanced library * @param lib - [in] dynamic library instance * @param className - [in] class name * @return pointer to class instance */ DynClass* DynLoader::GetClassInstance(DynLib& lib, const dyn_string& className) { for(auto entry : lib.instances) { if(entry->name == className) return entry->instance; } dyn_string builderName("Create" + className); // POSIX guarantees that the size of a pointer to object is equal to // the size of a pointer to a function. On Windows NT systems this is also a safe // assumption. std::function<DynClass*()> builder(std::bind( reinterpret_cast<DynClass*(*)()>(GetSymbolByName(lib, builderName.c_str())))); if(builder == nullptr) throw LoaderException("Factory builder `" + builderName + "` for Class `" + className + "` not found in " + lib.name); // Create an instance of the class auto instance = builder(); if(instance == nullptr) throw LoaderException("Unable to create instance of class `" + className + "`"); auto entry = new DynClassEntry(className, instance); lib.instances.push_back(entry); return instance; }
//-------------------------------------------------------------------------------------- DWORD GetKernelSymbolOffset(char *lpszSymbolName) { DWORD Ret = 0; // get system modules information PRTL_PROCESS_MODULES Info = (PRTL_PROCESS_MODULES)GetSysInf(SystemModuleInformation); if (Info) { char *lpszKernelName = (char *)Info->Modules[0].FullPathName + Info->Modules[0].OffsetToFileName; char szKernelPath[MAX_PATH]; // get full kernel image path GetSystemDirectory(szKernelPath, MAX_PATH); lstrcat(szKernelPath, "\\"); lstrcat(szKernelPath, lpszKernelName); DbgMsg(__FILE__, __LINE__, __FUNCTION__"(): Using kernel binary '%s'\r\n", szKernelPath); // load kernel module HMODULE hModule = LoadLibraryEx(szKernelPath, NULL, DONT_RESOLVE_DLL_REFERENCES); if (hModule) { // get symbol offset LARGE_INTEGER Addr; Addr.QuadPart = GetSymbolByName(szKernelPath, hModule, lpszSymbolName); if (Addr.QuadPart > 0) { Addr.QuadPart -= (ULONGLONG)hModule; Ret = Addr.LowPart; } FreeLibrary(hModule); } else { DbgMsg(__FILE__, __LINE__, "LoadLibraryEx() ERROR %d\r\n", GetLastError()); } M_FREE(Info); } return Ret; }
/** * @brief Get class instance * @param className - [in] class name * @return pointer to class instance */ DynamicClass * DynamicLibrary::GetInstance( const PDL_CHAR * className ) { const InstanceMap::iterator loadedInstance( instances_.find( className ) ); if ( loadedInstance != instances_.end() ) { return ( loadedInstance -> second ); } const pdl_string builderName( pdl_string( "Create" ) + pdl_string( className ) ); // Tricky code to prevent compiler error // ISO C++ forbids casting between pointer-to-function and pointer-to-object DynamicBuilder builder = 0; void * symbol = GetSymbolByName( builderName.c_str() ); ( void ) memcpy( &builder, &symbol, sizeof( void * ) ); if ( !builder ) { instances_[ className ] = 0; throw LoaderException( pdl_string( "Class `" ) + pdl_string( className ) + pdl_string( "` not found in " ) + libraryName_ ); } DynamicClass * instance = ( DynamicClass * )( *builder )(); instances_[ className ] = instance; return instance; }