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 #define _WINSOCKAPI_
18 // Avoidance of Windows.h to define min/max.
19 #ifndef NOMINMAX
20 #define NOMINMAX
21 #endif
22 #include <direct.h>
23 #include <windows.h>
24 
25 namespace InferenceEngine {
26 namespace details {
27 
28 /**
29  * @brief This class provides an OS shared module abstraction
30  */
31 class SharedObjectLoader {
32 private:
33  HMODULE shared_object;
34 
35  void ExcludeCurrentDirectory() {
36  // Exclude current directory from DLL search path process wise.
37  // If application specific path was configured before then
38  // current directory is alread excluded.
39  // GetDLLDirectory does not distinguish if aplication specific
40  // path was set to "" or NULL so reset it to "" to keep
41  // aplication safe.
42  if (GetDllDirectory(0, NULL) <= 1) {
43  SetDllDirectory(TEXT(""));
44  }
45  }
46 
47 public:
48  /**
49  * @brief Loads a library with the name specified. The library is loaded according to the
50  * WinAPI LoadLibrary rules
51  * @param pluginName Full or relative path to the plugin library
52  */
53  explicit SharedObjectLoader(LPCWSTR pluginName) {
54  ExcludeCurrentDirectory();
55 
56  shared_object = LoadLibraryW(pluginName);
57  if (!shared_object) {
58  char cwd[1024];
59  THROW_IE_EXCEPTION << "Cannot load library '" << details::wStringtoMBCSstringChar(std::wstring(pluginName)) << "': " << GetLastError()
60  << " from cwd: " << _getcwd(cwd, sizeof(cwd));
61  }
62  }
63 
64  explicit SharedObjectLoader(LPCSTR pluginName) {
65  ExcludeCurrentDirectory();
66 
67  shared_object = LoadLibrary(pluginName);
68  if (!shared_object) {
69  char cwd[1024];
70  THROW_IE_EXCEPTION << "Cannot load library '" << pluginName << "': " << GetLastError()
71  << " from cwd: " << _getcwd(cwd, sizeof(cwd));
72  }
73  }
74 
75  ~SharedObjectLoader() {
76  FreeLibrary(shared_object);
77  }
78 
79  /**
80  * @brief Searches for a function symbol in the loaded module
81  * @param symbolName Name of function to find
82  * @return A pointer to the function if found
83  * @throws InferenceEngineException if the function is not found
84  */
85  void* get_symbol(const char* symbolName) const {
86  if (!shared_object) {
87  THROW_IE_EXCEPTION << "Cannot get '" << symbolName << "' content from unknown library!";
88  }
89  auto procAddr = reinterpret_cast<void*>(GetProcAddress(shared_object, symbolName));
90  if (procAddr == nullptr)
91  THROW_IE_EXCEPTION << "GetProcAddress cannot locate method '" << symbolName << "': " << GetLastError();
92 
93  return procAddr;
94  }
95 };
96 
97 } // namespace details
98 } // 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.