Windows操作系统上的Python和Win32之间的IPC [英] IPC Between Python and Win32 on Windows OS

查看:96
本文介绍了Windows操作系统上的Python和Win32之间的IPC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个可以通过Win32 IPC API(CreateFileMapping()等)进行通信的C应用程序

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

我必须用Python应用程序替换客户端应用程序.

I have to replace client application with a Python application.

我已经在Python方面尝试了以下库.

I have tried the following libraries on Python side.

导入win32file,win32api

import win32file, win32api

但是该库没有CreateFileMapping()函数.

But this libraries does not have CreateFileMapping() functions.

我也尝试了mmap.mmap()函数,但没有观察到任何通信.

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

我还尝试了"Global \ SharedBuffer"和"SharedBuffer"作为共享内存名称,双方.

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 API对我来说不是必需的.我只需要Windows机器上C和python应用程序之间的简单共享缓冲区.

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

谢谢

推荐答案

我对此进行了测试..它有效..首先运行C ++代码.它将创建一个内存映射.然后运行python代码.它将写入地图.C ++代码将读取地图并打印写入其中的内容.

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..

我知道这段代码是错误的,因为我没有正确地序列化数据(也就是先将文件大小写到文件中,然后再将数据写到文件中,等等),但是…….这只是一个BASIC工作示例strong> ..仅此而已.

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

这篇关于Windows操作系统上的Python和Win32之间的IPC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆