Retrieving item text with JNA and SendMessage()

五迷三道 提交于 2019-12-11 09:29:54

问题


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.

来源:https://stackoverflow.com/questions/16724663/retrieving-item-text-with-jna-and-sendmessage

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!