2011-03-03 97 views
5

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

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) 

的第一个问题是,蟒蛇找不到ctypes的,所以我从项目网站下载的文件复制到

netbeans\6.9\jython-2.5.1\Lib\ 

(是的,即时通讯使用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 

就像init文件有一些错误O_O帮助家伙! 问候,克里斯

+0

并且错误是? – user470379 2011-03-03 21:22:01

回答

1

的Jython还没有对ctypes的全面支持:http://bugs.jython.org/issue1328

你不能简单地采取编译CPython中的ctypes的图书馆,并把它插入到Jython的。

+0

有关记录,现在在Jython 2.7测试版中,'ctypes'支持似乎好多了。 – Dolda2000 2014-02-12 01:13:21

3

Jython 2.5.1不支持ctypes。在2.5.2中增加了一些实验性支持,但它绝对没有完成。使用Jython代替JNA可能会带来更好的运气。有一个简短的教程here

8

​​Jython实验性并不完整。

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.

然后,他表明,这些变通:

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.

从Jython的用户名为线程邮件列表 “ctypes in Jython” 吉姆·贝克(一个Jython提交者)于2010年11月17日,写

而且在我们这个确认的评估相同的线程:

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.

0

好吧,thx帅哥!我只是重新配置我的NetBeans,现在它使用cPython。一切正常。我只是不得不将 user32.SetCursorPos(windowRect.left + x, windowRect.top + y)更改为: user32.SetCursorPos(c_ulong(windowRect.left + x), c_ulong(windowRect.left + y))