2011-03-03 88 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))