From d6590f88b0f4fdfda8cca843e180e752c5871722 Mon Sep 17 00:00:00 2001 From: Jinhao Date: Wed, 27 Jun 2018 04:26:23 +0800 Subject: [PATCH] remove the dependency of ole32.lib --- source/detail/mswin/platform_spec.hpp | 6 ++++-- source/detail/platform_spec_windows.cpp | 12 ++++++++++++ source/gui/filebox.cpp | 7 +++---- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/source/detail/mswin/platform_spec.hpp b/source/detail/mswin/platform_spec.hpp index 5eb6df45..a1845683 100644 --- a/source/detail/mswin/platform_spec.hpp +++ b/source/detail/mswin/platform_spec.hpp @@ -3,8 +3,8 @@ * Nana C++ Library(http://www.nanapro.org) * Copyright(C) 2003-2017 Jinhao(cnjinhao@hotmail.com) * - * Distributed under the Boost Software License, Version 1.0. - * (See accompanying file LICENSE_1_0.txt or copy at + * Distributed under the Boost Software License, Version 1.0. + * (See accompanying file LICENSE_1_0.txt or copy at * http://www.boost.org/LICENSE_1_0.txt) * * @file: nana/detail/platform_spec.hpp @@ -128,6 +128,8 @@ namespace detail public: co_initializer(); ~co_initializer(); + + void task_mem_free(void* p); private: HMODULE ole32_; }; diff --git a/source/detail/platform_spec_windows.cpp b/source/detail/platform_spec_windows.cpp index a3dc0b2a..179b87cf 100644 --- a/source/detail/platform_spec_windows.cpp +++ b/source/detail/platform_spec_windows.cpp @@ -100,6 +100,18 @@ namespace detail } } + void platform_spec::co_initializer::task_mem_free(void* p) + { + if (ole32_) + { + using CoTaskMemFree_t = void (__stdcall *)(LPVOID pv); + + CoTaskMemFree_t free_fn = reinterpret_cast(::GetProcAddress(ole32_, "CoTaskMemFree")); + if (free_fn) + free_fn(p); + } + } + struct platform_spec::implementation { std::map iconbase; diff --git a/source/gui/filebox.cpp b/source/gui/filebox.cpp index b94c6677..fcaba515 100644 --- a/source/gui/filebox.cpp +++ b/source/gui/filebox.cpp @@ -16,6 +16,7 @@ #if defined(NANA_WINDOWS) # include +# include "../detail/mswin/platform_spec.hpp" # ifndef NANA_MINGW // isn't supported well on MinGW # include # else @@ -1247,7 +1248,7 @@ namespace nana #ifdef NANA_WINDOWS std::optional target; - ::CoInitialize(nullptr); + nana::detail::platform_spec::co_initializer co_init; #ifndef NANA_MINGW IFileDialog *fd(nullptr); HRESULT hr = CoCreateInstance(CLSID_FileOpenDialog, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&fd)); @@ -1294,11 +1295,9 @@ namespace nana if (FALSE != SHGetPathFromIDList(pidl, folder_path)) target = folder_path; - CoTaskMemFree(pidl); + co_init.task_mem_free(pidl); } #endif - ::CoUninitialize(); - return target; #elif defined(NANA_POSIX)