Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using ctypes with jython

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

like image 1000
Krzysztof Kaczor Avatar asked Dec 21 '22 17:12

Krzysztof Kaczor


1 Answers

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.

like image 147
Steven Rumbalski Avatar answered Jan 08 '23 19:01

Steven Rumbalski