SDL_loadso.h 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2025 Sam Lantinga <slouken@libsdl.org>
  4. This software is provided 'as-is', without any express or implied
  5. warranty. In no event will the authors be held liable for any damages
  6. arising from the use of this software.
  7. Permission is granted to anyone to use this software for any purpose,
  8. including commercial applications, and to alter it and redistribute it
  9. freely, subject to the following restrictions:
  10. 1. The origin of this software must not be misrepresented; you must not
  11. claim that you wrote the original software. If you use this software
  12. in a product, an acknowledgment in the product documentation would be
  13. appreciated but is not required.
  14. 2. Altered source versions must be plainly marked as such, and must not be
  15. misrepresented as being the original software.
  16. 3. This notice may not be removed or altered from any source distribution.
  17. */
  18. /* WIKI CATEGORY: SharedObject */
  19. /**
  20. * # CategorySharedObject
  21. *
  22. * System-dependent library loading routines.
  23. *
  24. * Shared objects are code that is programmatically loadable at runtime.
  25. * Windows calls these "DLLs", Linux calls them "shared libraries", etc.
  26. *
  27. * To use them, build such a library, then call SDL_LoadObject() on it. Once
  28. * loaded, you can use SDL_LoadFunction() on that object to find the address
  29. * of its exported symbols. When done with the object, call SDL_UnloadObject()
  30. * to dispose of it.
  31. *
  32. * Some things to keep in mind:
  33. *
  34. * - These functions only work on C function names. Other languages may have
  35. * name mangling and intrinsic language support that varies from compiler to
  36. * compiler.
  37. * - Make sure you declare your function pointers with the same calling
  38. * convention as the actual library function. Your code will crash
  39. * mysteriously if you do not do this.
  40. * - Avoid namespace collisions. If you load a symbol from the library, it is
  41. * not defined whether or not it goes into the global symbol namespace for
  42. * the application. If it does and it conflicts with symbols in your code or
  43. * other shared libraries, you will not get the results you expect. :)
  44. * - Once a library is unloaded, all pointers into it obtained through
  45. * SDL_LoadFunction() become invalid, even if the library is later reloaded.
  46. * Don't unload a library if you plan to use these pointers in the future.
  47. * Notably: beware of giving one of these pointers to atexit(), since it may
  48. * call that pointer after the library unloads.
  49. */
  50. #ifndef SDL_loadso_h_
  51. #define SDL_loadso_h_
  52. #include <SDL3/SDL_stdinc.h>
  53. #include <SDL3/SDL_error.h>
  54. #include <SDL3/SDL_begin_code.h>
  55. /* Set up for C function definitions, even when using C++ */
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /**
  60. * An opaque datatype that represents a loaded shared object.
  61. *
  62. * \since This datatype is available since SDL 3.1.3.
  63. *
  64. * \sa SDL_LoadObject
  65. * \sa SDL_LoadFunction
  66. * \sa SDL_UnloadObject
  67. */
  68. typedef struct SDL_SharedObject SDL_SharedObject;
  69. /**
  70. * Dynamically load a shared object.
  71. *
  72. * \param sofile a system-dependent name of the object file.
  73. * \returns an opaque pointer to the object handle or NULL on failure; call
  74. * SDL_GetError() for more information.
  75. *
  76. * \threadsafety It is safe to call this function from any thread.
  77. *
  78. * \since This function is available since SDL 3.1.3.
  79. *
  80. * \sa SDL_LoadFunction
  81. * \sa SDL_UnloadObject
  82. */
  83. extern SDL_DECLSPEC SDL_SharedObject * SDLCALL SDL_LoadObject(const char *sofile);
  84. /**
  85. * Look up the address of the named function in a shared object.
  86. *
  87. * This function pointer is no longer valid after calling SDL_UnloadObject().
  88. *
  89. * This function can only look up C function names. Other languages may have
  90. * name mangling and intrinsic language support that varies from compiler to
  91. * compiler.
  92. *
  93. * Make sure you declare your function pointers with the same calling
  94. * convention as the actual library function. Your code will crash
  95. * mysteriously if you do not do this.
  96. *
  97. * If the requested function doesn't exist, NULL is returned.
  98. *
  99. * \param handle a valid shared object handle returned by SDL_LoadObject().
  100. * \param name the name of the function to look up.
  101. * \returns a pointer to the function or NULL on failure; call SDL_GetError()
  102. * for more information.
  103. *
  104. * \threadsafety It is safe to call this function from any thread.
  105. *
  106. * \since This function is available since SDL 3.1.3.
  107. *
  108. * \sa SDL_LoadObject
  109. */
  110. extern SDL_DECLSPEC SDL_FunctionPointer SDLCALL SDL_LoadFunction(SDL_SharedObject *handle, const char *name);
  111. /**
  112. * Unload a shared object from memory.
  113. *
  114. * Note that any pointers from this object looked up through
  115. * SDL_LoadFunction() will no longer be valid.
  116. *
  117. * \param handle a valid shared object handle returned by SDL_LoadObject().
  118. *
  119. * \threadsafety It is safe to call this function from any thread.
  120. *
  121. * \since This function is available since SDL 3.1.3.
  122. *
  123. * \sa SDL_LoadObject
  124. */
  125. extern SDL_DECLSPEC void SDLCALL SDL_UnloadObject(SDL_SharedObject *handle);
  126. /* Ends C function definitions when using C++ */
  127. #ifdef __cplusplus
  128. }
  129. #endif
  130. #include <SDL3/SDL_close_code.h>
  131. #endif /* SDL_loadso_h_ */