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 <windows.h>
21 #include <direct.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 '"
58  << pluginName << "': "
59  << GetLastError()
60  << " from cwd: " << _getcwd(cwd, 1024);
61  }
62  }
63  ~SharedObjectLoader() {
64  FreeLibrary(shared_object);
65  }
66 
67  /**
68  * @brief Searches for a function symbol in the loaded module
69  * @param symbolName Name of function to find
70  * @return A pointer to the function if found
71  * @throws InferenceEngineException if the function is not found
72  */
73  void *get_symbol(const char* symbolName) const {
74  if (!shared_object) {
75  THROW_IE_EXCEPTION << "Cannot get '" << symbolName << "' content from unknown library!";
76  }
77  auto procAddr = reinterpret_cast<void*>(GetProcAddress(shared_object, symbolName));
78  if (procAddr == nullptr)
79  THROW_IE_EXCEPTION << "GetProcAddress cannot locate method '" << symbolName << "': " << GetLastError();
80 
81  return procAddr;
82  }
83 };
84 
85 } // namespace details
86 } // namespace InferenceEngine
#define THROW_IE_EXCEPTION
A macro used to throw the exception with a notable description.
Definition: ie_exception.hpp:22
Definition: ie_argmax_layer.hpp:11