void AStyleFormat::LoadDynamicLibrary() // STATIC function to dynamically load the AStyle library. { // check if already loaded if (aslib.IsLoaded()) { ShowMessageDialog(wxString::Format( "AStyle library %s is already loaded!", GetDynamicLibraryName().c_str()), wxOK | wxICON_INFORMATION); return; } // load the library wxString libName = GetDynamicLibraryName(); aslib.Load(libName, wxDL_DEFAULT | wxDL_QUIET); if (!aslib.IsLoaded()) { ShowMessageDialog(wxString::Format( "Cannot load the AStyle library:\n%s.\n" "This must be corrected to format the text!", libName.c_str()), wxOK | wxICON_ERROR); return; } // get the AStyleMain address fpAStyleMain = (fpASMain) aslib.GetSymbol("AStyleMain"); if (fpAStyleMain == nullptr) { ShowMessageDialog(wxString::Format( "Cannot find the symbol AStyleMain in %s.\n" "The function must be undecorated in the library.\n" "This must be corrected to format the text!", libName.c_str()), wxOK | wxICON_ERROR); return; } // verify AStyleGetVersion function - the function address is obtained when it is called fpAStyleGetVersion = (fpASVer) aslib.GetSymbol("AStyleGetVersion"); if (fpAStyleGetVersion == nullptr) { ShowMessageDialog(wxString::Format( "Cannot find the symbol AStyleGetVersion in %s.\n" "The function must be undecorated in the library.\n" "This must be corrected to obtain the version!", libName.c_str()), wxOK | wxICON_ERROR); return; } // keep the dynamic library in memory aslibHandle = aslib.Detach(); }
char* AStyleFormat::CallAStyleMain(const char* textIn, const char* options) const { #ifdef ASTYLE_DYLIB // format using dll dynamic load if (fpAStyleMain == nullptr) // cannot use aslib.IsLoaded() after Detach() { ShowMessageDialog(wxString::Format( "Error calling the function AStyleMain.\n" "The AStyle library %s is not loaded.\n" "The text cannot be formatted.\n" "This must be corrected to format the text!", GetDynamicLibraryName().c_str()), wxOK | wxICON_ERROR); return nullptr; } char* textOut = fpAStyleMain(textIn, options, ErrorHandler, MemoryAlloc); #else // format using static library char* textOut = AStyleMain(textIn, options, ErrorHandler, MemoryAlloc); #endif // ASTYLE_DYLIB return textOut; }
bool PluginManager::load(const std::string& path) { std::string dynamic_library_name = GetDynamicLibraryName(path); std::string real_path = ResolvePathExtension(path); DynamicLibrary* dl = DynamicLibrary::load(real_path); if (NULL == dl) { fprintf(stderr, "load dynamic library failed"); return false; } _libraries[dynamic_library_name] = dl; return true; }
wxString AStyleFormat::GetAStyleVersion() const { #ifdef ASTYLE_DYLIB // get version using dll dynamic load if (fpAStyleGetVersion == nullptr) // cannot use aslib.IsLoaded() after Detach() { ShowMessageDialog(wxString::Format( "Error calling the function AStyleGetVersion.\n" "The AStyle library %s is not loaded.\n" "The version cannot be obtained!", GetDynamicLibraryName().c_str()), wxOK | wxICON_ERROR); return "????"; } const char* versionCStr = fpAStyleGetVersion(); // convert the version to a unicode string wxString version = wxString(versionCStr, wxConvUTF8); #else // get the version as a unicode string wxString version = wxString(AStyleGetVersion(), wxConvUTF8); #endif // ASTYLE_DYLIB return version; }