Error when loading .dll from a different directory using python ctypes.CDLL()

后端 未结 1 469
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-14 09:41

I have to following directory structure:

MainProject  
    |    ...project files  
    |    rtlsdr\\  
    |    |    rtlsdr.dll
    |    |    ...other .dll         


        
相关标签:
1条回答
  • 2021-01-14 09:57

    A DLL may have other DLL dependencies that are not in the working directory or the system path. Therefore the system has no way of finding those dependencies if not specified explicitly. The best way I found is to add the location of the directory containing dependencies to the system path:

    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')
    

    Once the current session is closed, the PATH variable will return to its original state.

    Another option is to change working directory, but that might affect other module imports:

    import os
    os.chdir(abs_path_to_rtlsdr)
    # load dll etc...
    
    0 讨论(0)
提交回复
热议问题