如何在Windows的Python3.8.10中调用xsensdeviceapi64.dll的类与函数?
os.add_dll_directory() only adds the target directory to Windows' DLL search path—it does not load the DLL into Python or expose its classes/functions. The returned _AddedDllDirectory object is just a handle to manage the added path, which is why accessing XsCallback from it throws an AttributeError. Additionally, Xsens' DLLs are native C/C++ libraries, not Python extension modules (.pyd), so renaming them won't make Python recognize their symbols.
Option 1: Wrap the C API with ctypes
Since the Xsens Device API provides a C interface, use Python's built-in ctypes module to load and interact with the DLLs directly:
Load the required DLLs:
First add the DLL directory to the search path, then load the dependencies and main API DLL:import os import ctypes dll_path = r'C:\path\to\xsens\dlls' os.add_dll_directory(dll_path) # Load dependencies first (XsTypes is required by Xsens Device API) xstypes = ctypes.CDLL('xstypes64.dll') xda = ctypes.CDLL('xsensdeviceapi64.dll')Define type signatures:
Map C types to Python types and declare function prototypes to ensure correct parameter passing. For example, to work withXsCallback, you'll need to define the callback function type (refer to Xsens' C API docs for exact definitions):# Example: Define a callback function matching Xsens' signature XsCallbackHandler = ctypes.CFUNCTYPE(ctypes.c_void_p, ctypes.c_void_p, ctypes.c_int) # Declare a function from the DLL with correct argument/return types xda.XsCallback_create.argtypes = [] xda.XsCallback_create.restype = ctypes.c_void_pImplement and register callbacks:
Write a Python function that matches the callback signature, wrap it with the defined type, and pass it to the Xsens API functions.
Option 2: Build a C++ Wrapper with pybind11
If you want to use the C++ interface (which handles memory management automatically), create a custom Python extension using pybind11:
- Grab the Xsens SDK source code (including the C++ wrapper headers).
- Set up a pybind11 project that links against
xstypes64.dllandxsensdeviceapi64.dll. - Write pybind11 bindings to expose
XsCallbackand other API components to Python. - Compile the project into a
.pydfile that you can import directly in Python.
Option 3: Check for Official Windows Wheel
Double-check the Xsens developer portal for a Windows-specific Python wheel. While you have a working Linux wheel, vendors sometimes release Windows versions separately or can provide one upon request.
- Ensure bitness matches: 64-bit DLLs must be used with 64-bit Python 3.8.10 (you're already using the 64-bit DLLs, which is correct).
- The C API is the most direct path for Windows; the C++ wrapper requires compilation since it's provided as source code.
- Always refer to the Xsens Device API documentation for exact function signatures, struct definitions, and callback protocols—this is critical for correct wrapping.
内容的提问来源于stack exchange,提问作者jvkloc

