By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,091 Members | 1,546 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,091 IT Pros & Developers. It's quick & easy.

ctypes: Problems using Windows-DLL from VB example code

P: n/a
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
May 9 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Noralf Trønnes schrieb:
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

May 9 '07 #2

P: n/a
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.
May 9 '07 #3

P: n/a
>Have you tried to pass the structure by reference?
>>
dso_lib.port_init(byref(init_data))

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

May 9 '07 #4

P: n/a
Thank you very much!

That did the trick.

Noralf.
May 9 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.