win_shared_object_loader.h
Go to the documentation of this file.
1 // Copyright (C) 2018-2020 Intel Corporation
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 
5 /**
6  * @brief WINAPI compatible loader for a shared object
7  *
8  * @file win_shared_object_loader.h
9  */
10 #pragma once
11 
12 #include "ie_api.h"
13 #include "details/ie_exception.hpp"
15 
16 // Avoidance of Windows.h to include winsock library.
17 #ifndef _WINSOCKAPI_
18 # define _WINSOCKAPI_
19 #endif
20 
21 // Avoidance of Windows.h to define min/max.
22 #ifndef NOMINMAX
23 # define NOMINMAX
24 #endif
25 
26 #include <direct.h>
27 #include <windows.h>
28 
29 namespace InferenceEngine {
30 namespace details {
31 
32 /**
33  * @brief This class provides an OS shared module abstraction
34  */
35 class SharedObjectLoader {
36 private:
37  HMODULE shared_object;
38 
39  void ExcludeCurrentDirectory() {
40  // Exclude current directory from DLL search path process wise.
41  // If application specific path was configured before then
42  // current directory is alread excluded.
43  // GetDLLDirectory does not distinguish if aplication specific
44  // path was set to "" or NULL so reset it to "" to keep
45  // aplication safe.
46  if (GetDllDirectory(0, NULL) <= 1) {
47  SetDllDirectory(TEXT(""));
48  }
49  }
50 
51 public:
52 #ifdef ENABLE_UNICODE_PATH_SUPPORT
53  /**
54  * @brief Loads a library with the name specified. The library is loaded according to the
55  * WinAPI LoadLibrary rules
56  * @param pluginName Full or relative path to the plugin library
57  */
58  explicit SharedObjectLoader(LPCWSTR pluginName) {
59  ExcludeCurrentDirectory();
60 
61  shared_object = LoadLibraryW(pluginName);
62  if (!shared_object) {
63  char cwd[1024];
64  THROW_IE_EXCEPTION << "Cannot load library '" << details::wStringtoMBCSstringChar(std::wstring(pluginName)) << "': " << GetLastError()
65  << " from cwd: " << _getcwd(cwd, sizeof(cwd));
66  }
67  }
68 #endif
69 
70  explicit SharedObjectLoader(LPCSTR pluginName) {
71  ExcludeCurrentDirectory();
72 
73  shared_object = LoadLibrary(pluginName);
74  if (!shared_object) {
75  char cwd[1024];
76  THROW_IE_EXCEPTION << "Cannot load library '" << pluginName << "': " << GetLastError()
77  << " from cwd: " << _getcwd(cwd, sizeof(cwd));
78  }
79  }
80 
81  ~SharedObjectLoader() {
82  FreeLibrary(shared_object);
83  }
84 
85  /**
86  * @brief Searches for a function symbol in the loaded module
87  * @param symbolName Name of function to find
88  * @return A pointer to the function if found
89  * @throws InferenceEngineException if the function is not found
90  */
91  void* get_symbol(const char* symbolName) const {
92  if (!shared_object) {
93  THROW_IE_EXCEPTION << "Cannot get '" << symbolName << "' content from unknown library!";
94  }
95  auto procAddr = reinterpret_cast<void*>(GetProcAddress(shared_object, symbolName));
96  if (procAddr == nullptr)
97  THROW_IE_EXCEPTION << "GetProcAddress cannot locate method '" << symbolName << "': " << GetLastError();
98 
99  return procAddr;
100  }
101 };
102 
103 } // namespace details
104 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
Inference Engine API.
Definition: ie_argmax_layer.hpp:15
The macro defines a symbol import/export mechanism essential for Microsoft Windows(R) OS...
This is a header file with functions related to filesystem operations.
A header file for the main Inference Engine exception.