WindowsError: exception: access violation writing 0x00000000

Philip Semanchuk philip at semanchuk.com
Tue Aug 4 11:47:31 EDT 2009


On Aug 4, 2009, at 11:25 AM, Sparky wrote:

> On Aug 3, 3:29 pm, Sparky <Samnspa... at gmail.com> wrote:
>> Hello! I am using cTypes on Windows to interface with a dll and I  
>> keep
>> getting an error when I execute this method:
>>
>> def eDigitalIn(self, channel, idNum = None, demo = 0, readD=0):
>>         """
>>         Name: U12.eAnalogIn(channel, idNum = None, demo = 0, readD=0)
>>         Args: See section 4.4 of the User's Guide
>>         Desc: This is a simplified version of Counter. Reads & resets
>> the counter (CNT).
>>         """
>>
>>         if idNum is None:
>>             idNum = self.id
>>
>>         ljid = ctypes.c_long(idNum)
>>         state = ctypes.c_long(999)
>>
>>         ecode = staticLib.ECount(ctypes.byref(ljid), demo, channel,
>> readD, ctypes.byref(state))
>>
>>         if ecode != 0: raise LabJackException(ecode)
>>         if ljid == -1: raise LabJackException(-1, "LabJack not
>> found.")
>>
>>         return {"idnum":ljid.value, "state":state.value}
>>
>> Here is the error message:
>> Traceback (most recent call last):
>>   File "<pyshell#4>", line 1, in <module>
>>     device.eDigitalIn(0)
>>   File "C:\Documents and Settings\All Users\Documents\Python
>> \LabJackPython_new\u12.py", line 118, in eDigitalIn
>>     ecode = staticLib.ECount(ctypes.byref(ljid), demo, channel,  
>> readD,
>> ctypes.byref(state))
>> WindowsError: exception: access violation writing 0x00000000
>>
>> Here is the signature of the method (which is known to work with C++
>> programs):
>>
>> long _stdcall EDigitalIn(long *idnum,
>>                                            long demo,
>>                                            long channel,
>>                                            long readD,
>>                                            long *state);
>>
>> staticLib is declared with staticLib = ctypes.windll.LoadLibrary
>> ("ljackuw").
>>
>> Any ideas?
>>
>> Thanks,
>> Sam
>
> One more thing, I seem to be getting back incorrect values for doubles
> passed by reference. Any suggestions? I am on a 64-bit machine but I
> should think that should not make a difference.

Hi Sam,
ctypes is pretty straightforward, and I wouldn't expect it to break on  
something simple. I looked over your code (warning -- I am a ctypes  
novice) and it looks OK to me. If you're getting null pointer writes  
I'd suspect that the call signature isn't what you think it is,  
perhaps due to a lack of an extern "C" declaration or something like  
that. In short, I don't think this is a problem in your Python code or  
in ctypes.

But since your Python code is the easiest to change, why not do a  
quick test of a minimal example?

def eDigitalIn_test_version(self, channel, idNum = None, demo = 0,  
readD=0):
         ljid = ctypes.c_long(42)
         state = ctypes.c_long(999)

         ecode = staticLib.ECount(ctypes.byref(ljid), 0, 0, 0,  
ctypes.byref(state))

         print ecode


Does that work any better?

You could also try replacing the byref() calls with pointers created  
by ctypes.pointer(). But I suspect that all this will do is give you  
confidence that it isn't your Python code that's wrong.

You might want to write a minimal C program that invokes ECount. That  
would help you to prove whether or not you have a working C (not C++!)  
interface for your function.

HTH
Philip







More information about the Python-list mailing list