SirParselot SirParselot - 1 year ago 128
Python Question

Issues using ctypes.WinDLL()

I am trying to get a spectrum analyzer program example working but it is having problems finding the module. Here is the error I'm getting

Traceback (most recent call last):
File "C:\Users\user\Documents\Programs\", line 10, in <module>
rsa300 = ctypes.WinDLL("C:\\Tektronix\\RSA306 API\\lib\\x64\\RSA300API.dll")
File "C:\Python27\lib\ctypes\", line 365, in __init__
self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found

The DLL exists and
with that path so I'm not sure what the problem is. Does anyone know what is wrong with this?

Answer Source

The RSA300API.DLL might have dependencies in the folder so prior to using it, use os.chdir to set the working directory, for example:

import os

os.chdir(r'C:\Tektronix\RSA306 API\lib\x64')
rsa300 = ctypes.WinDLL(r"C:\Tektronix\RSA306 API\lib\x64\RSA300API.dll")

Checking one of their samples, this appears to be the recommended way to access it.

Alternatively, as @eryksub has mentioned, LoadLibraryEx can be used. win32api could be used to get the handle and pass it to WinDLL as follows:

import ctypes
import win32api
import win32con

dll_name = r'C:\Tektronix\RSA306 API\lib\x64\RSA300API.dll'
dll_handle = win32api.LoadLibraryEx(dll_name, 0, win32con.LOAD_WITH_ALTERED_SEARCH_PATH)
rsa300 = ctypes.WinDLL(dll_name, handle=dll_handle)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download