Skip to content

Commit d7dd003

Browse files
jll63claude
andcommitted
samples/windll: simplify to pure exe-exports-int / DLL-imports-int
Remove all openmethod references. The example now demonstrates the core DLL mechanism in isolation: shared.hpp declares an int with dllexport/dllimport, main.cpp defines answer=42 and LoadLibraryA's the plugin, plugin.cpp exports get_answer() which reads the shared variable. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent ad0aa59 commit d7dd003

4 files changed

Lines changed: 29 additions & 38 deletions

File tree

samples/windll/CMakeLists.txt

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@
33
# See accompanying file LICENSE_1_0.txt
44
# or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
# Illustrates the dllvar mechanism: the exe owns (exports) the registry state;
7-
# the DLL imports it, contributing overriders that are consolidated by
8-
# initialize() before first use.
6+
# Illustrates the exe-exports-data / DLL-imports-data pattern on Windows:
7+
# the exe defines and exports a variable; the DLL imports and reads it.
98
#
109
# On non-Windows both compilation units are linked into a single executable.
1110

@@ -14,11 +13,10 @@ if (WIN32 OR CYGWIN)
1413
add_executable(windll_main main.cpp)
1514
target_compile_definitions(windll_main PRIVATE WINDLL_OWNER)
1615
set_target_properties(windll_main PROPERTIES ENABLE_EXPORTS ON)
17-
target_link_libraries(windll_main PRIVATE Boost::openmethod)
1816

19-
# DLL imports the registry state from the exe.
17+
# DLL imports the variable from the exe.
2018
add_library(windll_plugin SHARED plugin.cpp)
21-
target_link_libraries(windll_plugin PRIVATE Boost::openmethod windll_main)
19+
target_link_libraries(windll_plugin PRIVATE windll_main)
2220
set_target_properties(windll_plugin PROPERTIES ENABLE_EXPORTS ON)
2321

2422
# windll_main loads windll_plugin at runtime; build both before testing.
@@ -32,7 +30,6 @@ else()
3230
# Non-Windows: single executable, no DLL decoration needed.
3331
add_executable(windll_main main.cpp plugin.cpp)
3432
target_compile_definitions(windll_main PRIVATE WINDLL_OWNER)
35-
target_link_libraries(windll_main PRIVATE Boost::openmethod)
3633

3734
add_test(NAME windll COMMAND windll_main)
3835
endif()

samples/windll/main.cpp

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,25 @@
33
// See accompanying file LICENSE_1_0.txt
44
// or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
// This module owns the registry state (WINDLL_OWNER -> dllexport).
76
#define WINDLL_OWNER
87
#include "shared.hpp"
9-
#include <boost/openmethod/initialize.hpp>
108
#include <cassert>
11-
#include <string>
129
#ifdef _WIN32
1310
#include <windows.h>
1411
#endif
1512

16-
using namespace boost::openmethod;
17-
18-
BOOST_OPENMETHOD_CLASSES(Animal, Dog);
19-
BOOST_OPENMETHOD_OVERRIDE(speak, (virtual_ptr<Animal>), const char*) { return "?"; }
13+
int answer = 42;
2014

2115
int main() {
2216
#ifdef _WIN32
23-
// Load plugin before initialize() so its static constructors register
24-
// the Dog overrider into the shared registry state.
25-
LoadLibraryA("windll_plugin.dll");
17+
auto plugin = LoadLibraryA("windll_plugin.dll");
18+
assert(plugin);
19+
auto get_answer = (int (*)())GetProcAddress(plugin, "get_answer");
20+
assert(get_answer);
21+
assert(get_answer() == 42);
22+
FreeLibrary(plugin);
23+
#else
24+
extern int get_answer();
25+
assert(get_answer() == 42);
2626
#endif
27-
initialize();
28-
29-
Dog d;
30-
assert(speak(virtual_ptr<Dog>::final(d)) == std::string("woof"));
31-
Animal a;
32-
assert(speak(virtual_ptr<Animal>::final(a)) == std::string("?"));
3327
}

samples/windll/plugin.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,14 @@
33
// See accompanying file LICENSE_1_0.txt
44
// or copy at http://www.boost.org/LICENSE_1_0.txt)
55

6-
// This module imports the registry state from the exe (no WINDLL_OWNER).
76
#include "shared.hpp"
87

9-
using namespace boost::openmethod;
8+
#ifdef _WIN32
9+
#define EXPORT __declspec(dllexport)
10+
#else
11+
#define EXPORT
12+
#endif
1013

11-
BOOST_OPENMETHOD_CLASSES(Animal, Dog);
12-
BOOST_OPENMETHOD_OVERRIDE(speak, (virtual_ptr<Dog>), const char*) { return "woof"; }
14+
extern "C" EXPORT int get_answer() {
15+
return answer;
16+
}

samples/windll/shared.hpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,16 @@
55

66
#pragma once
77

8-
// Define WINDLL_OWNER in the module that owns (exports) the registry state.
9-
// All other modules that link against it automatically import it.
8+
// The module that owns (exports) the variable defines WINDLL_OWNER before
9+
// including this header. All other modules import it.
1010
#ifdef _WIN32
1111
# ifdef WINDLL_OWNER
12-
# define BOOST_OPENMETHOD_EXPORT_DEFAULT_REGISTRY
12+
# define WINDLL_API __declspec(dllexport)
1313
# else
14-
# define BOOST_OPENMETHOD_IMPORT_DEFAULT_REGISTRY
14+
# define WINDLL_API __declspec(dllimport)
1515
# endif
16+
#else
17+
# define WINDLL_API
1618
#endif
1719

18-
#include <boost/openmethod.hpp>
19-
20-
struct Animal { virtual ~Animal() = default; };
21-
struct Dog : Animal {};
22-
23-
BOOST_OPENMETHOD_CLASSES(Animal, Dog);
24-
BOOST_OPENMETHOD(speak, (boost::openmethod::virtual_ptr<Animal>), const char*);
20+
WINDLL_API extern int answer;

0 commit comments

Comments
 (0)