python+ctypes fails to find my dll

13664
4
08-23-2011 07:53 AM
CrispinCooper
New Contributor II
Hi

I have a python script which calls a dll, a la http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001500000013000000.htm

It worked fine when the path to the dll was hardcoded:

ctypes.windll.LoadLibrary(u'c:\absolute\path\to\my.dll')

but now I'd like to distribute the dll to other people, I don't know the absolute path.  It will however be in the same folder as the python script.

But neither of these work:

ctypes.windll.LoadLibrary(u'my.dll')
ctypws.windll.my

...both return the error "The specified module could not be found".

Does anyone know how to solve this?

Thanks in advance!
Tags (2)
0 Kudos
4 Replies
DanPatterson_Retired
MVP Emeritus
Would this work?
  
import sys, os.path
scriptName = sys.argv[0]
pathName = os.path.dirname(scriptName)
dllFile = "mydll.dll"
locatedAt = (pathName + "/" + dllFile).replace("\\","/")
print "script name", scriptName
print "path to script", pathName
print "dll location", locatedAt
0 Kudos
JasonScheirer
Occasional Contributor III
ctypes.windll.LoadLibrary(r'c:\absolute\path\to\my.dll')


Need that 'r' in front of the string.
0 Kudos
DanPatterson_Retired
MVP Emeritus
Jason
Doesn't help with differing path names.
0 Kudos
CrispinCooper
New Contributor II
Would this work?


Yes.  Excellent suggestion, thank you. 🙂

dirname = os.path.dirname(sys.argv[0])
dll = ctypes.windll.LoadLibrary(dirname+"\\my.dll")
0 Kudos