Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/142.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
GetWindowTextA,GetWindowText在编辑控件上返回空值 我试图从C++窗口java中的外部窗口中列出并获取编辑控件的内容,但不幸的是没有成功。_Java_C++_Controls_Edit_Jna - Fatal编程技术网

GetWindowTextA,GetWindowText在编辑控件上返回空值 我试图从C++窗口java中的外部窗口中列出并获取编辑控件的内容,但不幸的是没有成功。

GetWindowTextA,GetWindowText在编辑控件上返回空值 我试图从C++窗口java中的外部窗口中列出并获取编辑控件的内容,但不幸的是没有成功。,java,c++,controls,edit,jna,Java,C++,Controls,Edit,Jna,当我调用GetWindowText或GetWindowTextA时,它在编辑控件上返回一个空值,我知道GetWindowText/GetWindowTextW和GetWindowTextA之间有一些区别,但我不知道我做错了什么,因为它在所有其他控件上都起作用 这里的C++代码: BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam) { cout <<"----------CHILD------------"<<

当我调用GetWindowText或GetWindowTextA时,它在编辑控件上返回一个空值,我知道GetWindowText/GetWindowTextW和GetWindowTextA之间有一些区别,但我不知道我做错了什么,因为它在所有其他控件上都起作用

这里的C++代码:

BOOL CALLBACK EnumChildProc(HWND hwnd, LPARAM lParam)
{
    cout <<"----------CHILD------------"<<endl;
    char class_name[80];
    char title[80];
    GetClassName(hwnd,class_name, sizeof(class_name));
    GetWindowText(hwnd,title,sizeof(title));
    cout <<"\tWindow title: "<<title<<endl;
    cout <<"\tClass name: "<<class_name<<endl<<endl;
     return TRUE;
}

我不确定是什么原因导致了您的问题,但这对我来说效果很好:

byte[] windowText = new byte[512];
User32.INSTANCE.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText).trim();

编辑
您应该通过另一种方式获得编辑文本

User32.SendMessageA(editHwnd, User32.WM_GETTEXT, paramWPARAM, lParamStr);
e、 g


MINGW64/C++:似乎有一些限制,即使SendMessage(…WM_GETTEXT…)也会为大多数窗口句柄返回空窗口文本,只有从相关进程中才能得到有效响应。

它对我不起作用,只是用Notpad试过,打开它,在里面写了一些文本,但如果在hWnd上调用GetWindowTextA,我什么也得不到,在C++和Javajust都尝试了NoDead的代码,它既从记事本和编辑控件中找到HWNDS,又不返回任何文本,所以LPARAMSTR是空的,我还比较了HWNDS和StIs++,它们是正确的。此外,SendMessageA返回0(resultBool为0),所以我猜它失败了!或者这可能是某种32位/64位的限制?我只能使用另一个64位进程向64位进程发送消息吗?我解决了这个问题,它是32/64位限制。你的代码很好,谢谢。我用的是32位JRE,刚换成64位JRE,一切都很好。
User32.SendMessageA(editHwnd, User32.WM_GETTEXT, paramWPARAM, lParamStr);
import com.sun.jna.Native;
import com.sun.jna.platform.win32.WinDef.HWND;
import com.sun.jna.platform.win32.WinDef.LPARAM;
import com.sun.jna.platform.win32.WinDef.LRESULT;
import com.sun.jna.platform.win32.WinDef.WPARAM;
import com.sun.jna.win32.StdCallLibrary;

public class GetTextInNotePad {
   public static final String NOTEPAD_CLASS = "Notepad";
   public static final String EDIT_CLASS = "Edit";

   interface User32 extends StdCallLibrary {
      User32 INSTANCE = (User32) Native.loadLibrary("user32", User32.class);
      int WM_SETTEXT = 0x000c;
      int WM_GETTEXT = 0x000D;

      HWND FindWindowA(String lpClassName, String lpWindowName);
      HWND FindWindowExA(HWND hwndParent, HWND hwndChildAfter, String lpClassName,
            String lpWindowName);
      LRESULT SendMessageA(HWND paramHWND, int paramInt, WPARAM paramWPARAM, LPARAM paramLPARAM);
      LRESULT SendMessageA(HWND editHwnd, int wmGettext, long l, byte[] lParamStr);
      int GetClassNameA(HWND hWnd, byte[] lpString, int maxCount);
   }

   public static void main(String[] args) {
      User32 user32 = User32.INSTANCE;
      String lpClassName = "Notepad";
      HWND notePadHwnd = user32.FindWindowA(lpClassName , null);
      HWND editHwnd = user32.FindWindowExA(notePadHwnd, null, EDIT_CLASS, null); 
      byte[] lParamStr = new byte[512];
      LRESULT resultBool = user32.SendMessageA(editHwnd, User32.WM_GETTEXT, 512, lParamStr);

      System.out.println("lParamStr: " + Native.toString(lParamStr));
   }
}