]>
Commit | Line | Data |
---|---|---|
92f5a8d4 | 1 | // Copyright 2015-2019 Antony Polukhin. |
7c673cae FG |
2 | // |
3 | // Distributed under the Boost Software License, Version 1.0. | |
4 | // (See accompanying file LICENSE_1_0.txt | |
5 | // or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | ||
7 | #include <boost/predef/os.h> | |
8 | #if BOOST_OS_WINDOWS | |
9 | ||
10 | //[callplugcpp_tutorial9 | |
11 | #include <boost/dll/import.hpp> // for dll::import | |
12 | #include <boost/dll/shared_library.hpp> // for dll::shared_library | |
13 | #include <boost/function.hpp> | |
14 | #include <iostream> | |
15 | #include <windows.h> | |
16 | ||
17 | namespace dll = boost::dll; | |
18 | ||
19 | int main() { | |
20 | typedef HANDLE(__stdcall GetStdHandle_t)(DWORD ); // function signature with calling convention | |
21 | ||
22 | // OPTION #0, requires C++11 compatible compiler that understands GetStdHandle_t signature. | |
23 | /*<-*/ | |
24 | #if defined(_MSC_VER) && !defined(BOOST_NO_CXX11_TRAILING_RESULT_TYPES) && !defined(BOOST_NO_CXX11_DECLTYPE) && !defined(BOOST_NO_CXX11_VARIADIC_TEMPLATES) && !defined(BOOST_NO_CXX11_RVALUE_REFERENCES) /*->*/ | |
25 | auto get_std_handle = dll::import<GetStdHandle_t>( | |
26 | "Kernel32.dll", | |
27 | "GetStdHandle", | |
28 | boost::dll::load_mode::search_system_folders | |
29 | ); | |
30 | std::cout << "0.0 GetStdHandle() returned " << get_std_handle(STD_OUTPUT_HANDLE) << std::endl; | |
31 | ||
32 | // You may put the `get_std_handle` into boost::function<>. But boost::function<Signature> can not compile with | |
33 | // Signature template parameter that contains calling conventions, so you'll have to remove the calling convention. | |
34 | boost::function<HANDLE(DWORD)> get_std_handle2 = get_std_handle; | |
35 | std::cout << "0.1 GetStdHandle() returned " << get_std_handle2(STD_OUTPUT_HANDLE) << std::endl; | |
36 | /*<-*/ | |
37 | #endif /*->*/ | |
38 | ||
39 | // OPTION #1, does not require C++11. But without C++11 dll::import<> can not handle calling conventions, | |
40 | // so you'll need to hand write the import. | |
41 | dll::shared_library lib("Kernel32.dll", dll::load_mode::search_system_folders); | |
42 | GetStdHandle_t& func = lib.get<GetStdHandle_t>("GetStdHandle"); | |
43 | ||
44 | // Here `func` does not keep a reference to `lib`, you'll have to deal with that on your own. | |
45 | std::cout << "1.0 GetStdHandle() returned " << func(STD_OUTPUT_HANDLE) << std::endl; | |
46 | ||
47 | return 0; | |
48 | } | |
49 | ||
50 | //] | |
51 | ||
52 | #else // BOOST_WINDOWS | |
53 | ||
54 | #include <boost/dll/shared_library.hpp> // for dll::shared_library | |
55 | ||
56 | int main() { | |
57 | return 0; | |
58 | } | |
59 | ||
60 | #endif // BOOST_WINDOWS |