çªç¶ã§ããããã¤ãã®ããã« ã¼ãã£ã¨ ãããã¯ã¼ã¯ã ãµããµã ãè¿·ã£ã¦ããã¨ãé¢ç½ãç©ãè¦ã¤ãã¾ããã
- README: ctypes code generation
http://starship.python.net/crew/theller/ctypes/codegen.html
ctypes ã¯ã.DLLã.soãªã©ã®å
±æã©ã¤ãã©ãªããpythonããåçã«å¼ã³åºãã©ã¤ãã©ãªãªãã§ããããã®å¼ã³åºãã³ã¼ããçæããã³ã¼ãã®ããã§ãã
- h2xml.py ã§ãããããã¡ã¤ã«ãXMLã«å¤æã
- xml2py.py ã§ãXMLãã ctypesç¨ã®å¼ã³åºãã³ã¼ããçæãã
ã¨ããæé ãããã§ãã
èå³ã湧ããã®ã§ãæ©é使ã£ã¦ã¿ã¾ããã
ã¤ã³ã¹ãã¼ã«
è±æãªã®ã§ãé©å½ã«Web翻訳ã«ãããã¨ãGCC-XMLãçªã£è¾¼ãã¨ãæ¸ãã¦ããããã§ãã
ãªã®ã§ã以ä¸ã®ãã¡ã¤ã«ããã¦ã³ãã¼ããã¦ãå
¥ãã¾ããã
- ctypes-0.9.9.6.win32-py2.4.exe
- gccxml-20050318-setup.exe
ä½è«ã§ããããã¹ãããç°å¢ã¯ãPython 2.4.2 for Windowsã§ããã¯ãã
å¾ãå©ç¨ã«ããã£ã¦ãVisual C++ãå
¥ã£ã¦ããå¿
è¦ãããã£ã½ãã§ããâ¦æ¬å½ããªï¼ Core SDK ãçªã£è¾¼ãã°OKã®ãããªæ°ãããªãã§ã¯ãªãã§ããã誰ã試ãã¦ä¸ããã¾ãã
h2xml.py ã§ãXMLãã¡ã¤ã«ãä½ã
h2xml.pyã¯ãPythonã®ã¤ã³ã¹ãã¼ã«ãã©ã«ãããã'Lib\site-packages\ctypes\wrap' ã®å ´æã«ããã¾ãã
ãµã³ãã«ã«å¾ã£ã¦ãã¾ã㯠windows.h ã XMLåãã¦ã¿ã¾ãããã
F:\Wacky\Test\python\test>python C:\tool2\Python24\Lib\site-packages\ctypes\wrap \h2xml.py "C:\Program Files\Microsoft Visual Studio\VC98\Include\WINDOWS.H" -o w indows.xml -q -c Warning: Compiler "cl" specified, but more than one of MSVC 6, 7, and 7.1 are installed. Please specify "msvc6", "msvc7", or "msvc71" for the GCCXML_COMPILER setting. Using MSVC 6 because it was used to build GCC-XML. Warning: Compiler "cl" specified, but more than one of MSVC 6, 7, and 7.1 are installed. Please specify "msvc6", "msvc7", or "msvc71" for the GCCXML_COMPILER setting. Using MSVC 6 because it was used to build GCC-XML. skipped #define SNDMSG ::SendMessage FunctionType
ãã£ããªããwindows.xmlãã§ããããã¾ãã
ã¡ãªã¿ã«ãä¸ã§ warning message ãåºã¦ãã¾ããã'set GCCXML_COMPILER=msvc6'ã¨ãããããã£ãã³ãã³ããå©ãã°ãä½ã®æå¥ãè¨ãããªããªãã¾ãã
xml2py.py ã§ãctypesã®å¼ã³åºãã³ã¼ããä½æããã
XMLãã¡ã¤ã«ãåºæ¥ãã®ã§ãæ©é ctypesã³ã¼ããä½æããã¦ã¿ã¾ãããã
ã¾ãã¯ããµã³ãã«ã«å¾ã£ã¦ã
F:\Wacky\Test\python\test>python C:\tool2\Python24\Lib\site-packages\ctypes\wrap \xml2py.py windows.xml -s RECT # generated by 'xml2py' # flags 'windows.xml -s RECT' from ctypes import * # C:/PROGRA~1/MICROS~3/VC98/Include/windef.h 287 class tagRECT(Structure): pass RECT = tagRECT LONG = c_long tagRECT._fields_ = [ # C:/PROGRA~1/MICROS~3/VC98/Include/windef.h 287 ('left', LONG), ('top', LONG), ('right', LONG), ('bottom', LONG), ] assert sizeof(tagRECT) == 16, sizeof(tagRECT) assert alignment(tagRECT) == 4, alignment(tagRECT)
ãµã³ãã«ã¨åãé°å²æ°ã®ã³ã¼ããåºæ¥ä¸ããã¾ããã
次ãä½ãé©å½ã«å¼ã³åºãã³ã¼ããä½ã£ã¦ãåããã¦ã¿ã¾ãããã
ãã¨ãMessageBox é¢æ°ããã¾ã試ãã¾ãã
F:\Wacky\Test\python\test>python C:\tool2\Python24\Lib\site-packages\ctypes\wrap \xml2py.py windows.xml -w -s MessageBox -d # generated by 'xml2py' # flags 'windows.xml -w -s MessageBox -d' from ctypes import * # C:/PROGRA~1/MICROS~3/VC98/Include/windef.h 195 class HWND__(Structure): pass CHAR = c_char @ stdcall(c_int, 'user32', [POINTER(HWND__), POINTER(CHAR), POINTER(CHAR), c_uin t]) def MessageBoxA(p1, p2, p3, p4): # C:/PROGRA~1/MICROS~3/VC98/Include/winuser.h 6140 return MessageBoxA._api_(p1, p2, p3, p4) MessageBox = MessageBoxA # alias HWND__._fields_ = [ # C:/PROGRA~1/MICROS~3/VC98/Include/windef.h 195 ('unused', c_int), ] assert sizeof(HWND__) == 4, sizeof(HWND__) assert alignment(HWND__) == 4, alignment(HWND__)
åãåºããã³ã¼ãããtest.pyã«ãã¦ãã¾ãã¾ãã
test.py:
# generated by 'xml2py' # flags 'windows.xml -w -s MessageBox -d' from ctypes import * # C:/PROGRA~1/MICROS~3/VC98/Include/windef.h 195 class HWND__(Structure): pass CHAR = c_char @ stdcall(c_int, 'user32', [POINTER(HWND__), POINTER(CHAR), POINTER(CHAR), c_uint]) def MessageBoxA(p1, p2, p3, p4): # C:/PROGRA~1/MICROS~3/VC98/Include/winuser.h 6140 return MessageBoxA._api_(p1, p2, p3, p4) MessageBox = MessageBoxA # alias HWND__._fields_ = [ # C:/PROGRA~1/MICROS~3/VC98/Include/windef.h 195 ('unused', c_int), ] assert sizeof(HWND__) == 4, sizeof(HWND__) assert alignment(HWND__) == 4, alignment(HWND__)
æ©éåããã¦ã¿ã¾ãããã
F:\Wacky\Test\python\test>ipython F:\Wacky\Test\python\test>python C:\tool2\Python24\Scripts\ipython WARNING: Could not import user config! ('C:\Documents and Settings\user\_ipython/ipy_user_conf.py' does not exist? Please run '%upgrade') Python 2.4.2 (#67, Sep 28 2005, 12:41:11) [MSC v.1310 32 bit (Intel)] Type "copyright", "credits" or "license" for more information. IPython 0.7.2 -- An enhanced Interactive Python. ? -> Introduction to IPython's features. %magic -> Information about IPython's 'magic' % functions. help -> Python's own help system. object? -> Details about 'object'. ?object also works, ?? prints more. In [1]: import test --------------------------------------------------------------------------- exceptions.NameError Traceback (most recent call last) F:\Wacky\Test\python\test\<ipython console> F:\Wacky\Test\python\test\test.py 7 CHAR = c_char 8 ----> 9 @ stdcall(c_int, 'user32', [POINTER(HWND__), POINTER(CHAR), POINTER(CHAR ), c_uint]) 10 def MessageBoxA(p1, p2, p3, p4): 11 # C:/PROGRA~1/MICROS~3/VC98/Include/winuser.h 6140 NameError: name 'stdcall' is not defined
ãã¼ãã¾ãããã£ï¼
ã³ã¼ãããå¼ã³åºããããã«ä¿®æ£ãã
ä½ã§åããããã ããã¨æã£ã¦ããµã³ãã«ãçºãã¦ããã¨ãã¨ã©ã¼ç®æã®æããçæããã³ã¼ãã¨ãµã³ãã«ã§ã¯éããã§ããã
- ã¨ã©ã¼ãèµ·ããå ´åï¼@ stdcall(...)
- ãµã³ãã«ã®å ´åï¼@ decorator.stdcall(...)
å¾ãå é ã«'from ctypes import decorators'ãä»å
ãªãã»ã©ããã¨æã£ã¦ãã³ã¼ãã«ä¿®æ£ãå ãã¾ãã
test.py:
# generated by 'xml2py' # flags 'windows.xml -w -s MessageBox -d' from ctypes import * from ctypes import decorators # C:/PROGRA~1/MICROS~3/VC98/Include/windef.h 195 class HWND__(Structure): pass CHAR = c_char @ decorators.stdcall(c_int, 'user32', [POINTER(HWND__), POINTER(CHAR), POINTER(CHAR), c_uint]) def MessageBoxA(p1, p2, p3, p4): # C:/PROGRA~1/MICROS~3/VC98/Include/winuser.h 6140 return MessageBoxA._api_(p1, p2, p3, p4) MessageBox = MessageBoxA # alias HWND__._fields_ = [ # C:/PROGRA~1/MICROS~3/VC98/Include/windef.h 195 ('unused', c_int), ] assert sizeof(HWND__) == 4, sizeof(HWND__) assert alignment(HWND__) == 4, alignment(HWND__)
æ©éå®è¡ã
F:\Wacky\Test\python\test>python C:\tool2\Python24\Scripts\ipython ... In [1]: import test --------------------------------------------------------------------------- exceptions.TypeError Traceback (most recent call last) F:\Wacky\Test\python\test\<ipython console> F:\Wacky\Test\python\test\test.py 8 CHAR = c_char 9 ---> 10 @ decorators.stdcall(c_int, 'user32', [POINTER(HWND__), POINTER(CHAR), POINTER(CHAR), c_uint]) 11 def MessageBoxA(p1, p2, p3, p4): 12 # C:/PROGRA~1/MICROS~3/VC98/Include/winuser.h 6140 C:\tool2\Python24\lib\site-packages\ctypes\decorators.py in decorate(func) 96 else: 97 this_dll = dll ---> 98 api = ctypes.WINFUNCTYPE(restype, *argtypes)(func.func_name, this_dll) 99 # This simple way to find out an empty function body doesn't work. 100 ## if len(func.func_code.co_code) == 4: TypeError: function takes exactly 1 argument (2 given)
ã ãã¸ã£ã³ã
ä½ãæªããã ããã¨æã£ã¦ãæå½ãã次第ããã¼ãããã¦ãä¸ã® decorators.py ã以ä¸ã®ããã«ä¿®æ£ããã°è¯ãäºã«æ°ä»ãã¾ããã
96 else: 97 this_dll = dll 98 api = ctypes.WINFUNCTYPE(restype, *argtypes)(func.func_name, this_dll) â 98 api = ctypes.WINFUNCTYPE(restype, *argtypes)((func.func_name, this_dll)) 99 # This simple way to find out an empty function body doesn't work. 100 ## if len(func.func_code.co_code) == 4:
è¦ããã«ãã¿ãã«ã§æ¸¡ãã°ããããã§ãã
注æäºé
136è¡ã«ããåæ§ã«åãä¿®æ£ãå ãã¦ãã ããã
ã§ã¯ãæ°ãåãç´ãã¦ãå度 å¼ã³åºãã
ctypes posted from ãã©ãèµ
ä»åº¦ã¯ããã¾ãè¡ãã¾ããã
çµè«
æå¾ã«çµè«ã
- h2xml.pyã¨xml2py.pyã使ãã¨ãctypesã使ã£ãå¼ã³åºãã³ã¼ãã®ä½æããé常ã«ç°¡åã«ãªãã¾ãã
- ä½ããdecorators.py ã®ä¿®æ£ãè¡ãå¿
è¦ãããã¾ãã
(2.4.3ã§ã¯å¿ è¦ãªãããç¥ããªããã©ã試ãã¦ã¾ãã)