dll.h
1 /*
2  * EEZ PSU Firmware
3  * Copyright (C) 2015-present, Envox d.o.o.
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9 
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14 
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #pragma once
20 
21 #ifdef _WIN32
22 #include <windows.h>
23 typedef HANDLE eez_dll_lib_t;
24 #define EEZ_DLL_EXPORT extern "C" __declspec(dllexport)
25 #else
26 #include <dlfcn.h>
27 typedef void* eez_dll_lib_t;
28 #define EEZ_DLL_EXPORT extern "C"
29 #endif
30 
31 inline eez_dll_lib_t eez_dll_load(const char* lib_file_path) {
32 # ifdef _WIN32
33  return ::LoadLibraryA(lib_file_path);
34 # else //_WIN32
35  return ::dlopen(lib_file_path, RTLD_LAZY);
36 # endif //_WIN32
37 }
38 
39 inline void eez_dll_unload(eez_dll_lib_t lib) {
40 # ifdef _WIN32
41  ::FreeLibrary((HMODULE)lib);
42 # else //_WIN32
43  ::dlclose(lib);
44 # endif //_WIN32
45 }
46 
47 inline void* eez_dll_get_proc_address(eez_dll_lib_t lib, const char* proc_name) {
48 # ifdef _WIN32
49  return ::GetProcAddress((HMODULE)lib, proc_name);
50 # else //_WIN32
51  return ::dlsym(lib, proc_name);
52 # endif //_WIN32
53 }