2016-02-15 78 views
1

不同的目錄.dll文件加載時,我有以下目錄結構:錯誤從使用python ctypes.CDLL()

MainProject 
    | ...project files 
    | rtlsdr\ 
    | | rtlsdr.dll 
    | | ...other .dll's etc. 

我使用的功能CDLL()ctypes的到加載rtlsdr.dll。它正常工作時,我的工作目錄是rtlsdr\

$ cd rtlsdr 
$ python 
> from ctypes import * 
> d = CDLL('rtlsdr.dll') 

然而,當我嘗試從另一個目錄中加載文件:

$ cd MainProject 
$ python 
> from ctypes import * 
> d = CDLL('rtlsdr\\rtlsdr.dll') 

我得到一個錯誤:

WindowsError: [Error 126] The specified module could not be found. 

什麼這裏是問題嗎?

+0

可能的複製|訪問DLL使用ctypes](http://stackoverflow.com/questions/7586504/python-accessing-dll-using-ctypes) – eryksun

回答

2

A DLL可能有其他DLL依賴項不在工作目錄或系統路徑中。因此,如果沒有明確指定,系統無法找到這些依賴關係。我發現最好的方法是添加含有依賴於系統路徑的目錄位置:

import os 
from ctypes import * 
abs_path_to_rtlsdr = 'C:\\something\\...\\rtlsdr' 
os.environ['PATH'] = abs_path_to_rtlsdr + os.pathsep + os.environ['PATH'] 
d = CDLL('rtlsdr.dll') 

一旦當前會話關閉時,PATH變量將恢復到原來的狀態。

另一種選擇是改變工作目錄,但可能會影響其他模塊導入:

import os 
os.chdir(abs_path_to_rtlsdr) 
# load dll etc... 
[Python中的