File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- # Copyright (c) 2018-2025 Jean-Louis Leroy
2- # Distributed under the Boost Software License, Version 1.0.
3- # See accompanying file LICENSE_1_0.txt
4- # or copy at http://www.boost.org/LICENSE_1_0.txt)
5-
61# Illustrates the dllvar technique: the exe owns (exports) a variable; the
72# DLL imports it. Both see static_answer<answer_dllvar, void>::value as the
83# same linker symbol because the varying base class does not enter the mangled
94# name; SFINAE selects dllexport in the owner and dllimport in the client.
105
6+ cmake_minimum_required (VERSION 4.2 )
7+
118if (NOT (WIN32 OR CYGWIN ))
129 return ()
1310endif ()
Original file line number Diff line number Diff line change 1- // Copyright (c) 2018-2025 Jean-Louis Leroy
2- // Distributed under the Boost Software License, Version 1.0.
3- // See accompanying file LICENSE_1_0.txt
4- // or copy at http://www.boost.org/LICENSE_1_0.txt)
5-
61// Owner: answer_dllvar derives from dllexport_tag here.
72#define WINDLL_OWNER
83#include " shared.hpp"
@@ -13,8 +8,9 @@ int main() {
138 answer () = 42 ;
149 auto plugin = LoadLibraryA (" windll_plugin.dll" );
1510 assert (plugin);
16- auto get_answer = reinterpret_cast <int (*)()>(
17- GetProcAddress (plugin, " get_answer" ));
18- assert (get_answer () == 42 );
11+ auto print_answer = reinterpret_cast <void (*)()>(
12+ GetProcAddress (plugin, " print_answer" ));
13+ assert (print_answer);
14+ print_answer (); // prints "answer = 42"
1915 FreeLibrary (plugin);
2016}
Original file line number Diff line number Diff line change 1- // Copyright (c) 2018-2025 Jean-Louis Leroy
2- // Distributed under the Boost Software License, Version 1.0.
3- // See accompanying file LICENSE_1_0.txt
4- // or copy at http://www.boost.org/LICENSE_1_0.txt)
5-
61// Client: answer_dllvar derives from dllimport_tag here (no WINDLL_OWNER).
72#include " shared.hpp"
3+ #include < cstdio>
84
9- // answer() returns a reference to static_answer<answer_dllvar, void>::value —
10- // the one shared linker symbol — bound via dllimport.
11- extern " C " __declspec(dllexport) int get_answer () { return answer (); }
5+ extern " C " __declspec(dllexport) void print_answer () {
6+ printf ( " answer = %d \n " , answer ());
7+ }
Original file line number Diff line number Diff line change 1- // Copyright (c) 2018-2025 Jean-Louis Leroy
2- // Distributed under the Boost Software License, Version 1.0.
3- // See accompanying file LICENSE_1_0.txt
4- // or copy at http://www.boost.org/LICENSE_1_0.txt)
5-
61#pragma once
72#include < type_traits>
83
@@ -48,6 +43,4 @@ struct static_answer<Tag,
4843 static __declspec (dllimport) int value;
4944};
5045
51- // Accessor: always refers to static_answer<answer_dllvar, void>::value —
52- // the one shared linker symbol — regardless of which specialization is active.
5346inline int & answer () { return static_answer<answer_dllvar>::value; }
You can’t perform that action at this time.
0 commit comments