473,659 Members | 2,690 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

ctypes NULL pointers; was: Python To Send Emails Via Outlook Express

Posted in a previous thread was some Python code for accessing Window's
Simple MAPI api using the ctypes module.

http://groups-beta.google.com/group/...fa74cdba9b7be9

This Simple MAPI module was Ian's completed version of an example
I had posted in an earlier message. In it I had to set some pointer
fields in a C structure to NULL. I was not happy with the solution I
used so I made an inquiry on the ctypes-users mailing list. Thanks to
Thomas Heller I got some answers.

The simplest way to create a NULL pointer instance in ctypes is to
call the pointer class without an argument.

from ctypes import POINTER, c_int
INT_P = POINTER(c_int) # A pointer to a C integer
p = INT_P() # An INT_P instance set to NULL

When the pointer is a member of a structure things are a little different.
For the ctypes primatives c_void_p, c_char_p and c_wchar_p one can simply
use None to set the field NULL.

from ctypes import Structure, c_void_p
class STRUCT(Structur e):
_fields_ = [('voidptr', c_void_p)]
s=STRUCT( None ) # Create an instance with voidptr field NULL

This is documented in the ctypes tutorial. A problem with ctypes 0.9.2
prevents using None to initialize POINTER derived fields. This has
been fixed so future releases will allow it. In the meantime the
following will work.

class IPSTRUCT(Struct ure):
_fields_ [('intptr'), INT_P]
s=IPSTRUCT( INT_P() )

A null pointer of the correct type is assigned to the field. I hope this
clears up any confusion my Simple MAPI example may have caused regarding
ctypes pointers.

Lenard Lindstrom
<le***@telus.ne t>
Jul 18 '05 #1
3 5535
ian
Thanks again Lenard!!

Jul 18 '05 #2
ian
Hi Lenard
Hopefully I have understood you properly.
The updated script is now as follows, or you can download it from
http://www.kirbyfooty.com/simplemapi.py
Thanks again for all your help!!!

Kindest regards
Ian Cook
--------------------------------------------------------------------------
import os
from ctypes import *

FLAGS = c_ulong
LHANDLE = c_ulong
LPLHANDLE = POINTER(LHANDLE )
# Return codes
SUCCESS_SUCCESS = 0
# Recipient class
MAPI_ORIG = 0
MAPI_TO = 1
class STRUCT(Structur e):
_fields_ = [('voidptr', c_void_p)]
#NULL = c_void_p(None)
NULL=STRUCT( None ) # Create an instance with voidptr field NULL

class MapiRecipDesc(S tructure):
_fields_ = [('ulReserved', c_ulong),
('ulRecipClass' , c_ulong),
('lpszName', c_char_p),
('lpszAddress', c_char_p),
('ulEIDSize', c_ulong),
('lpEntryID', c_void_p),
]
lpMapiRecipDesc = POINTER(MapiRec ipDesc)
class MapiFileDesc(St ructure):
_fields_ = [('ulReserved', c_ulong),
('flFlags', c_ulong),
('nPosition', c_ulong),
('lpszPathName' , c_char_p),
('lpszFileName' , c_char_p),
('lpFileType', c_void_p),
]
lpMapiFileDesc = POINTER(MapiFil eDesc)
class MapiMessage(Str ucture):
_fields_ = [('ulReserved', c_ulong),
('lpszSubject', c_char_p),
('lpszNoteText' , c_char_p),
('lpszMessageTy pe', c_char_p),
('lpszDateRecei ved', c_char_p),
('lpszConversat ionID', c_char_p),
('flFlags', FLAGS),
('lpOriginator' , lpMapiRecipDesc ), # ignored?
('nRecipCount', c_ulong),
('lpRecips', lpMapiRecipDesc ),
('nFileCount', c_ulong),
('lpFiles', lpMapiFileDesc) ,
]
lpMapiMessage = POINTER(MapiMes sage)
MAPI = windll.mapi32
MAPISendMail=MA PI.MAPISendMail
MAPISendMail.re stype = c_ulong # Error code
MAPISendMail.ar gtypes = (LHANDLE, # lhSession
c_ulong, # ulUIParam
lpMapiMessage, # lpMessage
FLAGS, # lpFlags
c_ulong, # ulReserved
)
def SendMail(recipi ent, subject, body, attachfiles):
"""Post an e-mail message using Simple MAPI
Special thanks to Lenard Lindstrom!

recipient - string: address to send to (multiple address sperated
with a semicolin)
subject - string: subject header
body - string: message text
attach - string: files to attach (multiple attachments sperated
with a semicolin)

Example usage
import simplemapi

simplemapi.Send Mail("to******* *@server.com;to ********@server .com","My
Subject","My message body","c:\attac hment1.txt;c:\a ttchment2")
"""

# get list of file attachments
attach = []
AttachWork = attachfiles.spl it(';')

#verify the attachment file exists
for file in AttachWork:
if os.path.exists( file):
attach.append(f ile)
attach = map( os.path.abspath , attach )
nFileCount = len(attach)

if attach:
MapiFileDesc_A = MapiFileDesc * len(attach)
fda = MapiFileDesc_A( )
for fd, fa in zip(fda, attach):
fd.ulReserved = 0
fd.flFlags = 0
fd.nPosition = -1
fd.lpszPathName = fa
fd.lpszFileName = None
fd.lpFileType = None
lpFiles = fda
else:
# No attachments
lpFiles = cast(NULL, lpMapiFileDesc) # Make NULL

# Get the number of recipients
RecipWork = recipient.split (';')
RecipCnt = len(RecipWork)

# Formulate the recipients
MapiRecipDesc_A = MapiRecipDesc * len(RecipWork)
rda = MapiRecipDesc_A ()
for rd, ra in zip(rda, RecipWork):
rd.ulReserved = 0
rd.ulRecipClass = MAPI_TO
rd.lpszName = None
rd.lpszAddress = ra
rd.ulEIDSize = 0
rd.lpEntryID = None
recip = rda

# send the message
msg = MapiMessage(0, subject, body, None, None, None, 0,
cast(NULL, lpMapiRecipDesc ), RecipCnt, recip,
nFileCount, lpFiles)
rc = MAPISendMail(0, 0, byref(msg), 0, 0)
if rc != SUCCESS_SUCCESS :
raise WindowsError, "MAPI error %i" % rc

--------------------------------------------------------------------------------

Jul 18 '05 #4

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

Similar topics

1
2574
by: Thomas Heller | last post by:
ctypes 0.9.1 released - Sept 14, 2004 ===================================== Overview ctypes is a ffi (Foreign Function Interface) package for Python 2.3 and higher. ctypes allows to call functions exposed from dlls/shared libraries and has extensive facilities to create, access and manipulate
40
11850
by: ian | last post by:
Hi, I'm a newbie (oh no I can here you say.... another one...) How can I get Python to send emails using the default windows email client (eg outlook express)? I thought I could just do the following import win32com.client
4
7287
by: roni | last post by:
i dont like to use ocx controlx. is there new dll for vb.net that do the job ? or newer code, to send email throught outlook express.
14
9886
by: sridhar | last post by:
iam having user account on an exchangeserver. with that can i send an email using python? if iam using the following code iam getting error fromAddress = 'sridhar_kasturi@satyam.com' toAddress = 'sridhar_kasturi@satyam.com' msg = "Subject: Hello\n\nThis is the body of the message." import smtplib
12
10103
by: p.lavarre | last post by:
Q: The C idea of (pv != NULL) is said most directly in Python ctypes how? A: We are of course supposed to write something like: def c_not_null(pv): return (ctypes.cast(pv, ctypes.c_void_p).value != None) Yes?
3
4227
by: p.lavarre | last post by:
Subject: Python CTypes translation of (pv != NULL) And so then the next related Faq is: Q: How should I test for ((void *) -1)? A: (pv == 0xffffFFFF) works often.
3
2679
by: Chris AtLee | last post by:
Sorry for the repeat post...I'm not sure if my first post (on May 30th) went through or not. I've been trying to write a PAM module using ctypes. In the conversation function (my_conv in the script below), you're passed in a pam_response** pointer. You're supposed to allocate an array of pam_response's and set
0
2550
by: Egor Zindy | last post by:
Egor Zindy wrote: #!/usr/bin/env python """ A generic chipid library based on ctypes This module handles most of the functions in FTChipID.dll
2
1452
by: =?Utf-8?B?U3VzYW4=?= | last post by:
I can't send emails on my Microsoft outlook express 6. HELP?
0
8332
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8851
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
7356
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6179
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5649
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4175
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2750
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 we have to send another system
2
1737
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.