Publishing 2019 R1 content
[platform/upstream/dldt.git] / inference-engine / include / details / os / win_shared_object_loader.h
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         shared_object = LoadLibrary(pluginName);
42         if (!shared_object) {
43             THROW_IE_EXCEPTION << "Cannot load library '"
44                 << pluginName << "': "
45                 << GetLastError()
46                 << " from cwd: " << _getcwd(cwd, 1024);
47         }
48     }
49     ~SharedObjectLoader() {
50         FreeLibrary(shared_object);
51     }
52
53     /**
54      * @brief Searches for a function symbol in the loaded module
55      * @param symbolName Name of function to find
56      * @return A pointer to the function if found
57      * @throws InferenceEngineException if the function is not found
58      */
59     void *get_symbol(const char* symbolName) const {
60         if (!shared_object) {
61             THROW_IE_EXCEPTION << "Cannot get '" << symbolName << "' content from unknown library!";
62         }
63         auto procAddr = reinterpret_cast<void*>(GetProcAddress(shared_object, symbolName));
64         if (procAddr == nullptr)
65             THROW_IE_EXCEPTION << "GetProcAddress cannot locate method '" << symbolName << "': " << GetLastError();
66
67         return procAddr;
68     }
69 };
70
71 }  // namespace details
72 }  // namespace InferenceEngine