Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
515 views
in Technique[技术] by (71.8m points)

c++ - IPC Between Python and Win32 on Windows OS

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 "GlobalSharedBuffer" 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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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);
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...