Re: [ctypes-users] Calling a DLL from python like I would from VB
Brought to you by:
theller
From: Grzegorz A. H. <gha...@ra...> - 2007-04-11 14:39:20
|
Mark McMahon wrote: > Hi Grzegorz, > > Look at http://osdir.com/ml/python.ctypes/2006-03/msg00024.html for how to access functions by ordinal. > > And it is even referenced in the ctypes tutorial http://www.python.net/crew/theller/ctypes/tutorial.html. > > Getting a string from the returned pointer might be a little more difficult... > http://www.python.net/crew/theller/ctypes/tutorial.html#return-types > > Though it might be as easy as... [...] Unfortunately I'm already using that code, albeit a less clear version. > # get function with ordingal 2 > GetVersion = ctypes.windll.libdll[2] This is the problem: while VB sees the GetVersion function as the second ordinal, python doesn't! When I run this code: print dll count = 1 for f in range(10): try: func = dll[f] print "%d, ordinal %d: %s" % (f, count, func) count += 1 except AttributeError: pass dll[6].restype = ctypes.wintypes.LPCSTR print "GetDllVersion: '%s'" % (dll[6]()) I get: <WinDLL 'C:\temp\RTHKPa.dll', handle 10000000 at b45730> 4, ordinal 1: <_FuncPtr object at 0x00ADDBE8> 6, ordinal 2: <_FuncPtr object at 0x00ADDC60> 7, ordinal 3: <_FuncPtr object at 0x00ADDCD8> GetDllVersion: '268878736' If I try to access dll[2] that raises an AttributeError. The restype thing you suggested seems to change the contents of the pointer though, now it's not any more a weird string, but a weird number. ? > Things to try if that doesn't work are: > - Try different restypes I tried c_char_p, c_wchar_p and wintypes.LPCSTR with same result. > - Try cdll instead of windll They happen to do the same stuff. -- Rastertech España S.A. Grzegorz Adam Hankiewicz /Jefe de Producto TeraVial/ C/ Perfumería 21. Nave I. Polígono industrial La Mina 28770 Colmenar Viejo. Madrid (España) Tel. +34 918 467 390 (Ext.18) *·* Fax +34 918 457 889 gha...@ra... *·* www.rastertech.es <http://www.rastertech.es/> |