From db766496a8de357cc1460161ea0dd969dd8e5ab9 Mon Sep 17 00:00:00 2001 From: Jackson Sun Date: Wed, 24 Aug 2016 13:21:39 +0800 Subject: [PATCH] Add wchar_t* file name support for file_mapping constructor --- .../interprocess/detail/os_file_functions.hpp | 8 ++++++ include/boost/interprocess/detail/win32_api.hpp | 2 ++ include/boost/interprocess/file_mapping.hpp | 33 ++++++++++++++++++++++ 3 files changed, 43 insertions(+) diff --git a/include/boost/interprocess/detail/os_file_functions.hpp b/include/boost/interprocess/detail/os_file_functions.hpp index bcb9576a..7f3628f3 100644 --- a/include/boost/interprocess/detail/os_file_functions.hpp +++ b/include/boost/interprocess/detail/os_file_functions.hpp @@ -145,6 +145,14 @@ inline file_handle_t open_existing_file (name, (unsigned int)mode, winapi::open_existing, attr, 0); } +inline file_handle_t open_existing_file + (const wchar_t *name, mode_t mode = read_write, bool temporary = false) +{ + unsigned long attr = temporary ? winapi::file_attribute_temporary : 0; + return winapi::create_file + (name, (unsigned int)mode, winapi::open_existing, attr, 0); +} + inline bool delete_file(const char *name) { return winapi::unlink_file(name); } diff --git a/include/boost/interprocess/detail/win32_api.hpp b/include/boost/interprocess/detail/win32_api.hpp index 02c7b427..52807dc5 100644 --- a/include/boost/interprocess/detail/win32_api.hpp +++ b/include/boost/interprocess/detail/win32_api.hpp @@ -870,6 +870,7 @@ extern "C" __declspec(dllimport) int __stdcall DuplicateHandle , unsigned long dwDesiredAccess, int bInheritHandle , unsigned long dwOptions); extern "C" __declspec(dllimport) long __stdcall GetFileType(void *hFile); +extern "C" __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int, unsigned long, const wchar_t*, int, char*, int, const char*, int*); extern "C" __declspec(dllimport) void *__stdcall FindFirstFileA(const char *lpFileName, win32_find_data *lpFindFileData); extern "C" __declspec(dllimport) int __stdcall FindNextFileA(void *hFindFile, win32_find_data *lpFindFileData); extern "C" __declspec(dllimport) int __stdcall FindClose(void *hFindFile); @@ -887,6 +888,7 @@ extern "C" __declspec(dllimport) void * __stdcall CreateFileMappingA (void *, in extern "C" __declspec(dllimport) void * __stdcall MapViewOfFileEx (void *, unsigned long, unsigned long, unsigned long, std::size_t, void*); extern "C" __declspec(dllimport) void * __stdcall OpenFileMappingA (unsigned long, int, const char *); extern "C" __declspec(dllimport) void * __stdcall CreateFileA (const char *, unsigned long, unsigned long, struct interprocess_security_attributes*, unsigned long, unsigned long, void *); +extern "C" __declspec(dllimport) void * __stdcall CreateFileW (const wchar_t *, unsigned long, unsigned long, struct interprocess_security_attributes*, unsigned long, unsigned long, void *); extern "C" __declspec(dllimport) void __stdcall GetSystemInfo (struct system_info *); extern "C" __declspec(dllimport) int __stdcall FlushViewOfFile (void *, std::size_t); extern "C" __declspec(dllimport) int __stdcall VirtualUnlock (void *, std::size_t); diff --git a/include/boost/interprocess/file_mapping.hpp b/include/boost/interprocess/file_mapping.hpp index 0d1cf1fe..10ef95c0 100644 --- a/include/boost/interprocess/file_mapping.hpp +++ b/include/boost/interprocess/file_mapping.hpp @@ -60,6 +60,10 @@ class file_mapping //!modes. Throws interprocess_exception on error. file_mapping(const char *filename, mode_t mode); +#if (defined BOOST_INTERPROCESS_WINDOWS) + file_mapping(const wchar_t *filename, mode_t mode); +#endif + //!Moves the ownership of "moved"'s file mapping object to *this. //!After the call, "moved" does not represent any file mapping object. //!Does not throw @@ -160,6 +164,35 @@ inline file_mapping::file_mapping m_mode = mode; } +#if (defined BOOST_INTERPROCESS_WINDOWS) +inline file_mapping::file_mapping + (const wchar_t* filename, mode_t mode) +{ + std::wstring src = filename; + m_filename.resize(src.size() * 6 + 1); + //65001 is CP_UTF8 + int len = WideCharToMultiByte(65001, 0, filename, -1, &m_filename[0], m_filename.size(), 0, 0); + m_filename.resize(len); + + //Check accesses + if (mode != read_write && mode != read_only){ + error_info err = other_error; + throw interprocess_exception(err); + } + + //Open file + m_handle = ipcdetail::open_existing_file(filename, mode); + + //Check for error + if(m_handle == ipcdetail::invalid_file()){ + error_info err = system_error_code(); + this->priv_close(); + throw interprocess_exception(err); + } + m_mode = mode; +} +#endif + inline bool file_mapping::remove(const char *filename) { return ipcdetail::delete_file(filename); }