JNA: Pass Pointer to Structure to SendMessage function of User32.dll as the LPARAM

后端 未结 1 1814
一生所求
一生所求 2020-12-22 12:25

I need to perform a simple task : Print out the names of the list view items inside an explorer window. Suppose, I open \"C:\\Documents and Settings\" on my desktop, then wh

相关标签:
1条回答
  • 2020-12-22 12:49

    If you use a primitive array within a Structure, JNA interprets it as a primitive array nested within the native struct. The LVITEM field pszText has pointer type, and more specifically represents a writable byte buffer, so you must use Memory (or an NIO buffer). Using String is only appropriate for native const char * (i.e. where the buffer is read-only). After the call, you can use Pointer.getString(0) to extract the native NUL-terminated string from the memory buffer.

    As for "converting" a structure into an LPARAM value, there's no need. You are free to define your own method signature for SendMessage where the fourth parameter is of type LVITEM (i.e. a native struct *).

    I'd also recommend using the W32APIOptions.DEFAULT_OPTIONS when initializing your User32 library; they automatically handle mapping String and SendMessage to the appropriate native API mappings (windows ANSI or UNICODE, defaulting to UNICODE) so you can use String instead of WString and SendMessage instead of SendMessageW.

    EDIT

    For allocating the buffer to which the called function will write:

    public static class LVITEM extends Structure
    {
        ...
        private static final int MEMSIZE = 260;
        public Pointer pszText = new Memory(MEMSIZE);
        public int cchTextMax = MEMSIZE;
    

    The above structure (LVITEM) should return 60 for its size (more if you're on 64-bit).

    0 讨论(0)
提交回复
热议问题