我需要通过POSIX消息队列来通信Java应用程序和C进程,并且我想使用JNA来执行此操作。使用POSIX消息队列运行JNA示例
经过一番研究,阅读和你的帮助,我开始使用一个简单的Java应用程序,它尝试创建一个消息队列。
/** Simple example of JNA interface mapping and usage. */
public class HelloJNAWorld {
// This is the standard, stable way of mapping, which supports extensive
// customization and mapping of Java to native types.
public interface IPCLibrary extends Library {
IPCLibrary INSTANCE = (IPCLibrary)
Native.loadLibrary("c",IPCLibrary.class);
int msgget(NativeLong key, int msgflg);
}
public static void main(String[] args) {
int id = IPCLibrary.INSTANCE.msgget(new NativeLong(12500), 0600|1);
if(id<0){
System.out.println("Error creating message queue. Id:"+id);
System.out.println(Native.getLastError());
}else{
System.out.println("Message queue id:" + idCola);
}
}
}
我认为msgctl
是地图,因为它只是int msgget(key_t key, int msgflag);
最简单的方法。我假设我可以将key_t
映射为NativeLong
,但msget
返回-1。所以我检查了lastError
,返回的值是2,表示根据errno代码“没有这样的文件或者 ”。
你能帮我解决吗?也许key_
t应该以另一种方式映射?也许我需要更多的库或类似的东西?
我想我发现了什么。我没有注意到错误代码2意味着“没有这样的文件或目录”的通用方式。但是,应用到“msgget”错误列表中意味着“没有消息队列存在,并且msgflg没有断言IPC_CREAT。”所以我查找了IPC_CREAT的值,并更改了“int id = IPCLibrary.INSTANCE.msgget(new NativeLong(12500),0600 | 1);”为另一个“int id = IPCLibrary.INSTANCE.msgget(新的NativeLong(12500),0600 | 01000);”。现在msgget返回0,这似乎并不好,但我正在寻找答案。 – Anne 2012-08-09 09:10:01