Using ctypes with jython

非 Y 不嫁゛ 提交于 2019-12-04 04:00:19

问题


I have a trouble with using ctypes lib in my python script. Here is my code (found on the Internet):

if __name__ == "__main__":
    from ctypes import *
    user32 = windll.user32
    kernel32 = windll.kernel32

    class RECT(Structure):
        _fields_ = [
            ("left", c_ulong),
            ("top", c_ulong),
            ("right", c_ulong),
            ("bottom", c_ulong)];

    class GUITHREADINFO(Structure):
        _fields_ = [
        ("cbSize", c_ulong),
        ("flags", c_ulong),
        ("hwndActive", c_ulong),
        ("hwndFocus", c_ulong),
        ("hwndCapture", c_ulong),
        ("hwndMenuOwner", c_ulong),
        ("hwndMoveSize", c_ulong),
        ("hwndCaret", c_ulong),
        ("rcCaret", RECT)
        ]

    def moveCursorInCurrentWindow(x, y):
        # Find the focussed window.
        guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
        user32.GetGUIThreadInfo(0, byref(guiThreadInfo))
        focussedWindow = guiThreadInfo.hwndFocus

        # Find the screen position of the window.
        windowRect = RECT()
        user32.GetWindowRect(focussedWindow, byref(windowRect))

        # Finally, move the cursor relative to the window.
        user32.SetCursorPos(windowRect.left + x, windowRect.top + y)

    if __name__ == '__main__':
    # Quick test.
        moveCursorInCurrentWindow(100, 100)

The first problem was that python couldn't find the ctypes so i copied the files downloaded from the project site to

netbeans\6.9\jython-2.5.1\Lib\

(yep, im using netbeans) and then it shows this error:

>    from ctypes import *
>  File "C:\Users\k\.netbeans\6.9\jython-2.5.1\Lib\ctypes\__init__.py", line 10, in <module>
>    from _ctypes import Union, Structure, Array

Just like the init file has some errors o_O Help guys! Greetings, Chris


回答1:


Jython doesn't yet have full support for ctypes: http://bugs.jython.org/issue1328

You can't simply take the ctypes library compiled for CPython, and plug it into Jython.




回答2:


ctypes in Jython experimental and not complete.

From the jython-users mailing list in a thread titled "ctypes in Jython" Jim Baker (a Jython committer) wrote on November 17, 2010:

There's some experimental support for ctypes in 2.5.2 [the current version], but it's really more of a placeholder at this point.

He then suggests these work arounds:

I do recommend JNA if you can modify your ctypes code. JNA is pretty close to ctypes - JNA's API apparently was significantly influenced by ctypes! JNA also seems to work well with Jython.

The other option is to use something like execnet. For execnet specifically: it allows you to pair Jython with CPython, and it does seem to work well. But its GPL license makes it a non starter for many people. There are other choices out there too.

Further on in the same thread we have this confirming assessment:

I tried the ctypes module in 2.5.2rc2 recently, and found that: 1) There's no ctypes.util.find_library yet 2) ctypes.Structure doesn't support non-scalar types yet

So I agree with the "more of a placeholder" assessment. Still, it's exciting to see it getting started.




回答3:


ctypes is not supported in Jython 2.5.1. There has been some experimental support added in 2.5.2, but it's certainly nowhere near complete. Maybe you'll have better luck using JNA with Jython instead. There's a short tutorial here.




回答4:


Ok, thx dudes! I just reconfig my NetBeans, now its using cPython. Everything works. I just had to changed the line user32.SetCursorPos(windowRect.left + x, windowRect.top + y) to: user32.SetCursorPos(c_ulong(windowRect.left + x), c_ulong(windowRect.left + y))



来源:https://stackoverflow.com/questions/5186670/using-ctypes-with-jython

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!