Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

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_ACCESS,
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;
}


Report this thread to moderator Post Follow-up to this message
Old Post
Srijit Kumar Bhadra
03-19-05 08:59 PM


Re: Shared Memory Example (Python, ctypes, VC++)
Thanks.





Report this thread to moderator Post Follow-up to this message
Old Post
Do Re Mi chel La Si Do
03-19-05 08:59 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

Python archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 09:12 PM.

 

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.