Retrieving item text with JNA and SendMessage()

2019-08-03 19:15发布

问题:

I am attempting to retrieve the item text from a Win32 ListView-like control. I am using JNA and SendMessageW() to send LVM_GETITEMTEXTW to the control. I have been successful at retrieving the item count (via LVM_GETITEMCOUNT) but am stumped at this point. My User32 class is setup like so:

public interface MyUser32 extends User32 {

    MyUser32 INSTANCE = (MyUser32)Native.loadLibrary("user32", MyUser32.class);
    LRESULT SendMessageW(HWND hWnd, int msg, WPARAM wParam, LVITEM lParam); 

}

My LVITEM class is setup like so:

public class LVITEM extends Structure{


    public LVITEM() {
        pszText = new Memory(MEMSIZE);
        cchTextMax = MEMSIZE;
    }

    private static final int MEMSIZE = 256;

    public UINT mask;
    public int iItem;
    public int iSubItem;
    public UINT state;
    public UINT stateMask;
    public Pointer pszText;
    public int cchTextMax;
    public int iImage;
    public LPARAM lParam;
    public int iIndent;

    protected List<String> getFieldOrder() {    
        return Arrays.asList(new String[] { "mask", "iItem", "iSubItem", "state", "stateMask", "pszText", "cchTextMax", "iImage", "lParam", "iIndent"});    
    }
}

And the code that calls it all is like so:

MyUser32 u32 = MyUser32.INSTANCE;   
LVITEM lvItem = new LVITEM();
WPARAM wPar = new WPARAM(1);

...

lvItem.iSubItem = 0;
res = u32.SendMessageW(handle, LVM_GETITEMTEXTW, wPar, lvItem);

System.out.println(res.intValue());
s = lvItem.pszText.getString(0);        
System.out.println(s);

I've left out a bit of the code but I believe those are the important parts. My issue is that when I print out res.intValue() it is always 0 (meaning no text was returned) and when I print out the string value of pszText it is always some garbage characters. I'm completely stumped at this point so any suggestions are greatly appreciated. Thanks.