AlexOR
AlexOR

Reputation: 21

ctypes: OSError: exception: access violation reading 0x00000001

I try to communicate with a HV-Supply through a c dll with python. The most simple Function i get to work. But if i call the more complex function CAENHVInitSystem i get an Error: OSError: exception: access violation reading 0x00000001. I am quite new to ctypes in Python. As far as i know, this error maybe shows because some of my arguments have a wrong type. But how i can debug it more to know exactly which argument is wrong? does anyone see my mistake?

Thanks in advance

import os
from ctypes import *

bib = CDLL("CAENHVWrapper")

ret = bib.CAENHVLibSwRel()  # This call works
print(c_char_p(ret)) 

sysType = c_int(1) #SY2527
link = c_int(0) #TCP/IP
#arg = c_char_p(b'149.217.10.241')  #i change it for test to c_void_p but later the arg should be the ip adress
arg = c_void_p()                   
user = c_char_p(b'admin')
passwd = c_char_p(b'admin')
sysHndl = c_int()

# c function definition in the header file
#CAENHVLIB_API CAENHVRESULT CAENHV_InitSystem(
#   CAENHV_SYSTEM_TYPE_t system,
#   int LinkType,
#   void *Arg,
#   const char *UserName,
#   const char *Passwd,
#   int *handle);

# definition of the enum of the first argument
#typedef enum {
#   SY1527      = 0,
#   SY2527      = 1
#} CAENHV_SYSTEM_TYPE_t;

bib.CAENHVInitSystem.argtypes = [c_int, c_int, c_void_p, c_char_p, c_char_p,     POINTER(c_int)]
ret = bib.CAENHVInitSystem(sysType, link, arg, user, passwd, byref(sysHndl))

print(ret)
print(bib.CAENHV_GetError(sysHndl))

Upvotes: 2

Views: 8725

Answers (2)

Jacob Waters
Jacob Waters

Reputation: 347

In my Ctypes setup I got this error when I had multiple instances of the program accidentally run in parallel.

Essentially I forgot to close the old program instances and they were holding on to the DLL file and hence when the newest version tried to access it and was unable to.

Upvotes: 0

David Heffernan
David Heffernan

Reputation: 612794

The first parameter of CAENHVInitSystem is the system name with type const char*. You are mistakenly passing an integer with value 1. When CAENHVInitSystem interprets that as a pointer it tries to read memory at address 1. Hence the error. Change the first argument type to c_char_p, and pass text.

So far as I can tell, the function has 5 parameters rather than 6 so I do believe that you have more errors than just the one identified above.

In future, when asking questions about binary interop, you must supply details of both sides of the interface. I've answered this by doing a websearch for CAENHVInitSystem and hoping that the declaration that I find matches the one you are using. But perhaps that is not the case. You presumably have the true declaration for CAENHVInitSystem and that information is critical. It should be in the question.

Upvotes: 3

Related Questions