diff --git a/src/include/cpputils2/win/shm/shm.hpp b/src/include/cpputils2/win/shm/shm.hpp index f0cb8aa..a135f8f 100644 --- a/src/include/cpputils2/win/shm/shm.hpp +++ b/src/include/cpputils2/win/shm/shm.hpp @@ -9,6 +9,8 @@ #include "cpputils2/common/types.hpp" +#include + #include #include #include @@ -27,15 +29,22 @@ namespace CppUtils2 } Shm(const std::string& file_mem_path) + : mem_path(file_mem_path.begin(), file_mem_path.end()), hMapFile(nullptr), pBuf{ nullptr } + { + } + + Shm(const std::wstring& file_mem_path) : mem_path(file_mem_path), hMapFile(nullptr), pBuf{ nullptr } { } + + /// @brief Open an existing shared memory /// @param file_mem_path Path to the shared memory /// @param mem_size Size of the shared memory /// @return 0 on success, -1 on error - Result open_existing(const std::string& file_mem_path, std::size_t mem_size) + Result open_existing(const std::wstring& file_mem_path, std::size_t mem_size) { mem_path = file_mem_path; return open_existing(mem_size); @@ -50,7 +59,7 @@ namespace CppUtils2 { return Result::RET_ERROR; } - int flags = 0; + auto flags = Operation::OPEN; return shared_open(flags, mem_size); } @@ -64,7 +73,7 @@ namespace CppUtils2 return Result::RET_ERROR; } - int flags = 1; + auto flags = Operation::CREATE; return shared_open(flags, mem_size); } @@ -105,27 +114,28 @@ namespace CppUtils2 } private: - std::string mem_path; + std::wstring mem_path; HANDLE hMapFile; LPVOID pBuf; - - Result shared_open(int flags, std::size_t mem_size) + enum class Operation: int { - if (flags == 1) { - - //TCHAR szName[] = TEXT(mem_path.c_str()); + OPEN, + CREATE + }; - hMapFile = CreateFileMappingA( + Result shared_open(Operation flags, std::size_t mem_size) + { + if (flags == Operation::CREATE) { + hMapFile = CreateFileMapping( INVALID_HANDLE_VALUE, // use paging file nullptr, // default security PAGE_READWRITE, // read/write access 0, // maximum object size (high-order DWORD) mem_size, // maximum object size (low-order DWORD) - mem_path.c_str()); // name of mapping object + (LPCTSTR)mem_path.c_str()); // name of mapping object if (hMapFile == NULL) { - return Result::RET_ERROR; } @@ -138,19 +148,17 @@ namespace CppUtils2 if (pBuf == nullptr) { //_tprintf(TEXT("Could not map view of file (%d).\n"), GetLastError()); - CloseHandle(hMapFile); - return Result::RET_ERROR; } } else { - hMapFile = OpenFileMappingA( + hMapFile = OpenFileMapping( FILE_MAP_ALL_ACCESS, // read/write access FALSE, // do not inherit the name - mem_path.c_str()); // name of mapping object + (LPCTSTR)mem_path.c_str()); // name of mapping object if (hMapFile == NULL) { diff --git a/src/test/cpputils2_tests.cpp b/src/test/cpputils2_tests.cpp index e9ba701..6044e72 100644 --- a/src/test/cpputils2_tests.cpp +++ b/src/test/cpputils2_tests.cpp @@ -145,7 +145,7 @@ namespace #ifdef _WIN32 TEST(ExampleSHM, TestSHM) { - CppUtils2::Shm shm("test_shm"); + CppUtils2::Shm shm(L"test_shm"); auto ret = shm.allocate(sizeof(int32_t)); EXPECT_NE(ret, CppUtils2::Result::RET_ERROR); void* ptr = shm.get_raw_ptr(); @@ -159,9 +159,10 @@ namespace shm.unlink(); EXPECT_TRUE(true); } + TEST(ExampleShm, TestExisting) { - CppUtils2::Shm shm("test_shm"); + CppUtils2::Shm shm(L"test_shm"); auto ret = shm.allocate(sizeof(int32_t)); EXPECT_NE(ret, CppUtils2::Result::RET_ERROR);