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