Python:导入dll时出现访问冲突错误

时间:2013-12-12 14:30:32

标签: python dll import ctypes access-violation

我需要与某些硬件进行通信。我想将它加载到python。为了包装dll,我写了一个类来处理它。减少但工作:OOspec.py

# file OOSpec.py

from ctypes import *
import os

# types definitions
c_double_p = POINTER(c_double)
c_OOSpec_p = POINTER(c_ulong) # to ma byt proste pointer. 
                              # Pointer by snad mel mit rozsah unsigned int. 
c_int_p = POINTER(c_int)

class Device(Structure):        
    _fields_ = [("spectro", c_OOSpec_p), 
                ("smoothLen", c_int), 
                ("winStart", c_int), 
                ("winEnd", c_int), 
                ("wvlns", c_double_p), 
                ("Dwvlns", c_double_p), 
                ("spectrum", c_double_p), 
                ("SNspectrum", c_double_p), 
                ("NDspectrum", c_double_p), 
                ("specLen", c_int)]

DEV = POINTER(Device)

class OOSpec:

    def __init__(self):
        # library load
        self.Bandidll = CDLL("BanditTDll.dll")
        # initialisation of return variables
        OOBandit_init = self.Bandidll.OOBand_init
        OOBandit_init.restype = DEV
        # instrument initialization
        self.device = OOBandit_init()

    def closeDevice(self):
        self.Bandidll.OOBand_destroy(self.device)

def whatever():
    A = OOSpec()
    A.closeDevice()

#whatever()

函数whatever用于测试。如果取消注释,脚本将无错误地执行。

现在,如果我将此类导入其他脚本并尝试相同,则dll返回“访问违规写入”

这是尝试加载它:

from OOSpec.OOSpec import *

device = OOSpec()

执行构造函数会触发错误。我已经检查了DLL内部。除非dll想要写入在调用期间分配的内存,否则所有内容都会被称为normaly。

任何帮助将不胜感激,我不明白为什么在没有错误的情况下从一个文件调用相同的代码,但是从另一个文件触发错误。我甚至不确定在哪里寻找问题的根源。

Windows 7,64位,Python 3.2。在MSVC ++ 2008 ExpressEddition中编写和测试的DLL

0 个答案:

没有答案