在Postgres PL / Python中加载dll库的正确方法是什么?

时间:2011-11-08 06:39:34

标签: postgresql ctypes plpython

以下是错误

drop function testing();
CREATE FUNCTION testing()
 RETURNS text
AS $$
import ctypes
try:
   ctypes.windll.LoadLibrary("D:\\jcc.dll")
except:
   import traceback
   plpy.error(traceback.format_exc())
return ''
$$ LANGUAGE plpythonu;
select testing();

错误讯息:

ERROR:  ('Traceback (most recent call last):\n  File "<string>", line 5, in __plpython_procedure_testing_1517640\n  File "D:\\Python26\\Lib\\ctypes\\__init__.py", line 431, in LoadLibrary\n    return self._dlltype(name)\n  File "D:\\Python26\\Lib\\ctypes\\__init__.py", line 353, in __init__\n    self._handle = _dlopen(self._name, mode)\nWindowsError: [Error 126] The specified module could not be found\n',)

它在python解释器中工作正常。

Python 2.6.4 (r264:75708, Oct 26 2009, 08:23:19) [MSC v.1500 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import ctypes
>>> ctypes.windll.LoadLibrary("D:\\jcc.dll")
<WinDLL 'D:\jcc.dll', handle 410000 at 1d9cb10>
>>>

1 个答案:

答案 0 :(得分:1)

“无法找到指定的模块”是Windows发出的有用错误消息之一,并不总是意味着您的意思。

如果您尝试加载的DLL或任何依赖于的DLL,则Windows将生成该消息。

由于PostgreSQL在自己的用户帐户中运行,因此它与您的解释器在测试时运行的路径不同。如果jcc.dll依赖于(例如)c:\jccsupportfiles\aaa.dll并且c:\jccsupportfiles位于您的PATH而不是Pg服务器的PATH,则可以解释您的问题。

尝试使用Dependency Walker (depends.exe)来确定DLL需要哪些DLL以及它们所在的位置。看看它是否是PATH问题。

考虑将jcc.dll所需的所有DLL放在与jcc.dll相同的目录中,而不是弄乱Pg服务器的PATH。 IIRC当Windows尝试加载它所依赖的模块时,它将始终与它首先加载的模块位于同一目录中。

相关问题