juce_DynamicLibrary.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. ==============================================================================
  3. This file is part of the juce_core module of the JUCE library.
  4. Copyright (c) 2013 - Raw Material Software Ltd.
  5. Permission to use, copy, modify, and/or distribute this software for any purpose with
  6. or without fee is hereby granted, provided that the above copyright notice and this
  7. permission notice appear in all copies.
  8. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD
  9. TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN
  10. NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL
  11. DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER
  12. IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  14. ------------------------------------------------------------------------------
  15. NOTE! This permissive ISC license applies ONLY to files within the juce_core module!
  16. All other JUCE modules are covered by a dual GPL/commercial license, so if you are
  17. using any other modules, be sure to check that you also comply with their license.
  18. For more details, visit www.juce.com
  19. ==============================================================================
  20. */
  21. #ifndef JUCE_DYNAMICLIBRARY_H_INCLUDED
  22. #define JUCE_DYNAMICLIBRARY_H_INCLUDED
  23. /**
  24. Handles the opening and closing of DLLs.
  25. This class can be used to open a DLL and get some function pointers from it.
  26. Since the DLL is freed when this object is deleted, it's handy for managing
  27. library lifetimes using RAII.
  28. */
  29. class JUCE_API DynamicLibrary
  30. {
  31. public:
  32. /** Creates an unopened DynamicLibrary object.
  33. Call open() to actually open one.
  34. */
  35. DynamicLibrary() noexcept : handle (nullptr) {}
  36. /**
  37. */
  38. DynamicLibrary (const String& name) : handle (nullptr) { open (name); }
  39. /** Destructor.
  40. If a library is currently open, it will be closed when this object is destroyed.
  41. */
  42. ~DynamicLibrary() { close(); }
  43. /** Opens a DLL.
  44. The name and the method by which it gets found is of course platform-specific, and
  45. may or may not include a path, depending on the OS.
  46. If a library is already open when this method is called, it will first close the library
  47. before attempting to load the new one.
  48. @returns true if the library was successfully found and opened.
  49. */
  50. bool open (const String& name);
  51. /** Releases the currently-open DLL, or has no effect if none was open. */
  52. void close();
  53. /** Tries to find a named function in the currently-open DLL, and returns a pointer to it.
  54. If no library is open, or if the function isn't found, this will return a null pointer.
  55. */
  56. void* getFunction (const String& functionName) noexcept;
  57. /** Returns the platform-specific native library handle.
  58. You'll need to cast this to whatever is appropriate for the OS that's in use.
  59. */
  60. void* getNativeHandle() const noexcept { return handle; }
  61. private:
  62. void* handle;
  63. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (DynamicLibrary)
  64. };
  65. #endif // JUCE_DYNAMICLIBRARY_H_INCLUDED