DynamicLibrary DynamicLibrary::getRunningProcess() { os::clearError(__LINE__, __FILE__); DynamicLibrary runningProcess = DynamicLibrary(); #ifdef MACE_WINAPI runningProcess.dll = GetModuleHandle(nullptr); MACE_CONSTEXPR char* errorMessage = "Handle to running process was nullptr"; #elif defined(MACE_POSIX) //the NULL macro is used instead of nullptr because dlopen accepts an argument of 0 specifically //it needs to be an integer, not a pointer value. runningProcess.dll = dlopen(NULL, RTLD_LOCAL | RTLD_LAZY); char* systemError = dlerror(); const char* errorMessage; if (systemError == nullptr) { errorMessage = "Handle to running process was nullptr"; } else { errorMessage = systemError; } #endif if (runningProcess.dll == nullptr) { MACE__THROW(NullPointer, "Internal Error: " + std::string(errorMessage)); } runningProcess.created = true; os::checkError(__LINE__, __FILE__, "Error retrieving current running process as dynamic library: " + std::string(errorMessage)); return runningProcess; }
static QString getLibVersion( const QString &libraries ) { QStringList libs = libraries.split( "|" ); QString resultFound, resultNotFound; Q_FOREACH( const QString &lib, libs ) { try { QString ver = QString::fromStdString( DynamicLibrary( lib.toLatin1() ).getVersionStr() ); resultFound += QString( "%1 (%2)" ) .arg( lib ) .arg( ver == "missing" ? DiagnosticsDialog::tr("failed to get version info") : ver ); } catch( const std::runtime_error &e ) { QString tmp = QString("%1 - %2") .arg( lib ) .arg( QString( e.what() ).contains( "missing" ) ? DiagnosticsDialog::tr("failed to get version info") : DiagnosticsDialog::tr("library not found") ); if ( !resultNotFound.isEmpty() ) resultNotFound += "<br />"; resultNotFound += tmp; } } return !resultFound.isEmpty() ? resultFound : resultNotFound; }
QString DiagnosticsDialog::getLibVersion( const QString &lib ) const { try { QString ver = QString::fromStdString( DynamicLibrary( lib.toLatin1() ).getVersionStr() ); return QString( "%1 (%2)" ) .arg( lib ) .arg( ver == "missing" ? tr( "failed to get version info" ) : ver ); } catch( const std::runtime_error &e ) { return QString("%1 - %2") .arg( lib ) .arg( QString( e.what() ).contains( "missing" ) ? tr( "failed to get version info" ) : tr( "library not found" ) ); } }
PEGASUS_NAMESPACE_BEGIN CMPIProviderModule::CMPIProviderModule(const String & fileName) { PEG_METHOD_ENTER( TRC_CMPIPROVIDERINTERFACE, "CMPIProviderModule::CMPIProviderModule()"); String resolvedFileName; #ifdef PEGASUS_OS_TYPE_WINDOWS if (fileName[1] != ':') #else if (fileName[0]!='/') #endif resolvedFileName=ProviderManager::_resolvePhysicalName(fileName); else resolvedFileName=fileName; _library = DynamicLibrary(resolvedFileName); PEG_METHOD_EXIT(); }
// Test reference counting void Test4() { DynamicLibrary library(getLibraryFileName(VALID_LIBRARY_NAME)); PEGASUS_TEST_ASSERT(!library.isLoaded()); // Load the library Boolean loaded = library.load(); PEGASUS_TEST_ASSERT(loaded); PEGASUS_TEST_ASSERT(library.isLoaded()); // Load the library again loaded = library.load(); PEGASUS_TEST_ASSERT(loaded); PEGASUS_TEST_ASSERT(library.isLoaded()); // Unload the library library.unload(); PEGASUS_TEST_ASSERT(library.isLoaded()); // Unload the library again library.unload(); PEGASUS_TEST_ASSERT(!library.isLoaded()); // Load the library and then assign to a new instance library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); library = DynamicLibrary(getLibraryFileName(VALID_LIBRARY_NAME)); PEGASUS_TEST_ASSERT(!library.isLoaded()); library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); library.load(); PEGASUS_TEST_ASSERT(library.isLoaded()); // Call the destructor while the library is loaded }
// The caller assumes the repsonsibility of making sure // the libraryPath is correctly formatted CIMIndicationConsumerProvider* ConsumerModule::load( const String & consumerName, const String & libraryPath) { PEG_METHOD_ENTER(TRC_LISTENER, "ConsumerModule::load"); // check whether the module is cached; // if it's not already in memory, load it if (!_library.isLoaded()) { if (!FileSystem::exists(libraryPath) || !FileSystem::canRead(libraryPath)) { throw Exception( MessageLoaderParms( "DynListener.ConsumerModule.INVALID_LIBRARY_PATH", "The library ($0:$1) does not exist or cannot be read.", libraryPath, consumerName)); } _library = DynamicLibrary(libraryPath); } PEG_TRACE((TRC_LISTENER,Tracer::LEVEL4, "Loading library(consumer module): %s", (const char*)consumerName.getCString())); if (!_library.load()) { throw Exception( MessageLoaderParms( "DynListener.ConsumerModule.CANNOT_LOAD_LIBRARY", "Cannot load consumer library ($0:$1), load error $2", _library.getFileName(), consumerName, _library.getLoadErrorMessage())); } PEG_TRACE((TRC_LISTENER,Tracer::LEVEL3, "Successfully loaded library(consumer module) %s", (const char*)consumerName.getCString())); // locate the entry point CIMProvider* (*createProvider)(const String&) = (CIMProvider* (*)(const String&)) _library.getSymbol("PegasusCreateProvider"); if (!createProvider) { _library.unload(); throw Exception( MessageLoaderParms( "DynListener.ConsumerModule.ENTRY_POINT_NOT_FOUND", "The entry point for consumer library ($0:$1) cannot be found.", libraryPath, consumerName)); } // create the consumer provider CIMProvider* providerRef = createProvider(consumerName); if(!providerRef) { _library.unload(); throw Exception( MessageLoaderParms( "DynListener.ConsumerModule.CREATE_PROVIDER_FAILED", "createProvider failed for consumer library ($0:$1)", libraryPath, consumerName)); } // test for the appropriate interface CIMIndicationConsumerProvider* consumerRef = dynamic_cast<CIMIndicationConsumerProvider *>(providerRef); if(!consumerRef) { _library.unload(); throw Exception( MessageLoaderParms( "DynListener.ConsumerModule.CONSUMER_IS_NOT_A", "Consumer ($0:$1) is not a CIMIndicationConsumerProvider.", libraryPath, consumerName)); } PEG_METHOD_EXIT(); return consumerRef; }