JNA:正确映射COPYDATASTRUCT?

时间:2016-11-01 22:06:19

标签: java jna wm-copydata

我努力工作(调用win32 API:SendMessage使用WM_COPYDATA和COPYDATASTRUCT来保存数据)并且因为它现在在我的Windows 7计算机上工作,我想知道我的映射是否是好的如果我的解决方案没有副作用?

这是我的代码:

/**
     * For usage with WM_COPYDATA
     * cf : https://msdn.microsoft.com/en-us/library/windows/desktop/ms649010(v=vs.85).aspx
     */
    long SendMessage(HWND hWnd, int msg, WPARAM wParam, COPYDATASTRUCT.ByReference lParam);

    int WM_COPYDATA = 0x004A;


//cf : https://msdn.microsoft.com/en-us/library/windows/desktop/ms649010(v=vs.85).aspx
class COPYDATASTRUCT extends Structure {

    public static class ByReference extends COPYDATASTRUCT implements Structure.ByReference {
    }

    public COPYDATASTRUCT() {
        super();
    }

    public int dwData;
    public long cbData;
    public Pointer lpData;

    protected List<String> getFieldOrder() {
        return Arrays.asList(new String[] { "dwData", "cbData", "lpData" });
    }
}

调用代码有两个例子:

User32Extension.COPYDATASTRUCT.ByReference dataStruct = new User32Extension.COPYDATASTRUCT.ByReference();
        String message = "Hello ! :-) !";
        Memory m = new Memory(message.length() + 1);
        m.setString(0, message);
        dataStruct.dwData = 10;
        dataStruct.cbData = message.length() + 1;
        dataStruct.lpData = m;
        dataStruct.write(); // writes to native memory the structure.
        result = user32.SendMessage(hwndTarget, // target hwnd.
                User32Extension.WM_COPYDATA, // copy data message.
                wparam, // current hwnd
                dataStruct // data by reference here
        );

        User32Extension.COPYDATASTRUCT.ByReference myDataStruct = new User32Extension.COPYDATASTRUCT.ByReference();
        User32Extension.TEST_STRUCT myStruct = new User32Extension.TEST_STRUCT();
        //simple C structure here with 4 fields of C types int, char, char and long.
        myStruct.iNumber = 677;
        myStruct.cCode = 'E';
        myStruct.cCode2 = 'T';
        myStruct.lLong1 = new NativeLong(123456789L);
        myStruct.write();
        LOGGER.trace("myStruct (size=" + myStruct.size() + ")=" + myStruct.toString(true));

        myDataStruct.dwData = 11;
        myDataStruct.cbData = myStruct.size();
        myDataStruct.lpData = myStruct.getPointer();
        myDataStruct.write(); // writes to native memory the structure.
        result = user32.SendMessage(hwndTarget, // target hwnd.
                User32Extension.WM_COPYDATA, // copy data message.
                wparam, // current hwnd
                myDataStruct // data
        );

关键是这个代码与我在网上找到的所有内容相比,是COPYDATASTRUCT属性 cbData是long类型。 如果我设置为int,则它不起作用(在遗留C应用程序的WndProc中未正确接收数据)。将DWORD映射到长java类型是否正确?使用NativeLong会更好吗?

另外需要注意的是,对所有实例化的结构(myStruct和myDataStruct)显式调用Structure.write()。在调用SendMessage api之前,为了没有空的内存是必要的。 你认为这是正常的吗?或者jna应该在调用SendMessage之前自动调用它?

提前致谢。

1 个答案:

答案 0 :(得分:0)

此原生代码:

typedef struct tagCOPYDATASTRUCT {
  ULONG_PTR dwData;
  DWORD     cbData;
  PVOID     lpData;
} COPYDATASTRUCT, *PCOPYDATASTRUCT;

映射到此JNA结构定义:

public class COPYDATASTRUCT extends Structure {
    ULONG_PTR dwData;
    DWORD     cbData; // or use "int"
    Pointer   lpData;
}

第一个和最后一个字段的大小不同(并且结构将具有不同的对齐/填充),具体取决于您是否运行32位或64位。