Vusal Aliyev
Vusal Aliyev

Reputation: 75

Python CDLL cannot load library 2 times

Sorry my English is very bad. So. I write simple Dynamic Link Library in Dev c++. Its working nice.Today I import this is library in my python project. Dynamic Link Library File Path:lib/my_dll.dll #Dialog box config.py

DLL_PATH = "lib/my_dll.dll"

main.py(config.py)imported

def my_func():
   dll = CDLL(DLL_PATH)
   return dll.func1()

So i open python interpreter and write this.

from main import *
a = my_func()    #Its work nice so a == "c:\\Windows\\a.txt"

but i reuse this function python generate next error

OSErrror:[WinError 126] The specified module could not be found

Thank you for reading!

Upvotes: 1

Views: 1440

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

In the comments you state that your DLL function call an API function that shows a file selection dialog. File selection dialogs, unless you tell them not to, can change the working directory. Since you failed to specify a full path to the DLL, the DLL search is performed each time. The first time it succeeds because your working directory happens to be what is needed for the DLL to be found. Subsequent times the DLL search fails because your working directory has changed.

Some advice:

  1. Always use either just a file name, or a full absolute path when loading a DLL. In your case I suspect that you need to use the latter.
  2. You load the DLL each time your function is called. Loading it once only will suffice. Don't waste time by loading it again and again.
  3. It might be easier to show the file dialog directly from Python and avoid having to create a DLL for just that purpose.
  4. Your ctypes function import doesn't specify the restype and so I don't see how you can obtain text from that function. What's more, I don't see how you can avoid leaking memory each time you call the function. Unless the text lives in a static array.

Upvotes: 1

Related Questions