定义/从C / C实例化一个Java对象++ [英] Define/Instantiate a java object from c/c++

查看:137
本文介绍了定义/从C / C实例化一个Java对象++的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在java中,看起来像下面这样的code:

I a code in java that looks like the following:

class MyClass
{
public void MyFunc(ISomeListener listener);
}

我要调用从C ++程序此方法并传递给它所需的参数。注意,该参数是一个接口的实现。因此,我需要的确定 创建的Java对象从C ++,并把它传递给方法。我怎样才能做到这一点?

I want to call this method from a c++ program and pass to it the required parameter. Note that the parameter is an implementation of an interface. Thus I need to define AND create java object from c++ and pass it to the method. How can I do this?

P.S。
如果有帮助,原来的问题是,Java程序是一个硬件(像驱动程序)的接口。我想从这个Java code创建一个DLL,以便它可以在任何语言中使用。所以,我想我会使用C / C包住code ++,然后作出一个DLL出来的。

P.S. If it helps, the original problem is that the java program is an interface to a hardware (like a driver). I'm trying to create a DLL from this java code so that it can be used in any language. So I thought I'd wrap the code using C/C++ and then make a DLL out of that.

推荐答案

我设法在java中定义监听器类来解决这个问题。听者与所需的回调签名的本地方法。

I managed to solve the problem by defining the Listener class in java. The listener has a native method with the desired callback signature.

public class MyImpl implements MyInterface
{
  @override
  public native int callback(int i);
}

此编译使用javac。然后用JAVAH生产函数声明与JNI使用。在C程序中,我定义的功能和使用的 RegisterNatives

P.S。如何定义实际使用JNI类的问题仍然没有答案。

P.S. The question of how to actually define a class using JNI remains unanswered.

编辑:结果
有一个缺点,使用这种方法。我必须注册类范围的回调不会反对的范围。


There's a downside to using this method. I will have to register callbacks in class scope not object scope.

这篇关于定义/从C / C实例化一个Java对象++的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆