简体   繁体   English

无法使用Jython + JNA获取当前的前台窗口标题

[英]Cannot get current foreground window title using Jython + JNA

I am trying to get the current foreground window title using JNA and Jython. 我正在尝试使用JNA和Jython获取当前的前台窗口标题。 My code is just a translation of a Java code to accomplish the same. 我的代码只是Java代码的翻译,以实现相同的目的。 Code in Java works - I have tested it - and was taken from here: https://stackoverflow.com/a/10315344/1522521 Java中的代码有效-我已经对其进行了测试-并从此处获取: https : //stackoverflow.com/a/10315344/1522521

When I call GetLastError() I get 1400 code back, which is ERROR_INVALID_WINDOW_HANDLE - stating that the window handle is invalid. 当我调用GetLastError()我返回了1400代码,即ERROR_INVALID_WINDOW_HANDLE说明窗口句柄无效。 I am sure that window handle is correct, because I can successfully call GetWindowTextLength(); 我确定窗口句柄是正确的,因为我可以成功调用GetWindowTextLength(); ShowWindow(handle, WinUser.SW_MINIMIZE); and ShowWindow(handle, WinUser.SW_MAXIMIZE); ShowWindow(handle, WinUser.SW_MAXIMIZE); to get the length of the title (seems correct) and manipulate the window. 获取标题的长度(似乎正确)并操纵窗口。

I have a hunch, that the problem is with how I use variable text as argument for GetWindowText() . 我直觉,问题在于我如何使用可变text作为GetWindowText()参数。 According to JNA's Javadoc it suppose to be char[] buffer for JNA to copy the text. 根据JNA的Javadoc,它假定是char[]缓冲区,供JNA复制文本。 As I simply pass 'string' it may be incorrect. 当我简单地传递“字符串”时,这可能是不正确的。 This is my code: 这是我的代码:

    def get_current_window_text():
        """
        Get current foreground window title.
        """
        handle = User32.INSTANCE.GetForegroundWindow()
        if User32.INSTANCE.IsWindowVisible(handle):
            print "Text lenght:", User32.INSTANCE.GetWindowTextLength(handle)

            max_length = 512
            text = ''
            result = User32.INSTANCE.GetWindowText(handle, text, max_length)
            print "Copied text length:", result
            if result:
                print "Window text:", text
                return result

            else:
                last_error_code = Kernel32.INSTANCE.GetLastError()
                if last_error_code == Kernel32.ERROR_INVALID_WINDOW_HANDLE:
                    print "[ERROR] GetWindowText: Invalid Window handle!"

                else:
                    print "[ERROR] Unknown error code:", last_error_code

        else:
            print "[ERROR] Current window is not visible"

My hunch was correct, the problems was with incorrect argument when calling GetWindowText(). 我的预感是正确的,问题在于调用GetWindowText()时参数不正确。 It suppose to be char[] - not a Jython variable. 它假定是char[] -不是Jython变量。 This lead me to research more and find something I wasn't aware before - Java arrays in Jython. 这使我进行了更多的研究,找到了我以前不知道的东西-Jython中的Java数组。 As stated in Jython documentation http://www.jython.org/archive/21/docs/jarray.html : 如Jython文档http://www.jython.org/archive/21/docs/jarray.html中所述

Many Java methods require Java array objects as arguments. The way that these arguments are used means that they must correspond to fixed-length, mutable sequences, sometimes of primitive data types. The PyArray class is added to support these Java arrays and instances of this class will be automatically returned from any Java method call that produces an array. In addition, the "jarray" module is provided to allow users of Jython to create these arrays themselves, primarily for the purpose of passing them to a Java method.

The documentation has the mapping table as well. 该文档也有映射表。 The working code would be something like this: 工作代码将如下所示:

import jarray    

text_length = User32.INSTANCE.GetWindowTextLength(handle)
max_length = 512
text = jarray.zeros(text_length, 'c')  
result = User32.INSTANCE.GetWindowText(handle, text, max_length)
print 'Copied text:', result

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM