ctypes: Problems using Windows-DLL from VB example code

  • Thread starter Noralf Trønnes
  • Start date
N

Noralf Trønnes

Hi

I'm trying to use a DLL from Python using ctypes.
The call to dso_lib.capture_hold_chk() does return 232. So it seem to work.
The call to dso_lib.port_init(init_data) gives:
WindowsError: exception: access violation reading 0x00000006

Any suggestions?

Thanks.

Noralf


Here is my python code:
import os
from ctypes import *

# Hardware control data
class A_INIT(Structure):
_fields_ = [("time_d_va", c_long),
("tri_in_sel", c_int),
("ch1_div", c_int),
("ch1_in_sel", c_int),
("ch2_div", c_int),
("ch2_in_sel", c_int),
("ram_rw_mode", c_int),
("ram_copy_mode", c_int),
("ram_copy_delay", c_int),
("ho_mode", c_int),
("ho_mode1", c_int),
("CH", c_int),
("TRI_12E", c_int),
("Ch1_To_Gnd", c_int),
("Ch2_To_Gnd", c_int)]

dso_lib_filepath = os.path.join(os.path.dirname(__file__),
'dso_2100usb_s.dll')
dso_lib = cdll.LoadLibrary(dso_lib_filepath)
init_data = A_INIT()

init_data.time_d_va = 0
init_data.tri_in_sel = 7
init_data.ch1_div = 4
init_data.ch1_in_sel = 1
init_data.ch2_div = 4
init_data.ch2_in_sel = 1
init_data.ram_rw_mode = 1
init_data.ram_copy_mode = 14
init_data.ram_copy_delay = 0
init_data.ho_mode = 1
init_data.ho_mode1 = 0
init_data.CH = 0
init_data.TRI_12E = 0
init_data.Ch1_To_Gnd = 0
init_data.Ch2_To_Gnd = 0

print dso_lib.capture_hold_chk() # returns 232

print dso_lib.port_init(init_data) # WindowsError exception


Here is the code from a Visual Basic example using the DLL:

Type A_INIT 'hardware control data
time_d_va As Long
tri_in_sel As Integer
ch1_div As Integer
ch1_in_sel As Integer
ch2_div As Integer
ch2_in_sel As Integer
ram_rw_mode As Integer
ram_copy_mode As Integer
ram_copy_delay As Integer
ho_mode As Integer
ho_mode1 As Integer
CH As Integer
TRI_12E As Integer
Ch1_To_Gnd As Integer
Ch2_To_Gnd As Integer
End Type
Public init_data As A_INIT

Public RESULT As Integer

Public Declare Function port_init Lib "dso_2100usb_s.dll" (init_data As
A_INIT) As Integer

Sub init()
init_data.ch1_div = 4
init_data.ch1_in_sel = 1
init_data.Ch1_To_Gnd = 0
init_data.ch2_div = 4
init_data.ch2_in_sel = 1
init_data.Ch2_To_Gnd = 0
init_data.ho_mode = 1
RESULT = port_init(init_data)
End Sub
 
T

Thomas Heller

Noralf said:
Hi

I'm trying to use a DLL from Python using ctypes.
The call to dso_lib.capture_hold_chk() does return 232. So it seem to work.
The call to dso_lib.port_init(init_data) gives:
WindowsError: exception: access violation reading 0x00000006

Any suggestions?

Have you tried to pass the structure by reference?

dso_lib.port_init(byref(init_data))

Thomas
 
N

Noralf Trønnes

Have you tried to pass the structure by reference?
dso_lib.port_init(byref(init_data))

Thomas

That gives me another exeption:

print dso_lib.port_init(byref(init_data))
ValueError: Procedure called with not enough arguments (4 bytes missing) or
wrong calling convention

Noralf.
 
T

Thomas Heller

Have you tried to pass the structure by reference?
That gives me another exeption:

print dso_lib.port_init(byref(init_data))
ValueError: Procedure called with not enough arguments (4 bytes missing) or
wrong calling convention

Please try using 'windll' instead of 'cdll' to load the library; then call 'dso_lib.port_init(byref(init_data))'.

Thomas
 

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

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top