Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 24 additions & 16 deletions src/include/cpputils2/win/shm/shm.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

#include "cpputils2/common/types.hpp"

#include <string>

#include <windows.h>
#include <stdio.h>
#include <conio.h>
Expand All @@ -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);
Expand All @@ -50,7 +59,7 @@ namespace CppUtils2
{
return Result::RET_ERROR;
}
int flags = 0;
auto flags = Operation::OPEN;
return shared_open(flags, mem_size);
}

Expand All @@ -64,7 +73,7 @@ namespace CppUtils2
return Result::RET_ERROR;
}

int flags = 1;
auto flags = Operation::CREATE;

return shared_open(flags, mem_size);
}
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
{
Expand Down
5 changes: 3 additions & 2 deletions src/test/cpputils2_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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);

Expand Down
Loading