From a93e9de9f5eaada1fe8ce8ecc1cebfd1614ed2b9 Mon Sep 17 00:00:00 2001 From: Yun Peng Date: Thu, 30 Jun 2016 14:27:38 +0000 Subject: Added a dll hello world example program run following commands to test it: bazel build --cpu=x64_windows_msvc examples/windows/dll:hello ./bazel-bin/examples/windows/dll/hello -- Change-Id: I096f6ff66473b1c9980b0aa36ec291f39512dd71 Reviewed-on: https://bazel-review.googlesource.com/#/c/3932 MOS_MIGRATED_REVID=126300284 --- examples/windows/dll/BUILD | 15 +++++++++++++++ examples/windows/dll/hello-library.cpp | 23 +++++++++++++++++++++++ examples/windows/dll/hello-world.cpp | 30 ++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+) create mode 100644 examples/windows/dll/BUILD create mode 100644 examples/windows/dll/hello-library.cpp create mode 100644 examples/windows/dll/hello-world.cpp (limited to 'examples') diff --git a/examples/windows/dll/BUILD b/examples/windows/dll/BUILD new file mode 100644 index 0000000000..2b7e956fc5 --- /dev/null +++ b/examples/windows/dll/BUILD @@ -0,0 +1,15 @@ +cc_binary( + name = "hellolib.dll", + srcs = [ + "hello-library.cpp", + ], + linkshared = 1, +) + +cc_binary( + name = "hello", + srcs = [ + "hello-world.cpp", + ], + data = [":hellolib.dll"], +) diff --git a/examples/windows/dll/hello-library.cpp b/examples/windows/dll/hello-library.cpp new file mode 100644 index 0000000000..4cfd7c9c0a --- /dev/null +++ b/examples/windows/dll/hello-library.cpp @@ -0,0 +1,23 @@ +#include +#include + +#ifdef __cplusplus +extern "C" { +#endif + +#include + +__declspec(dllexport) char *get_time() { + time_t ltime; + time(<ime); + return ctime(<ime); +} + +__declspec(dllexport) void say_hello(char *message) { + printf("Hello from dll!\n%s", message); +} + +#ifdef __cplusplus +} +#endif + diff --git a/examples/windows/dll/hello-world.cpp b/examples/windows/dll/hello-world.cpp new file mode 100644 index 0000000000..7483e9f070 --- /dev/null +++ b/examples/windows/dll/hello-world.cpp @@ -0,0 +1,30 @@ +#include +#include +typedef char *(__cdecl *GET_TIME_PTR)(); +typedef void(__cdecl *SAY_HELLO_PTR)(char *); + +int main() { + HINSTANCE hellolib; + GET_TIME_PTR get_time; + SAY_HELLO_PTR say_hello; + + bool success = FALSE; + + hellolib = LoadLibrary(TEXT("hellolib.dll")); + + if (hellolib != NULL) { + get_time = (GET_TIME_PTR)GetProcAddress(hellolib, "get_time"); + say_hello = (SAY_HELLO_PTR)GetProcAddress(hellolib, "say_hello"); + + if (NULL != get_time && NULL != say_hello) { + success = TRUE; + char *now = get_time(); + say_hello(now); + } + FreeLibrary(hellolib); + } + + if (!success) printf("Failed to load dll and call functions\n"); + + return 0; +} -- cgit v1.2.3