Updated: python ctype question about "access violation reading location 0x5a5a5a5a"

Yanping Zhang zhan0645 at yahoo.com
Thu Mar 30 02:50:40 CEST 2006

Here are more details about my codes, please help!

The function declared in C:
typedef void (WINAPI *PLEARNCALLBACKPROC) (unsigned int progress, unsigned int sigQuality,
unsigned long carrierFreq, void *userData);

UUIRTDRV_API BOOL PASCAL UUIRTLearnIR(HUUHANDLE hHandle, int codeFormat, char *IRCode,
PLEARNCALLBACKPROC progressProc, void *userData, BOOL *pAbort, unsigned int param1, void
*reserved0, void *reserved1);

My python codes:

import ctypes

port = windll.uuirtdrv

myhandle = c_void_p()
myhandle = port.UUIRTOpen()

#+++++++ Call back function ++++++++
LEARNCALLBACKFUNC = CFUNCTYPE(c_void_p, c_uint, c_uint, c_ulong, c_void_p)

def py_learncallback_func(progress,sigQuality, carrierFreq, userdata):
    print "progress is: %d %d %d %d" % (progress, sigQuality & 0xff, carrierFreq, userdata)
    if progress == 100:
       mybool = c_int(1)

callback_func = LEARNCALLBACKFUNC(py_learncallback_func)


glearnBuffer = create_string_buffer('\000'*2048)

mybool = c_int(0)
param1 = c_int(0)
reserved0 = c_void_p(0)
reserved1 = c_void_p(0)

mydata = create_string_buffer("0x5a5a5a5a")
#userdata = c_long(0x5a5a5a5a)  #failed
#userdata = byref(mydata)       #failed
#userdata = pointer(mydata)     #failed 
userdata = c_void_p(0x5a5a5a5a) #failed

learnformat = c_int(0x0000)

port.UUIRTLearnIR(myhandle, learnformat, glearnBuffer, callback_func, userdata,
mybool,param1,reserved0, reserved1)

I tried to define userdata in different ways and all failed with "access violation
reading(writing) location...". The c routine just passes userdata to call back function and didn't
use it anywhere else.


> Hi All,
> I need to use this C routine in python and there is a void pointer parameter in it: 
> (this routine was written by someone else):
> myfunc(int a, (void *)userdata, int b)
> I saw someone in his C++ wrapper used this routine in this way:
> myfunc(a, (void *)0x5a5a5a5a, b)
> In my python wrapper, I tried  to call it as the following and both failed:
> 1. myfunc(c_int(a), 0x5a5a5a5a, c_int(b))
>    got error "access voilation reading from 0x5a5a5a5a"
> 2. 
>   data = 0x5a5a5a5a
>   mydata = c_void_p(data)
>   myfunc(c_int(a), mydata, c_int(b))
>   same error as in 1
> Can anyone know how to fix it? Thanks!
> __________________________________________________
> Do You Yahoo!?
> Tired of spam?  Yahoo! Mail has the best spam protection around 
> http://mail.yahoo.com 

Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 

More information about the Python-list mailing list