7

I have two C application that can communicate via win32 IPC APIs (CreateFileMapping() etc.)

I have to replace client application with a Python application.

I have tried the following libraries on Python side.

import win32file, win32api

But this libraries does not have CreateFileMapping() functions.

I have tried also mmap.mmap() function but I could not observe any communication.

import mmap

sharedMemory = mmap.mmap(0, 512, "Local\\SharedBuffer")

sharedMemory.write("AB")

I have also tried "Global\SharedBuffer" and "SharedBuffer" as shared Memory names both two side.

#define SHARED_BUFFER_NAME          ((LPCSTR)L"Local\\SharedBuffer")

HANDLE bufferHandle = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, 512, SHARED_BUFFER_NAME);

// Create a map for accessing Shared Buffer
sharedBuffer = (char*)MapViewOfFile(bufferHandle, FILE_MAP_ALL_ACCESS, 0, 0, SHARED_BUFFER_SIZE);

memset(sharedBuffer, 0, SHARED_BUFFER_SIZE);

while (sharedBuffer[0] == 0);

while (1);

win32 APIs are not mandatory for me. I need only simple shared buffer between C and python application on Windows Machine.

Thanks

6
  • 2
    why are you casting a const wchar_t* to a const char*? Commented Sep 30, 2014 at 7:50
  • Where is this casting? MapViewOfFile() function returns 'void*' so I am casting it to char array to access bytes. Commented Sep 30, 2014 at 7:53
  • 1
    You are casting here: #define SHARED_BUFFER_NAME ((LPCSTR)L"Local\\SharedBuffer") It should be LPCWSTR but you casted it to a const char* aka LPCSTR.. Commented Oct 2, 2014 at 23:24
  • 1
    Also, are you sure that python's mmap works with windows memory mapped objects? It seems to work like in Java (memory-mapped FILES).. meaning that you memory map a file on disk instead of pages. But I'm not sure if that's true or not. You'll have to test it. Commented Oct 2, 2014 at 23:30
  • 1
    @Brandon: Windows uses memory-mapped "file sections" for shared memory. Commented May 19, 2023 at 20:06

1 Answer 1

17

I tested this.. it works.. Run the C++ code first. It will create a memory map. Then run the python code. It will write to the map. The C++ code will read the map and print what was written to it..

I know this code is BAD because I don't serialise the data properly (aka writing the size to the file first then the data, etc..) but meh.. It's JUST a BASIC working example.. Nothing more.

Python:

import mmap

shm = mmap.mmap(0, 512, "Local\\Test") #You should "open" the memory map file instead of attempting to create it..
if shm:
    shm.write(bytes("5", 'UTF-8'));
    shm.write(bytes("Hello", 'UTF-8'))
    print("GOOD")

C++:

#include <windows.h>
#include <cstring>
#include <cstdbool>
#include <iostream>

typedef struct
{
    void* hFileMap;
    void* pData;
    char MapName[256];
    size_t Size;
} SharedMemory;

bool CreateMemoryMap(SharedMemory* shm)
{
    if ((shm->hFileMap = CreateFileMapping(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, shm->Size, shm->MapName)) == NULL)
    {
        return false;
    }

    if ((shm->pData = MapViewOfFile(shm->hFileMap, FILE_MAP_ALL_ACCESS, 0, 0, shm->Size)) == NULL)
    {
        CloseHandle(shm->hFileMap);
        return false;
    }
    return true;
}

bool FreeMemoryMap(SharedMemory* shm)
{
    if (shm && shm->hFileMap)
    {
        if (shm->pData)
        {
            UnmapViewOfFile(shm->pData);
        }

        if (shm->hFileMap)
        {
            CloseHandle(shm->hFileMap);
        }
        return true;
    }
    return false;
}

int main()
{
    SharedMemory shm = {0};
    shm.Size = 512;
    sprintf(shm.MapName, "Local\\Test");

    if (CreateMemoryMap(&shm))
    {
        char* ptr = (char*)shm.pData;
        memset(ptr, 0, shm.Size);

        while (ptr && (*ptr == 0))
        {
            Sleep(100);
        }

        int size = (int)*ptr;
        ptr += sizeof(char);

        int i = 0;
        for (; i < size; ++i)
        {
            std::cout<<ptr[i];
        }
        FreeMemoryMap(&shm);
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

Btw.. this is C code.. not really C++ (other than the use of std::cout I took this C structure and its accompanying functions from my github.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.