问题
I'm trying to list and get the content of Edit Controls from an external Window in C++ / Java, unfortunately with no success.
When I call GetWindowText or GetWindowTextA it returns an empty value on Edit Controls, I know there are some differences between GetWindowText / GetWindowTextW and GetWindowTextA but I don't know What I'm doing wrong since it works on all other controls.
Here the C++ Code:
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;
}
Java Code:
User32.INSTANCE.EnumChildWindows(hWnd, new User32.WNDENUMPROC() {
@Override
public boolean callback(Pointer hWnd, Pointer arg) {
byte[] windowClassx = new byte[512];
User32.INSTANCE.GetClassNameA(hWnd, windowClassx, 512);
String wClass = Native.toString(windowClassx);
System.out.println(" - Found sub window / control class: " + new String(windowClassx).trim());
if (wClass.toLowerCase().equals("edit")){
byte[] windowTextx = new byte[128];
user32.GetWindowText(hWnd, windowTextx, 128);
String wText = Native.toString(windowTextx);
System.out.println(wText);
}
return true;
}
}, null);
回答1:
I'm not sure what is causing your problem, but this has worked just fine for me:
byte[] windowText = new byte[512];
User32.INSTANCE.GetWindowTextA(hWnd, windowText, 512);
String wText = Native.toString(windowText).trim();
Edit
You should get the edit text a different way, via
User32.SendMessageA(editHwnd, User32.WM_GETTEXT, paramWPARAM, lParamStr);
e.g.,
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));
}
}
来源:https://stackoverflow.com/questions/20596453/getwindowtexta-getwindowtext-returns-empty-value-on-edit-control