WindowsError: exception: access violation writing 0x00000000

S

Sparky

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
 
S

Sparky

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.

Thanks again,
Sam
 
P

Philip Semanchuk

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
 
S

Sparky

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

Hey Philip,

Thank you for your response. It turns out I was calling a function
that I was not intending on calling, so it was not a Python problem.

Thanks,
Sam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top