简体   繁体   English

将指针从java传递给本机

[英]Passing pointer from java to native

I'm making an interface to a DLL library, so i can use it with Java. 我正在创建一个DLL库的接口,所以我可以用它与Java。 I need my native function to modify the value of a jlong parameter. 我需要我的原生函数来修改jlong​​参数的值。 Like the parameter was passed by reference. 就像参数通过引用传递一样。 My Java method must have the exact parameters as the native function MPUSBWrite shown down here. 我的Java方法必须具有与此处显示的本机函数MPUSBWrite一样的确切参数。

Example of actual not working code: The MPUSBWrite gets its 4th parameter as reference and modify itrs value. 实际不工作代码的示例:MPUSBWrite获取其第4个参数作为参考并修改其值。 I always get 0 when i read the pLength passed variable. 当我读取pLength传递的变量时,我总是得到0。

Java: Java的:

public static native boolean Write(long handle, byte[] pData, int dwLen, long pLength, int dwMilliseconds);

Native C++: 原生C ++:

JNIEXPORT jboolean JNICALL Java_jPicUsb_iface_Write
(JNIEnv *env, jclass jc, jlong handle, jbyteArray pData, jint dwLen, jlong pLength, jint dwMilliseconds) {
    jniByteArray b(env, pData);
    DWORD res = MPUSBWrite((HANDLE)handle,b.getBytes(),dwLen,(PDWORD)pLength,dwMilliseconds);
    if (res) {
        return JNI_TRUE;
    } else {
        return JNI_FALSE;
    }
}

Is there a way my C++ code modify the value of the pLength variable by reference? 有没有办法我的C ++代码通过引用修改pLength变量的值?

SWIG supports this functionality. SWIG支持此功能。

With SWIG you can update Java parameter values like pointers in C++. 使用SWIG,您可以更新Java参数值,例如C ++中的指针。 This is working for Object parameters very fine. 这适用于Object参数非常好。 You can put a Long object in to get it work. 你可以放一个Long对象来使它工作。

With Swig you can write a layer between java and C++ where you can put the pointer value after a function call into the Java object. 使用Swig,您可以在java和C ++之间编写一个层,您可以在函数调用之后将指针值放入Java对象。 Swig also generates the Java classes, you have to us to call the C++ functions. Swig还生成Java类,您必须调用C ++函数。 When the C++ function contains another C++ class as parameter, Swig generates this C++ class as Java object with getter,setter and all C++ functions you want to call. 当C ++函数包含另一个C ++类作为参数时,Swig将此C ++类生成为具有getter,setter和要调用的所有C ++函数的Java对象。

For this functionality Swig has a special language you have to write in a VisualStudio project. 对于此功能,Swig具有您必须在VisualStudio项目中编写的特殊语言。 but all this is described in the Swig Manual 但所有这些都在Swig手册中有所描述

SWIG is a software development tool that connects programs written in C and C++ with a variety of high-level programming languages. SWIG是一个软件开发工具,它将用C和C ++编写的程序与各种高级编程语言连接起来。 SWIG is used with different types of languages including common scripting languages such as Perl, PHP, Python, Tcl and Ruby. SWIG与不同类型的语言一起使用,包括常见的脚本语言,如Perl,PHP,Python,Tcl和Ruby。 The list of supported languages also includes non-scripting languages such as C#, Common Lisp (CLISP, Allegro CL, CFFI, UFFI), Java, Lua, Modula-3, OCAML, Octave and R. 支持的语言列表还包括非脚本语言,如C#,Common Lisp(CLISP,Allegro CL,CFFI,UFFI),Java,Lua,Modula-3,OCAML,Octave和R.

No. 没有。

Your best option, if you truly need to do this, is to define your fourth parameter as a long[], and have your JNI code update the array element. 如果你真的需要这样做,你最好的选择是将你的第四个参数定义为long [],并让你的JNI代码更新数组元素。

Actually, your best option would be to consider why you can't return the long from the method. 实际上,你最好的选择是考虑为什么你不能从方法中返回多长时间。

This is not a JNI issue. 这不是JNI问题。 The function is called by value, there is no way to modify that. 该函数按值调用,无法修改该函数。 Pass in an object or an array, so you can have the reference. 传入一个对象或一个数组,以便您可以获得引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM