472,805 Members | 1,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,805 software developers and data experts.

Shared Memory Example (Python, ctypes, VC++)

Hello,
Here are code snippets to create and access shared memory in Python
with and without ctypes module.

With regards,
Srijit

Filename : SharedMemCreate.py

import msvcrt, mmap
from ctypes import *

FILE_MAP_ALL_ACCESS = 0xF001F
INVALID_HANDLE_VALUE = 0xFFFFFFFF
SHMEMSIZE = 256
PAGE_READWRITE = 0x04
szName = c_char_p("MyFileMappingObject_ctypes")
szMsg = "Message from Python(ctypes) process"

hMapObject = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_ VALUE,
None, PAGE_READWRITE, 0, SHMEMSIZE, szName)
if (hMapObject == 0):
print "Could not open file mapping object"
raise WinError()

pBuf = windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS,
0, 0, SHMEMSIZE)
if (pBuf == 0):
print "Could not map view of file"
raise WinError()
else:
memcpy = cdll.msvcrt.memcpy
memcpy(pBuf, szMsg, len(szMsg))

shmem = mmap.mmap(0, 256, "MyFileMappingObject", mmap.ACCESS_WRITE)
shmem.write("Message from Python process")

msvcrt.getch()

windll.kernel32.UnmapViewOfFile(pBuf)
windll.kernel32.CloseHandle(hMapObject)
shmem.close()

Filename : SharedMemAccess.py

from ctypes import *
import mmap

FILE_MAP_ALL_ACCESS = 0xF001F
INVALID_HANDLE_VALUE = 0xFFFFFFFF
FALSE = 0
TRUE = 1
SHMEMSIZE = 256
szName = c_char_p("MyFileMappingObject")

hMapObject = windll.kernel32.OpenFileMappingA(FILE_MAP_ALL_ACCE SS,
FALSE, szName)
if (hMapObject == 0):
print "Could not open file mapping object"
raise WinError()

pBuf = windll.kernel32.MapViewOfFile(hMapObject, FILE_MAP_ALL_ACCESS,
0, 0, 0)

if (pBuf == 0):
print "Could not map view of file"
raise WinError()
else:
pBuf_str = cast(pBuf, c_char_p)
print pBuf_str.value

windll.kernel32.UnmapViewOfFile(pBuf)
windll.kernel32.CloseHandle(hMapObject)

shmem = mmap.mmap(0, 256, "MyFileMappingObject_ctypes",
mmap.ACCESS_READ)
print shmem.read(64)
shmem.close()
Source code to access shared memory, created in Python, from VC++
program

// Cplusplus_SharedMemoryAccess.cpp : Defines the entry point for the
console application.
//

#include "stdafx.h"
using namespace std;

#include <windows.h>
#include <memory.h>
#include <conio.h>
#include <stdio.h>

#define SHMEMSIZE 256

HANDLE hMapObject = NULL; // handle to file mapping
LPCTSTR pBuf;
TCHAR szName[]= TEXT("MyFileMappingObject");

int _tmain(int argc, _TCHAR* argv[])
{
hMapObject = OpenFileMapping(
FILE_MAP_ALL_ACCESS, // read/write access
FALSE, // do not inherit the name
szName // name of mapping object
);

if (hMapObject == NULL || hMapObject == INVALID_HANDLE_VALUE) {
cout << "Could not open file mapping object " << GetLastError()
<< endl;
return 0;
}
pBuf = (LPTSTR) MapViewOfFile(hMapObject, // handle to mapping
object
FILE_MAP_ALL_ACCESS, // read/write
permission
0,
0,
SHMEMSIZE
);

if (pBuf == NULL)
{
cout << "Could not map view of file " << GetLastError() << endl;

return 0;
}

cout << "pBuf = " << pBuf << endl;
MessageBox(NULL, pBuf, TEXT("Process2"), MB_OK);

UnmapViewOfFile(pBuf);
CloseHandle(hMapObject);

return 0;
}

Jul 18 '05 #1
1 7355
Thanks.


Jul 18 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Srijit Kumar Bhadra | last post by:
Hello, Here is some sample code with pywin32 build 203 and ctypes 0.9.6. Best regards, /Srijit File: SharedMemCreate_Mutex_win32all.py # This application should be used with...
5
by: Claudio Grondi | last post by:
Background information: --------------------------------- in order to monitor mainboard sensory data as fan speeds, temperatures, applications like SpeedFan http://www.almico.com/speedfan.php or...
3
by: alanrn | last post by:
I would like to start a dialog on how to implement the equivalent functionality of UNIX shared memory in .NET. I work with a factory automation system. The bulk of the system is written in C/C++....
1
by: Gerald Klix | last post by:
I read the whol email thread carefully and could not find any sentence by Guido, which states that he does not accept ctypes for the standard library. He just declined to rewrite winreg. Did I miss...
2
by: Sebastjan Trepca | last post by:
Hey! Are there any "live" modules for working with shared memory in Python? I found some but they are all dead(posh,shm)... Thanks, Sebastjan
0
by: follower | last post by:
This post is mostly Google-bait for anyone else that might want to compile SpiderMonkey ( libjs / libjs.so / libjs.dylib ) for OS X (10.4.5 in my case) and then use it with Python's ctypes. I can't...
0
by: Nikita the Spider | last post by:
Hi all, In the late 90s Vladimir Marangozov wrote a module that provided an interface to System V shared memory on *nix platforms. I found a copy on the Net, dusted it off, compiled it, plugged a...
5
by: Tim | last post by:
Hello Everyone, I am getting shared memory in python using the following. szName = c_char_p(name) hMapObject = windll.kernel32.CreateFileMappingA(INVALID_HANDLE_VALUE, None, PAGE_READONLY, 0,...
2
by: gaurav kashyap | last post by:
Dear all, I have a server program that listens to a particular port and a number of client programs that connect to the server. Now i want to put some data in form of python list in main...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
0
by: Rina0 | last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.