SDL_sysloadso.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. /*
  2. Simple DirectMedia Layer
  3. Copyright (C) 1997-2022 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. #include "../../SDL_internal.h"
  19. #ifdef SDL_LOADSO_OS2
  20. /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
  21. /* System dependent library loading routines */
  22. #include "SDL_loadso.h"
  23. #include "../../core/os2/SDL_os2.h"
  24. #define INCL_DOSMODULEMGR
  25. #define INCL_DOSERRORS
  26. #include <os2.h>
  27. void *
  28. SDL_LoadObject(const char *sofile)
  29. {
  30. ULONG ulRC;
  31. HMODULE hModule;
  32. CHAR acError[256];
  33. PSZ pszModName;
  34. if (!sofile) {
  35. SDL_InvalidParamError("sofile");
  36. return NULL;
  37. }
  38. pszModName = OS2_UTF8ToSys(sofile);
  39. ulRC = DosLoadModule(acError, sizeof(acError), pszModName, &hModule);
  40. if (ulRC != NO_ERROR && !SDL_strrchr(pszModName, '\\') && !SDL_strrchr(pszModName, '/')) {
  41. /* strip .dll extension and retry only if name has no path. */
  42. size_t len = SDL_strlen(pszModName);
  43. if (len > 4 && SDL_strcasecmp(&pszModName[len - 4], ".dll") == 0) {
  44. pszModName[len - 4] = '\0';
  45. ulRC = DosLoadModule(acError, sizeof(acError), pszModName, &hModule);
  46. }
  47. }
  48. if (ulRC != NO_ERROR) {
  49. SDL_SetError("Failed loading %s: %s (E%u)", sofile, acError, ulRC);
  50. hModule = NULLHANDLE;
  51. }
  52. SDL_free(pszModName);
  53. return (void *)hModule;
  54. }
  55. void *
  56. SDL_LoadFunction(void *handle, const char *name)
  57. {
  58. ULONG ulRC;
  59. PFN pFN;
  60. ulRC = DosQueryProcAddr((HMODULE)handle, 0, name, &pFN);
  61. if (ulRC != NO_ERROR) {
  62. /* retry with an underscore prepended, e.g. for gcc-built dlls. */
  63. SDL_bool isstack;
  64. size_t len = SDL_strlen(name) + 1;
  65. char *_name = SDL_small_alloc(char, len + 1, &isstack);
  66. _name[0] = '_';
  67. SDL_memcpy(&_name[1], name, len);
  68. ulRC = DosQueryProcAddr((HMODULE)handle, 0, _name, &pFN);
  69. SDL_small_free(_name, isstack);
  70. }
  71. if (ulRC != NO_ERROR) {
  72. SDL_SetError("Failed loading procedure %s (E%u)", name, ulRC);
  73. return NULL;
  74. }
  75. return (void *)pFN;
  76. }
  77. void
  78. SDL_UnloadObject(void *handle)
  79. {
  80. if (handle != NULL) {
  81. DosFreeModule((HMODULE)handle);
  82. }
  83. }
  84. #endif /* SDL_LOADSO_OS2 */
  85. /* vi: set ts=4 sw=4 expandtab: */