在jython中使用ctypes

Krz*_*zor 4 python ctypes jython

我在python脚本中使用ctypes lib时遇到了麻烦.这是我的代码(在互联网上找到):

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)
Run Code Online (Sandbox Code Playgroud)

第一个问题是python无法找到ctypes,因此我将从项目站点下载的文件复制到

netbeans\6.9\jython-2.5.1\Lib\
Run Code Online (Sandbox Code Playgroud)

(是的,即时通讯使用netbeans)然后它显示此错误:

>    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
Run Code Online (Sandbox Code Playgroud)

就像init文件有一些错误o_O帮助人!问候,克里斯

Ste*_*ski 7

ctypes 在Jython实验中并没有完成.

来自Jython -users邮件列表的标题为" Jython中的ctypes "的Jim Baker(一名Jython提交者)于2010年11月17日写道:

在2.5.2 [当前版本]中对ctypes有一些实验支持,但在这一点上它实际上更像是一个占位符.

然后,他建议这些工作:

如果你可以修改你的ctypes代码我建议JNA.JNA非常接近ctypes - JNA的API显然受到了ctypes的显着影响!JNA似乎也适用于Jython.

另一种选择是使用像execnet这样的东西.对于execnet来说:它允许你将Jython与CPython配对,它似乎运行良好.但它的GPL许可证使它成为许多人的首选.还有其他选择.

在同一个帖子中我们进行了这个确认评估:

我最近在2.5.2rc2中尝试了ctypes模块,发现:1)还没有ctypes.util.find_library 2)ctypes.Structure不支持非标量类型

所以我同意"更多的占位符"评估.不过,看到它开始是令人兴奋的.