首頁  >  文章  >  後端開發  >  為什麼我無法在 Windows 上的 Python 3.8 中載入 DLL 模組?

為什麼我無法在 Windows 上的 Python 3.8 中載入 DLL 模組?

Barbara Streisand
Barbara Streisand原創
2024-10-28 21:29:02683瀏覽

Why Can't I Load My DLL Module in Python 3.8  on Windows?

在Windows 上用Python 載入DLL 模組時遇到的問題

最近嘗試在Windows 上匯入已編譯的libuvc DLL 時出現錯誤“無法找到模組“libuvc.dll””。儘管使用 ctypes.util.find_library 正確找到了檔案的路徑,但 Python 無法載入它。問題出在 ctypes.cdll.LoadLibrary 函式中的 winmode 參數。

了解 Winmode 參數

在 Python 3.8 之前,winmode 不存在,mode 是直接設定為 ctypes.DEFAULT_MODE,它對應於零值。 Python 3.8 中引入了 winmode。

當 winmode 設定為 None 時,搜尋模式預設為 nt._LOAD_LIBRARY_SEARCH_DEFAULT_DIRS。此搜尋模式不考慮環境變數或路徑的變更。

但是,將 winmode 設為 0 指定應使用完整路徑,且程式庫載入成功。 Microsoft 文件 (https://learn.microsoft.com/en-us/windows/win32/api/libloaderapi/nf-libloaderapi-loadlibraryexa) 中記錄了此行為。

解決問題

要解決載入問題,只需在呼叫ctypes.cdll.LoadLibrary 時指定winmode=0 即可,如下列程式碼片段所示:

import ctypes

name = ctypes.util.find_library('libuvc')
lib = ctypes.cdll.LoadLibrary(name, winmode=0)

以上是為什麼我無法在 Windows 上的 Python 3.8 中載入 DLL 模組?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn