我参考的代码如下:Java code
假设我们现在有这样一个C语言结构体
struct UserStruct{
long id;
wchar_t* name;
int age;
};
使用上述结构体的函数
#define MYLIBAPI extern "C" __declspec( dllexport )
MYLIBAPI void sayUser(UserStruct* pUserStruct);
对应的Java程序中,在例1的 接口中添加下列代码:
public static class UserStruct extends Structure{
public NativeLong id;
public WString name;
public int age;
public static class ByReference extends UserStruct implements Structure.ByReference { }
public static class ByValue extends UserStruct implements Structure.ByValue
{ }
}
public void sayUser(UserStruct.ByReference struct);
Java中的调用代码:
UserStruct userStruct=new UserStruct ();
userStruct.id=new NativeLong(100);
userStruct.age=30;
userStruct.name=new WString("奥巴马"); TestDll1.INSTANCE.sayUser(userStruct); 我觉得这段代码和我的应该比较类似的, 我做了如下的声明,请高手帮看看哪里有问题谢谢
首先模仿结构的声明:
Java code
public static class UserStruct extends Structure{
public static class ByReference extends UserStruct implements Structure.ByReference { }
public static class ByValue extends UserStruct implements Structure.ByValue { }
public int Address;
public int Length;
public byte num[][]=new byte[8][30];
}
接口函数里做如下模仿
Java code
public static interface RFSAPIV2 extends Library {
RFSAPIV2 INSTANCE = (RFSAPIV2)
Native.loadLibrary("RFSAPIV2",
RFSAPIV2.class);
int ReadLabelID(long hh,int nMax, UserStruct.ByReference idBuffer,int[] nCounter);
} 在按钮过程中做如下
Java codeprivate void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
UserStruct idBuffer = new UserStruct();
idBuffer.Address=0;
idBuffer.Length=0;
idBuffer.num[0][0]=(byte)0x00; // 这地方没有把数组全面初始化不知道影响否??? int nCounter[]={0};
int nMax=10; apiReturn=RFSAPIV2.INSTANCE.ReadID(hScanner[0], nMax, idBuffer, nCounter);
} 做语法检查时,,提示上面的函数调用部分的红色字体,即第三个参数 错误。
“
无法将 接口 desktopapplication2.DesktopApplication2View.RFSAPIV2 中的 方法 ReadID 应用于 long,int,desktopapplication2.DesktopApplication2View.UserStruct.ByReference,int[](long,int,desktopapplication2.DesktopApplication2View.UserStruct,int[])
--
(按 Alt-Enter 组合键可显示提示)
”
请指导一下 ,谢谢。
指导解决问题后,送500分,外加3合1 ARM开发板一块(邮资自付)
假设我们现在有这样一个C语言结构体
struct UserStruct{
long id;
wchar_t* name;
int age;
};
使用上述结构体的函数
#define MYLIBAPI extern "C" __declspec( dllexport )
MYLIBAPI void sayUser(UserStruct* pUserStruct);
对应的Java程序中,在例1的 接口中添加下列代码:
public static class UserStruct extends Structure{
public NativeLong id;
public WString name;
public int age;
public static class ByReference extends UserStruct implements Structure.ByReference { }
public static class ByValue extends UserStruct implements Structure.ByValue
{ }
}
public void sayUser(UserStruct.ByReference struct);
Java中的调用代码:
UserStruct userStruct=new UserStruct ();
userStruct.id=new NativeLong(100);
userStruct.age=30;
userStruct.name=new WString("奥巴马"); TestDll1.INSTANCE.sayUser(userStruct); 我觉得这段代码和我的应该比较类似的, 我做了如下的声明,请高手帮看看哪里有问题谢谢
首先模仿结构的声明:
Java code
public static class UserStruct extends Structure{
public static class ByReference extends UserStruct implements Structure.ByReference { }
public static class ByValue extends UserStruct implements Structure.ByValue { }
public int Address;
public int Length;
public byte num[][]=new byte[8][30];
}
接口函数里做如下模仿
Java code
public static interface RFSAPIV2 extends Library {
RFSAPIV2 INSTANCE = (RFSAPIV2)
Native.loadLibrary("RFSAPIV2",
RFSAPIV2.class);
int ReadLabelID(long hh,int nMax, UserStruct.ByReference idBuffer,int[] nCounter);
} 在按钮过程中做如下
Java codeprivate void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
UserStruct idBuffer = new UserStruct();
idBuffer.Address=0;
idBuffer.Length=0;
idBuffer.num[0][0]=(byte)0x00; // 这地方没有把数组全面初始化不知道影响否??? int nCounter[]={0};
int nMax=10; apiReturn=RFSAPIV2.INSTANCE.ReadID(hScanner[0], nMax, idBuffer, nCounter);
} 做语法检查时,,提示上面的函数调用部分的红色字体,即第三个参数 错误。
“
无法将 接口 desktopapplication2.DesktopApplication2View.RFSAPIV2 中的 方法 ReadID 应用于 long,int,desktopapplication2.DesktopApplication2View.UserStruct.ByReference,int[](long,int,desktopapplication2.DesktopApplication2View.UserStruct,int[])
--
(按 Alt-Enter 组合键可显示提示)
”
请指导一下 ,谢谢。
指导解决问题后,送500分,外加3合1 ARM开发板一块(邮资自付)
{
String dllPath = "";
System.loadLibrary(dllPath + "libterm");
}
的是用的 JNA 方法时的声明么?
啦啦啦
楼主好样的
java当中先定义本地方法,供其他地方使用。
实现由dll完成,load之后就ok。