Medical Imaging Interaction Toolkit  2016.11.0
Medical Imaging Interaction Toolkit
usModuleUtils.cpp
Go to the documentation of this file.
1 /*=============================================================================
2 
3  Library: CppMicroServices
4 
5  Copyright (c) German Cancer Research Center,
6  Division of Medical and Biological Informatics
7 
8  Licensed under the Apache License, Version 2.0 (the "License");
9  you may not use this file except in compliance with the License.
10  You may obtain a copy of the License at
11 
12  http://www.apache.org/licenses/LICENSE-2.0
13 
14  Unless required by applicable law or agreed to in writing, software
15  distributed under the License is distributed on an "AS IS" BASIS,
16  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  See the License for the specific language governing permissions and
18  limitations under the License.
19 
20 =============================================================================*/
21 
22 
23 #include "usModuleUtils_p.h"
24 
25 #include <usLog_p.h>
26 #include <usModuleInfo.h>
27 #include <usUtils_p.h>
28 
29 US_BEGIN_NAMESPACE
30 
31 namespace {
32 #ifdef US_BUILD_SHARED_LIBS
33  const bool sharedLibMode = true;
34 #else
35  const bool sharedLibMode = false;
36 #endif
37 }
38 
39 #ifdef __GNUC__
40 
41 #ifndef _GNU_SOURCE
42 #define _GNU_SOURCE
43 #endif
44 
45 #include <dlfcn.h>
46 
47 std::string GetLibraryPath_impl(void* symbol)
48 {
49  Dl_info info;
50  if (dladdr(symbol, &info))
51  {
52  return info.dli_fname;
53  }
54  else
55  {
56  US_DEBUG << "GetLibraryPath_impl() failed for address " << symbol;
57  }
58  return "";
59 }
60 
61 void* GetSymbol_impl(const ModuleInfo& moduleInfo, const char* symbol)
62 {
63  // Clear the last error message
64  dlerror();
65 
66  void* selfHandle = 0;
67  if (!sharedLibMode || moduleInfo.name == "main")
68  {
69  // Get the handle of the executable
70  selfHandle = dlopen(0, RTLD_LAZY);
71  }
72  else
73  {
74  selfHandle = dlopen(moduleInfo.location.c_str(), RTLD_LAZY);
75  }
76 
77  if (selfHandle)
78  {
79  void* addr = dlsym(selfHandle, symbol);
80  if (!addr)
81  {
82  const char* dlerrorMsg = dlerror();
83  if (dlerrorMsg)
84  {
85  US_DEBUG << "GetSymbol_impl() failed: " << dlerrorMsg;
86  }
87  }
88  dlclose(selfHandle);
89  return addr;
90  }
91  else
92  {
93  US_DEBUG << "GetSymbol_impl() dlopen() failed: " << dlerror();
94  }
95  return 0;
96 }
97 
98 #elif _WIN32
99 
100 #include <windows.h>
101 
102 std::string GetLibraryPath_impl(void *symbol)
103 {
104  HMODULE handle = 0;
105  BOOL handleError = GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS | GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
106  static_cast<LPCTSTR>(symbol), &handle);
107  if (!handleError)
108  {
109  // Test
110  US_DEBUG << "GetLibraryPath_impl():GetModuleHandle() " << GetLastErrorStr();
111  return "";
112  }
113 
114  char modulePath[512];
115  if (GetModuleFileName(handle, modulePath, 512))
116  {
117  return modulePath;
118  }
119 
120  US_DEBUG << "GetLibraryPath_impl():GetModuleFileName() " << GetLastErrorStr();
121  return "";
122 }
123 
124 void* GetSymbol_impl(const ModuleInfo& moduleInfo, const char* symbol)
125 {
126  HMODULE handle = NULL;
127  if (!sharedLibMode || moduleInfo.name == "main")
128  {
129  handle = GetModuleHandle(NULL);
130  }
131  else
132  {
133  handle = GetModuleHandle(moduleInfo.location.c_str());
134  }
135 
136  if (!handle)
137  {
138  US_DEBUG << "GetSymbol_impl():GetModuleHandle() " << GetLastErrorStr();
139  return 0;
140  }
141 
142  void* addr = (void*)GetProcAddress(handle, symbol);
143  if (!addr)
144  {
145  US_DEBUG << "GetSymbol_impl():GetProcAddress(handle," << symbol << ") " << GetLastErrorStr();
146  }
147  return addr;
148 }
149 #else
150 std::string GetLibraryPath_impl(void*)
151 {
152  return "";
153 }
154 
155 void* GetSymbol_impl(const ModuleInfo&, const char* symbol)
156 {
157  return 0;
158 }
159 #endif
160 
161 std::string ModuleUtils::GetLibraryPath(void* symbol)
162 {
163  return GetLibraryPath_impl(symbol);
164 }
165 
166 void* ModuleUtils::GetSymbol(const ModuleInfo& module, const char* symbol)
167 {
168  return GetSymbol_impl(module, symbol);
169 }
170 
171 US_END_NAMESPACE
static void info(const char *fmt,...)
Definition: svm.cpp:100
static std::string GetLastErrorStr()
Definition: mitkIOUtil.cpp:45
std::string GetLibraryPath_impl(void *)
void * GetSymbol_impl(const ModuleInfo &, const char *symbol)