cmDynamicLoaderC.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. /*=========================================================================
  2. Program: Insight Segmentation & Registration Toolkit
  3. Module: $RCSfile$
  4. Language: C++
  5. Date: $Date$
  6. Version: $Revision$
  7. Copyright (c) 2002 Insight Consortium. All rights reserved.
  8. See ITKCopyright.txt or http://www.itk.org/HTML/Copyright.htm for details.
  9. This software is distributed WITHOUT ANY WARRANTY; without even
  10. the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
  11. PURPOSE. See the above copyright notices for more information.
  12. =========================================================================*/
  13. // This file is actually 4 different implementations.
  14. // 1. HP machines which uses shl_load
  15. // 2. Apple OSX which uses NSLinkModule
  16. // 3. Windows which uses LoadLibrary
  17. // 4. Most unix systems which use dlopen (default )
  18. // Each part of the ifdef contains a complete implementation for
  19. // the static methods of cmDynamicLoader.
  20. // Ugly stuff for library handles
  21. // They are different on several different OS's
  22. #if defined(__hpux)
  23. # include <dl.h>
  24. typedef shl_t cmLibHandle;
  25. #elif defined(_WIN32)
  26. #include <windows.h>
  27. typedef HMODULE cmLibHandle;
  28. #else
  29. typedef void* cmLibHandle;
  30. #endif
  31. typedef void (*cmDynamicLoaderFunction)();
  32. // ---------------------------------------------------------------
  33. // 1. Implementation for HPUX machines
  34. #ifdef __hpux
  35. #define CMDYNAMICLOADER_DEFINED 1
  36. #include <dl.h>
  37. cmDynamicLoaderFunction cmDynamicLoaderGetSymbolAddress(cmLibHandle lib,
  38. const char* sym)
  39. {
  40. void* addr;
  41. int status;
  42. status = shl_findsym (&lib, sym, TYPE_PROCEDURE, &addr);
  43. return (cmDynamicLoaderFunction)((status < 0) ? (void*)0 : addr);
  44. }
  45. #endif
  46. // ---------------------------------------------------------------
  47. // 2. Implementation for Darwin (including OSX) Machines
  48. #ifdef __APPLE__
  49. #define CMDYNAMICLOADER_DEFINED
  50. #include <mach-o/dyld.h>
  51. cmDynamicLoaderFunction cmDynamicLoaderGetSymbolAddress(cmLibHandle lib,
  52. const char* sym)
  53. {
  54. void *result=0;
  55. if(NSIsSymbolNameDefined(sym))
  56. {
  57. NSSymbol symbol= NSLookupAndBindSymbol(sym);
  58. if(symbol)
  59. {
  60. result = NSAddressOfSymbol(symbol);
  61. }
  62. }
  63. return (cmDynamicLoaderFunction)result;
  64. }
  65. #endif
  66. // ---------------------------------------------------------------
  67. // 3. Implementation for Windows win32 code
  68. #ifdef _WIN32
  69. #include <windows.h>
  70. #define CMDYNAMICLOADER_DEFINED 1
  71. cmDynamicLoaderFunction cmDynamicLoaderGetSymbolAddress(cmLibHandle lib,
  72. const char* sym)
  73. {
  74. #ifdef UNICODE
  75. wchar_t *wsym = new wchar_t [mbstowcs(NULL, sym, 32000)];
  76. mbstowcs(wsym, sym, 32000);
  77. void *ret = GetProcAddress(lib, wsym);
  78. delete [] wsym;
  79. return (cmDynamicLoaderFunction)ret;
  80. #else
  81. return (cmDynamicLoaderFunction)GetProcAddress(lib, sym);
  82. #endif
  83. }
  84. #endif
  85. // ---------------------------------------------------------------
  86. // 4. Implementation for default UNIX machines.
  87. // if nothing has been defined then use this
  88. #ifndef CMDYNAMICLOADER_DEFINED
  89. #define CMDYNAMICLOADER_DEFINED
  90. // Setup for most unix machines
  91. #include <dlfcn.h>
  92. cmDynamicLoaderFunction cmDynamicLoaderGetSymbolAddress(cmLibHandle lib,
  93. const char* sym)
  94. {
  95. return (cmDynamicLoaderFunction)dlsym(lib, sym);
  96. }
  97. #endif