]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/dll/example/tutorial9/tutorial9.cpp
1 // Copyright 2015-2016 Antony Polukhin.
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)
7 #include <boost/predef/os.h>
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>
17 namespace dll
= boost::dll
;
20 typedef HANDLE(__stdcall GetStdHandle_t
)(DWORD
); // function signature with calling convention
22 // OPTION #0, requires C++11 compatible compiler that understands GetStdHandle_t signature.
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
>(
28 boost::dll::load_mode::search_system_folders
30 std::cout
<< "0.0 GetStdHandle() returned " << get_std_handle(STD_OUTPUT_HANDLE
) << std::endl
;
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
;
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");
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
;
52 #else // BOOST_WINDOWS
54 #include <boost/dll/shared_library.hpp> // for dll::shared_library
60 #endif // BOOST_WINDOWS