jna中传递结构体数组问题
问题描述:在java程序中传递单个结构体时,程序正常; 但是传递的参数为结构体数组时,程序出错。
提示: java.lang.IllegalArgumentException: Structure array elements must use contiguous memory (bad backing address at Structure array index 1)
具体代码如下:
dlljna2.h
dlljna2.cpp
TestDll1Service.java
此时java程序运行正常
但是我稍作改动dlljna2.cpp文件改为
然后将TestDll1Service.java代码改为
就会报错:java.lang.IllegalArgumentException: Structure array elements must use contiguous memory (bad backing address at Structure array index 1)
请问如何解决这个问题?谢谢!
提示: java.lang.IllegalArgumentException: Structure array elements must use contiguous memory (bad backing address at Structure array index 1)
具体代码如下:
dlljna2.h
#define MYLIBAPI extern "C" __declspec( dllexport )
#define CLIENT_INPUT_SIZE 4
struct UserStruct{
long id;
wchar_t* name;
int age;
};
MYLIBAPI void sayUser(UserStruct* pUserStruct);dlljna2.cpp
#include "stdafx.h"
#include "dlljna2.h"
#include "iostream"
void sayUser(UserStruct* pUserStruct){
std::wcout.imbue(std::locale("chs"));
std::wcout<<L"ID:"<<pUserStruct->id<<std::endl;
std::wcout<<L"姓名:"<<pUserStruct->name<<std::endl;
std::wcout<<L"年龄:"<<pUserStruct->age<<std::endl;
}
TestDll1Service.java
package test.cn;
import test.cn.TestDll1Service.TestDll1.UserStruct;
import com.sun.jna.Library;
import com.sun.jna.NativeLong;
import com.sun.jna.Structure;
import com.sun.jna.Native;
import com.sun.jna.WString;
public class TestDll1Service {
public final static int CLIENT_INPUT_SIZE = 4;
public interface TestDll1 extends Library {
TestDll1 INSTANCE = (TestDll1) Native.loadLibrary("dlljna2",
TestDll1.class);
public void say(WString value);
public static class UserStruct extends Structure {
public NativeLong id;
public WString name;
public int age;
}
public void sayUser(UserStruct struct);
}
public TestDll1Service() {
}
public static void main(String[] args) {
UserStruct userStruct = new UserStruct();
userStruct.id = new NativeLong(100);
userStruct.age = 30;
userStruct.name = new WString("沈东良");
TestDll1.INSTANCE.sayUser(userStruct);
}
}
此时java程序运行正常
但是我稍作改动dlljna2.cpp文件改为
#include "stdafx.h"
#include "dlljna2.h"
#include "iostream"
void sayUser(UserStruct* pUserStruct){
std::wcout.imbue(std::locale("chs"));
for(int i = 0; i < CLIENT_INPUT_SIZE; i++)
{
std::wcout<<L"ID:"<<pUserStruct[i].id<<std::endl;
std::wcout<<L"姓名:"<<pUserStruct[i].name<<std::endl;
std::wcout<<L"年龄:"<<pUserStruct[i].age<<std::endl;
}
然后将TestDll1Service.java代码改为
package test.cn;
import test.cn.TestDll1Service.TestDll1.UserStruct;
import com.sun.jna.Library;
import com.sun.jna.NativeLong;
import com.sun.jna.Structure;
import com.sun.jna.Native;
import com.sun.jna.WString;
public class TestDll1Service {
public final static int CLIENT_INPUT_SIZE = 4;
public interface TestDll1 extends Library {
TestDll1 INSTANCE = (TestDll1) Native.loadLibrary("dlljna2",
TestDll1.class);
public void say(WString value);
public static class UserStruct extends Structure {
public NativeLong id;
public WString name;
public int age;
}
public void sayUser(UserStruct[] struct);
}
public TestDll1Service() {
}
public static void main(String[] args) {
UserStruct[] userStructs = new UserStruct[CLIENT_INPUT_SIZE];
for(int i = 0; i < CLIENT_INPUT_SIZE; i++)
{
userStructs[i] =new UserStruct();
userStructs[i].id = new NativeLong(100);
userStructs[i].age = 30;
userStructs[i].name = new WString("沈东良");
}
TestDll1.INSTANCE.sayUser(userStructs);
}
}
就会报错:java.lang.IllegalArgumentException: Structure array elements must use contiguous memory (bad backing address at Structure array index 1)
请问如何解决这个问题?谢谢!
...全文
相关推荐
java用JNA调用dll实例,包含各种参数调用
实例中演示了各种参数形式的DLL函数调用,如:基本数据类型、基本数据类型的指针和引用、结构体、结构体的指针和引用、函数指针和回调函数、字符串指针、输入数组、输出数组等,非常实用。
JNA实现回调及其他
JNA调用C/C++简单demo,包含文档及简单Demo,基本使用的都包含
JNA 学习文档 深入浅出JJNNAA——快速调用原生函数
深入浅出JJNNAA——快速调用原生函数
jna学习教程
本文原名《使用JNA 方便地调用原生函数》发表于2009 年3 月的“程序员”杂志上。 感谢程序员杂志的许可,使这篇文章能够成为免费的电子版,发布于网络上。 程序员杂志发表此文时,略有裁剪,因此本文比程序员上的文章内容更多。
jna-4.0,jar
jna-4.0.jar以及他的对应源码的下载


