Error loading dll in path with parenthesis using c

2019-03-29 19:23发布

问题:

I am trying to access a dll located in the "c:/Program Files (x86)" folder in a 64-bits processor PC.

If I use os.path.exists to check if the dll exists, I receive an afirmative answer:

>>> print os.path.exists('c:/Program Files (x86)/Some Folder/SomeDll.dll')
True

But when I try to load the dll using ctypes, I get the following error:

>>> from ctypes import WinDLL
>>> some_dll = WinDLL('c:/Program Files (x86)/Some Folder/SomeDLL.dll')
Traceback (most recent call last):
  File "<console>", line 1, in <module>
  File "C:\Python26\lib\ctypes\__init__.py", line 353, in __init__
    self._handle = _dlopen(self._name, mode)
WindowsError: [Error 126] The specified module could not be found

In 32-bit PCs the dll is located in the "c:/Program Files" folder and I can open it without problems. I think that perhaps the problem is the presence of parenthesis in the folder name. As the returned exception was a WindowsError, it seems that it is a flaw in the operating system function responsible of loading libraries.

So, the question is: how do I load a dll located in the "c:/Program Files (x86)" folder? I can't copy the dll to another destination, it must be located in the original path...

Thank you!

回答1:

Have you tried "C:/Progra~1/SomeFolder/SomeDll" ?

Another suggestion:

 os.chdir(r"C:\Program Files(x86)\SomeFolder")
 the_dll = WinDLL("SomeDLL.dll")      


回答2:

Sorry I do not have 50 Rep to comment (STUPID Requirement) but /Program Files (x86)/ is just Progra~2 while /Program Files/ is Progra~1 Just in case Felipe Ferri reads this again :) or any one else trying to do the same.
NOTE: This is also assuming you do not have another long folder that starts with Progra, if so this can change your result - you are basically picking from an array of directories in alphabetical order - this works with any folder(s) longer than 8 characters, you take off the last two and add ~1 for first and increment for each other folder with the same set of characters.