2011-03-03 10 views
5

Tengo un problema con el uso de ctypes lib en mi secuencia de comandos python. Aquí está mi código (que se encuentra en Internet):Uso de ctypes con jython

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) 

El primer problema fue que el pitón no podía encontrar las ctypes así que copié los archivos descargados desde el sitio del proyecto para

netbeans\6.9\jython-2.5.1\Lib\ 

(sip , im usando NetBeans) y luego se muestra este 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 

al igual que el archivo de inicio tiene algunos errores o_O ayuda chicos! Saludos, Chris

+0

¿Y el error es? – user470379

Respuesta

1

Jython todavía no tiene soporte completo para ctypes: http://bugs.jython.org/issue1328

No se puede simplemente tomar la biblioteca ctypes compilado para CPython, y lo conectan en Jython.

+0

Para el registro, el soporte de 'ctypes' parece ser mucho mejor ahora, en la versión beta de Jython 2.7. – Dolda2000

3

ctypes no es compatible con Jython 2.5.1. Se ha agregado algo de soporte experimental en 2.5.2, pero ciertamente no está completo. Tal vez tenga mejor suerte usando JNA con Jython. Hay un breve tutorial here.

8

ctypes en Jython experimental y no es completo.

De los Jython lista de distribución en un hilo titulado "ctypes in Jython" Jim Baker (committer Jython) escribió el 17 de noviembre de 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.

Sugiere, entonces estos arounds de trabajo:

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.

Más adelante, en el mismo hilo tenemos esta evaluación confirma:

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

Ok, tíos! Acabo de reconfigurar mis NetBeans, ahora está usando cPython. Todo funciona. Solo tuve que cambiar la línea user32.SetCursorPos(windowRect.left + x, windowRect.top + y) a: user32.SetCursorPos(c_ulong(windowRect.left + x), c_ulong(windowRect.left + y))