473,498 Members | 310 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with access to shared memory(W2K) / ORIGINALLY (win32) speedfan api control

Background information:
---------------------------------
in order to monitor mainboard sensory data
as fan speeds, temperatures, applications
like SpeedFan http://www.almico.com/speedfan.php
or MBM http://mbm.livewiredev.com/
can be used.
Both of the mentioned apps expose data got
from the hardware in a shared memory area.

The goal to achieve:
---------------------------
is to supervise the motherboard sensory
data from within a Python script

The approach:
-------------------
access to the shared memory area
exposed by motherboard hardware
querying apps from Python script.

The problem:
-----------------
the below listed script code returns
always same values, what
leads to the conclusion, that something
is going wrong.

Can anyone help?

Thanks in advance

Claudio

The code
-------------
I use is provided also here:
http://www.codecomments.com/Python/message430495.html
http://mail.python.org/pipermail/pyt...ch/271853.html
(the mmap part of it doesn't work at all)

from ctypes import *

class Buffer(Structure):
# just to see if any useful data can be acquired
_fields_ = [
("Data0", c_longlong)
,("Data1", c_longlong)
,("Data2", c_longlong)
,("Data3", c_longlong)
,("Data4", c_longlong)
,("Data5", c_longlong)
,("Data6", c_longlong)
,("Data7", c_longlong)
,("Data8", c_longlong)
,("Data9", c_longlong)
,("DataA", c_longlong)
,("DataB", c_longlong)
,("DataC", c_longlong)
,("DataD", c_longlong)
,("DataE", c_longlong)
,("DataF", c_longlong)
]

szName = c_char_p("SFSharedMemory_ALM") # SpeedFan
szName = c_char_p("$M$B$M$5$S$D$") # MBM
# szName = c_char_p("blabla") # not existing shared memory

FILE_MAP_ALL_ACCESS = 0xF001F
FALSE = 0

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:
print repr(pBuf)
print repr(hMapObject)

pBuf_str = cast(pBuf, c_char_p)
print repr(pBuf_str.value)
print repr(pBuf_str)

pBuf_buf = cast(pBuf, Buffer)
print
print
print repr(pBuf_buf)
print
print repr(pBuf_buf.Data0)
print repr(pBuf_buf.Data1)
print repr(pBuf_buf.Data2)
print repr(pBuf_buf.Data3)
print repr(pBuf_buf.Data4)
print repr(pBuf_buf.Data5)
print repr(pBuf_buf.Data6)
print repr(pBuf_buf.Data7)
print repr(pBuf_buf.Data8)
print repr(pBuf_buf.Data9)
print repr(pBuf_buf.DataA)
print repr(pBuf_buf.DataB)
print repr(pBuf_buf.DataC)
print repr(pBuf_buf.DataD)
print repr(pBuf_buf.DataE)
print repr(pBuf_buf.DataF)
#:if/else (pBuf == 0)

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

The output
--------------
is always the same:

13107200
80
''
c_char_p('')
<__main__.Buffer object at 0x007E3570>

13107200L
0L
0L
0L
0L
0L
0L
0L
0L
0L
0L
0L
0L
0L
0L
0L

Jul 18 '05 #1
5 4405
"Claudio Grondi" <cl************@freenet.de> writes:
Background information:
---------------------------------
in order to monitor mainboard sensory data
as fan speeds, temperatures, applications
like SpeedFan http://www.almico.com/speedfan.php
or MBM http://mbm.livewiredev.com/
can be used.
Both of the mentioned apps expose data got
from the hardware in a shared memory area.

The goal to achieve:
---------------------------
is to supervise the motherboard sensory
data from within a Python script

The approach:
-------------------
access to the shared memory area
exposed by motherboard hardware
querying apps from Python script.
For the mistake you made see below, hope that helps.

For the general way to solve this problem you should know that you can
access a shared memory area with a certain name also with Python's mmap
module, although that is probably not documented very good. There was a
recipe posted a few days ago which showed different ways to acces shared
memory, with or without ctypes.
The problem:
-----------------
the below listed script code returns
always same values, what
leads to the conclusion, that something
is going wrong.

Can anyone help?

Thanks in advance

Claudio

The code
-------------
I use is provided also here:
http://www.codecomments.com/Python/message430495.html
http://mail.python.org/pipermail/pyt...ch/271853.html
(the mmap part of it doesn't work at all)

from ctypes import *

class Buffer(Structure):
# just to see if any useful data can be acquired
_fields_ = [
("Data0", c_longlong)
,("Data1", c_longlong)
,("Data2", c_longlong)
,("Data3", c_longlong)
,("Data4", c_longlong)
,("Data5", c_longlong)
,("Data6", c_longlong)
,("Data7", c_longlong)
,("Data8", c_longlong)
,("Data9", c_longlong)
,("DataA", c_longlong)
,("DataB", c_longlong)
,("DataC", c_longlong)
,("DataD", c_longlong)
,("DataE", c_longlong)
,("DataF", c_longlong)
]

szName = c_char_p("SFSharedMemory_ALM") # SpeedFan
szName = c_char_p("$M$B$M$5$S$D$") # MBM
# szName = c_char_p("blabla") # not existing shared memory

FILE_MAP_ALL_ACCESS = 0xF001F
FALSE = 0

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:
print repr(pBuf)
print repr(hMapObject)

pBuf_str = cast(pBuf, c_char_p)
print repr(pBuf_str.value)
print repr(pBuf_str)

pBuf_buf = cast(pBuf, Buffer)

Here's the problem. pBuf is a pointer to a Buffer structure, not the
buffer structure itself.
Something like
pBuf_buf = Buffer.from_address(pBuf)
may work.

Thomas
Jul 18 '05 #2
> For the mistake you made see below, hope that helps.
It doesn't.
pBuf_buf = cast(pBuf, Buffer) Here's the problem. pBuf is a pointer to a Buffer structure, not the
buffer structure itself.
Something like
pBuf_buf = Buffer.from_address(pBuf)
may work.

If I currently understand it right, cast() should
do exactly this, i.e. should write the content
pointed to by pBuf to a Python variable
pBuf_buf which is an object of the Buffer
class, where I can access the single
components like any other Python
variables - am I right or wrong?

How can I access content at any
real memory address in Python
like you suggest with the
fictive(?) .from_address(memPointer)
method?
Wouldn't it be anyway protected?
Is not cast() a kind of solution to this
problem?

Because the exepected buffer
contains zero bytes I can't use
cast(pBuf, c_char_p) to get the entire
content as it is done in the recipe
provided by Srijit Kumar Bhadra.
At least the .Data0 value is set by cast()
in my trials, but it seems, that this has
nothing to do with the shared memory
area content.
For the general way to solve this problem you should know that you can
access a shared memory area with a certain name also with Python's mmap
module, although that is probably not documented very good. There was a
recipe posted a few days ago which showed different ways to acces shared
memory, with or without ctypes.

The code I use is provided also here:
http://www.codecomments.com/Python/message430495.html
http://mail.python.org/pipermail/pyt...ch/271853.html
(the mmap part of it doesn't work at all)


So I am already using the recipe, right?
But it doesn't work as expected.
mmap doesn't work at all failing in
shmem = mmap.mmap(0, 0, "$M$B$M$5$S$D$", mmap.ACCESS_READ)
with WindowsError: [Errno 87] wrong parameter

I am on Windows 2000 SP 4, Pyhon version 2.3.5
using ctypes version 0.9.5 .

Any further hints?

Claudio
Jul 18 '05 #3
"Claudio Grondi" <cl************@freenet.de> writes:
For the mistake you made see below, hope that helps.

It doesn't.
> pBuf_buf = cast(pBuf, Buffer)

Here's the problem. pBuf is a pointer to a Buffer structure, not the
buffer structure itself.
Something like
pBuf_buf = Buffer.from_address(pBuf)
may work.

If I currently understand it right, cast() should
do exactly this, i.e. should write the content
pointed to by pBuf to a Python variable
pBuf_buf which is an object of the Buffer
class, where I can access the single
components like any other Python
variables - am I right or wrong?


You are right - my bad.
pBuf is a pointer to your structure, so:

ptr = cast(pBuf, POINTER(Buffer))
print ptr # should print <ctypes.LP_Buffer object at ...>
struct = ptr[0]

and now 'struct' should contain the data that you need.
For the general way to solve this problem you should know that you can
access a shared memory area with a certain name also with Python's mmap
module, although that is probably not documented very good. There was a
recipe posted a few days ago which showed different ways to acces shared
memory, with or without ctypes.

> The code I use is provided also here:
> http://www.codecomments.com/Python/message430495.html
> http://mail.python.org/pipermail/pyt...ch/271853.html
> (the mmap part of it doesn't work at all)


So I am already using the recipe, right?
But it doesn't work as expected.
mmap doesn't work at all failing in
shmem = mmap.mmap(0, 0, "$M$B$M$5$S$D$", mmap.ACCESS_READ)
with WindowsError: [Errno 87] wrong parameter


I get this error when I try to open a non-existing shared memory block.
From the mmap docs:

If length is 0, the maximum length of the map is the current size of
the file, except that if the file is empty Windows raises an exception
(you cannot create an empty mapping on Windows).

If I run it with non-zero length it does not raise an exception

shmem = mmap.mmap(0, 0, "$M$B$M$5$S$D$", mmap.ACCESS_READ)

although it *creates* the shared memory block, IIUC. Are you sure your
speedfan app is running when you get the WindowsError?

Thomas
Jul 18 '05 #4
Thomas Heller,
it seems, that your email address
th*****@python.net doesn't work
or at least don't accept attachments,
so maybe you can provide me with
another one?
ptr = cast(pBuf, POINTER(Buffer))
print ptr # should print <ctypes.LP_Buffer object at ...>
struct = ptr[0]
results in breakdown of Python
(Windows exception, can't read from memory
at 0x0...07)
due to existance of ptr = cast(pBuf, POINTER(Buffer)) but the print print ptr # should print <ctypes.LP_Buffer object at ...> does its output to the console...

Strange...

Claudio

"Thomas Heller" <th*****@python.net> schrieb im Newsbeitrag
news:y8**********@python.net... "Claudio Grondi" <cl************@freenet.de> writes:
For the mistake you made see below, hope that helps.

It doesn't.
> pBuf_buf = cast(pBuf, Buffer)
Here's the problem. pBuf is a pointer to a Buffer structure, not the
buffer structure itself.
Something like
pBuf_buf = Buffer.from_address(pBuf)
may work.

If I currently understand it right, cast() should
do exactly this, i.e. should write the content
pointed to by pBuf to a Python variable
pBuf_buf which is an object of the Buffer
class, where I can access the single
components like any other Python
variables - am I right or wrong?


You are right - my bad.
pBuf is a pointer to your structure, so:

ptr = cast(pBuf, POINTER(Buffer))
print ptr # should print <ctypes.LP_Buffer object at ...>
struct = ptr[0]

and now 'struct' should contain the data that you need.
For the general way to solve this problem you should know that you can
access a shared memory area with a certain name also with Python's mmap
module, although that is probably not documented very good. There was a recipe posted a few days ago which showed different ways to acces shared memory, with or without ctypes.

> The code I use is provided also here:
> http://www.codecomments.com/Python/message430495.html
> http://mail.python.org/pipermail/pyt...ch/271853.html
> (the mmap part of it doesn't work at all)


So I am already using the recipe, right?
But it doesn't work as expected.
mmap doesn't work at all failing in
shmem = mmap.mmap(0, 0, "$M$B$M$5$S$D$", mmap.ACCESS_READ)
with WindowsError: [Errno 87] wrong parameter


I get this error when I try to open a non-existing shared memory block.
From the mmap docs:

If length is 0, the maximum length of the map is the current size of
the file, except that if the file is empty Windows raises an exception
(you cannot create an empty mapping on Windows).

If I run it with non-zero length it does not raise an exception

shmem = mmap.mmap(0, 0, "$M$B$M$5$S$D$", mmap.ACCESS_READ)

although it *creates* the shared memory block, IIUC. Are you sure your
speedfan app is running when you get the WindowsError?

Thomas

Jul 18 '05 #5
Supervising motherboard sensory data as
fan speeds or CPU and board temperatures
in Python can be done with help of the MBM 5
utility available at http://mbm.livewiredev.com/.

This utility exposes data got from the hardware
sensors in a shared memory area for querying
by another applications.

The pyMBM.py script provided below prints
excerpts of the content of the by MBM 5
in shared memory area provided data
and can be used as a starting point
for writing own scripts which need to
query motherboard sensory data or
as inspiration for creating an
appropriate pyMBM Python module.

Claudio Grondi

print
print ' pyMBM.py ver. 0.10 2005-04-08 '
print '*** ************************************************ ***'
print '*** THIS code accesses shared memory area exposed by ***'
print '*** MBM 5 (MotherBoard Monitor) ***'
print '*** install and run MBM 5 before using this script ***'
print '*** MBM 5 home: http://mbm.livewiredev.com ***'
print '*** Port of the MBM API to Python by Claudio Grondi ***'
print '*** http://www.python.org/moin/ClaudioGrondi ***'
print '*** ************************************************ ***'

# centralized settings for portability and upgradability
SENSOR_INDEX_LENGTH = 10
SENSOR_INFO_LENGTH = 100

SENSOR_NAME_LEN = 12
TIME_STRING_LENGTH = 41
MBM_PATHNAME_LENGTH = 256

SMBUSNAME_STRING_LENGTH = 41

PADDING1_SIZE_BYTES = 3
PADDING2_SIZE_BYTES = 4
PADDING3_SIZE_BYTES = 8

#define BT_ISA "type_isa"
#define BT_SMBUS "type_smbus"
#define BT_VIA686ABUS "type_via686abus"
#define BT_DIRECTIO "type_directio"
#define BT_UNKNOWN "type_unknown"
# typedef enum { btISA = 0, btSMBus, btVIA686ABus, btDirectIO } bus_t;
bus_t = [
"ISA " # 0
,"SMBus " # 1
,"Via686aBus" # 2
,"DirectIO " # 3
]

#define SMBT_INTEL "type_intel"
#define SMBT_AMD "type_amd"
#define SMBT_ALI "type_ali"
#define SMBT_NFORCE "type_nforce"
#define SMBT_SIS "type_sis"
#define SMBT_UNK BT_UNKNOWN
# typedef enum { smtSMBIntel = 0, smtSMBAMD, smtSMBALi, smtSMBNForce,
smtSMBSIS } smb_t;
smb_t = [
"Intel " # 0
,"AMD " # 1
,"Ali " # 2
,"nForce" # 3
,"sis " # 4
]

#define ST_UNKNOWN BT_UNKNOWN
#define ST_TEMPERATURE "type_temperature"
#define ST_VOLTAGE "type_voltage"
#define ST_FAN "type_fan"
#define ST_MHZ "type_mhz"
#define ST_PERCENTAGE "type_percentage"
# typedef enum { stUnknown = 0, stTemperature, stVoltage, stFan, stMhz,
stPercentage } sensor_t;
sensor_t = [
"unknown " # 0
,"temperature" # 1
,"voltage " # 2
,"fan " # 3
,"mhz " # 4
,"percentage " # 5
]

from ctypes import *

class index(Structure):
_fields_ = [
("_type" , c_int ) # type of sensor
,("_count" , c_int ) # number of sensor for that type
]
class sensor(Structure):
_fields_ = [
("_type" , c_ubyte ) # type of sensor
,("_name" , c_ubyte * SENSOR_NAME_LEN ) # name of sensor
,("_padding1", c_ubyte * PADDING1_SIZE_BYTES ) # padding of 3 byte
,("_current" , c_double ) # current value
,("_low" , c_double ) # lowest readout
,("_high" , c_double ) # highest readout
,("_readout" , c_long ) # total number of readout
,("_padding2", c_ubyte * PADDING2_SIZE_BYTES ) # padding of 4 byte
,("_total" , c_double ) # actual a long double - total amout of
all readouts
,("_padding3", c_ubyte * PADDING3_SIZE_BYTES ) # padding of 6 byte
,("_alarm1" , c_double ) # temp & fan: low alarm; voltage: % off;
,("_alarm2" , c_double ) # temp: high alarm
]
class info(Structure):
_fields_ = [
("_smbBase" , c_short ) # SMBus base address
,("_smbType" , c_ubyte ) # bus_t variant of SMBus/Isa bus used to
access chip
# typedef enum
{btISA=0,btSMBus,btVIA686ABus,btDirectIO} bus_t;
,("_smbCode" , c_ubyte ) # smb_t variant of SMBus sub type, Intel,
AMD or ALi
# typedef enum
{smtSMBIntel=0,smtSMBAMD,smtSMBALi,smtSMBNForce,sm tSMBSIS} smb_t;
,("_smbAddr" , c_ubyte ) # Address of sensor chip on SMBus
,("_smbName" , c_ubyte * SMBUSNAME_STRING_LENGTH ) # Nice name for
SMBus
,("_isaBase" , c_short ) # ISA base address of sensor chip on ISA
,("_chipType" , c_int ) # Chip nr, connects with Chipinfo.ini
,("_voltageSubType", c_ubyte ) # Subvoltage option selected
]
class data_t(Structure):
_fields_ = [
("_version", c_double ) # version number (example:
51090)
,("_index" , index * SENSOR_INDEX_LENGTH ) # Sensor index
,("_sensor" , sensor * SENSOR_INFO_LENGTH ) # sensor info
,("_info" , info ) # misc. info
,("_start" , c_ubyte * TIME_STRING_LENGTH ) # start time,
TIME_STRING_LENGTH = 41
,("_current", c_ubyte * TIME_STRING_LENGTH ) # current time
,("_path" , c_ubyte * MBM_PATHNAME_LENGTH ) # MBM path
]

FILE_MAP_READ = 2
strNameOfSharedMemoryAreaToAccess = "$M$B$M$5$S$D$"

handle = windll.kernel32.OpenFileMappingA(FILE_MAP_READ, 0,
strNameOfSharedMemoryAreaToAccess)
if not handle:
raise WinError()
addr = windll.kernel32.MapViewOfFile(
handle
,FILE_MAP_READ
,0
,0
,0
)
if not addr:
raise WinError()
obj_data_t = data_t.from_address(addr)

# obj_data_t holds the entire shared memory area content which can be
accessed
# now from within Python code like it is done below:

if(__name__ == '__main__'):
print
print '_version ', obj_data_t._version
print

# print '_index ', obj_data_t._index
itemCounter = 0
for item in obj_data_t._index:
itemCounter += 1
print '_index[%2i]._type ='%itemCounter, sensor_t[item._type],
'_index._count =',repr(item._count)
print

# print '_sensor ', obj_data_t._sensor
itemCounter = 0
for item in obj_data_t._sensor:
itemCounter += 1
print '_sensor[%3i]._type ='%itemCounter, sensor_t[item._type],
strSensorName = ''
for subitem in item._name:
strSensorName += chr(subitem)
print ' ._name =', strSensorName,
print ' ._current = %7.2f'%item._current, ' ._low = %7.2f'%item._low,
' ._high = %7.2f'%item._high
print
# print '_info ', obj_data_t._info
print '_info._smbType =', bus_t[obj_data_t._info._smbType]
print '_info._smbCode =', smb_t[obj_data_t._info._smbCode]

print
print '_start ',
strStart = ''
for char in obj_data_t._start:
strStart += chr(char)
print strStart
print '_current ',
strCurrent = ''
for char in obj_data_t._current:
strCurrent += chr(char)
print strCurrent,
print
print '_path ',
strPath = ''
for char in obj_data_t._path:
strPath += chr(char)
print strPath,

windll.kernel32.UnmapViewOfFile(addr)
windll.kernel32.CloseHandle(handle)

print
raw_input(' (exit with ENTER) #> OK? ')
#:if
Jul 18 '05 #6

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

Similar topics

1
1605
by: BDob | last post by:
Standard documents "Access control is not considered in determining overriding." To further explain what's puzzling me, I put up an example below: class Base { public: virtual void...
3
1392
by: john sun | last post by:
Hi, I don't understand one thing when authoring cp ctor. The below is simplified class and cp constructor. When initializing the data member, we use object ref TEST to access its private member...
5
2413
by: Fabio Rossi | last post by:
Hi! I'm learning C++ from the book "Thinking in C++". Now I'm reading about nested classes and access control. I have written this code #include <iostream> class Outer { private: int...
0
1197
by: Soeren S. Joergensen | last post by:
Hi, When using pinvoke to get an access control list (ACL) and from that ACL getting every entry (ACE) I need to get the SID for wich this ACE concerns: ACE (for allowed access) structure...
1
1840
by: TK | last post by:
What's the best way to implement roll-base access control with forms authentication? I have a IIS6+ASP.NET server which hosts some ASP.NET web applications as separated path that's like...
0
2062
by: William F. Zachmann | last post by:
A web site that will run on Windows Server 2003 and IIS 6.0 needs to provide three levels of access, one for the public and two others for two levels of subscribers. This is a port of a prior site...
4
2035
by: JimC | last post by:
On my main form in a C# program, I create an instance of another form that contains a ListView control, in the usual way, that is: public class frmMain : System.Windows.Forms.Form { // ...
6
2992
by: Notgiven | last post by:
I am considering a large project and they currently use LDAP on MS platform. It would be moved to a LAMP platform. OpenLDAP is an option though I have not used it before. I do feel fairly...
0
1325
by: islandfong | last post by:
Hi, I am using ASP.NET 2.0 to implement a user access control system. The idea is that I would allow users (admin) to create new role, new user, assign user to role and the role is given...
8
2119
by: xz | last post by:
Why C++ (as well as Java) adopts class-based access control instead of instance-based access control? I had never paid attention to whether an access-control is class-based or instance-based but...
0
7124
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7163
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6884
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7375
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5460
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3090
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3078
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1416
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
287
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.