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  /**
53  * @brief A shared pointer to SharedObjectLoader
54  */
55  using Ptr = std::shared_ptr<SharedObjectLoader>;
56 
57 #ifdef ENABLE_UNICODE_PATH_SUPPORT
58  /**
59  * @brief Loads a library with the name specified. The library is loaded according to the
60  * WinAPI LoadLibrary rules
61  * @param pluginName Full or relative path to the plugin library
62  */
63  explicit SharedObjectLoader(LPCWSTR pluginName) {
64  ExcludeCurrentDirectory();
65 
66  shared_object = LoadLibraryW(pluginName);
67  if (!shared_object) {
68  char cwd[1024];
69  THROW_IE_EXCEPTION << "Cannot load library '" << details::wStringtoMBCSstringChar(std::wstring(pluginName)) << "': " << GetLastError()
70  << " from cwd: " << _getcwd(cwd, sizeof(cwd));
71  }
72  }
73 #endif
74 
75  explicit SharedObjectLoader(LPCSTR pluginName) {
76  ExcludeCurrentDirectory();
77 
78  shared_object = LoadLibraryA(pluginName);
79  if (!shared_object) {
80  char cwd[1024];
81  THROW_IE_EXCEPTION << "Cannot load library '" << pluginName << "': " << GetLastError()
82  << " from cwd: " << _getcwd(cwd, sizeof(cwd));
83  }
84  }
85 
86  ~SharedObjectLoader() {
87  FreeLibrary(shared_object);
88  }
89 
90  /**
91  * @brief Searches for a function symbol in the loaded module
92  * @param symbolName Name of function to find
93  * @return A pointer to the function if found
94  * @throws InferenceEngineException if the function is not found
95  */
96  void* get_symbol(const char* symbolName) const {
97  if (!shared_object) {
98  THROW_IE_EXCEPTION << "Cannot get '" << symbolName << "' content from unknown library!";
99  }
100  auto procAddr = reinterpret_cast<void*>(GetProcAddress(shared_object, symbolName));
101  if (procAddr == nullptr)
102  THROW_IE_EXCEPTION << "GetProcAddress cannot locate method '" << symbolName << "': " << GetLastError();
103 
104  return procAddr;
105  }
106 };
107 
108 } // namespace details
109 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:25
Definition: cldnn_config.hpp:16
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.