JNA通过引用dll传递字符串,但不返回 [英] JNA passing String by reference to dll but non return

查看:551
本文介绍了JNA通过引用dll传递字符串,但不返回的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在使用Java和dll时遇到问题。

Hi I have a problem with java and dll.

我需要通过引用将字符串值传递给dll,但这并不成功。

I need to pass string value to dll by reference but, It's not successful.

public short ReadData(int block_id, int offset, int data_size,StringByReference dataBuf, IntByReference err_code);

问题是 StringByReference dataBuf ,我尝试找到解决方案的许多方法,但是没有用。

The problem is StringByReference dataBuf, I try many way to find solution but it doesn't work.

调用函数是:

ReadData(0, 4, 13, ??? , status);

原因没有.dll文件。

Cause it Had no .dll document.

功能

`public static short Read_Data(int card_type, int block_id, int offset, int data_size, String dataBuf, IntByReference err_code){
        short rc;
        IntByReference status = new IntByReference(0);
         int len = data_size+16;
        //StringByReference dataBuf_ref =  new StringByReference( spaces(data_size+16));
        //StringBuilder zText = new StringBuilder ();
        //zText.append(dataBuf);
        dataBuf = spaces(data_size+16);
        //StringByReference a = new StringByReference(dataBuf);
    //  StringByReference a = new StringByReference(dataBuf);

    //Pointer ppp =  new Memory(dataBuf.length()+1);
    //ppp.setByte(len, dataBuf.getBytes()[0]);
    //PointerByReference p = new PointerByReference(ppp);


    //Pointer pp = null ;
    //pp.setString(len, dataBuf);
    //p.setValue(a.getPointer());
    StringBuffer_REF z = new StringBuffer_REF(dataBuf);

    //byte[] b = dataBuf.getBytes(); 
    //Memory m = new Memory(len+1);
    //m.write(0, b, 0, len);




    System.out.println("Read_Data");

    System.out.println("status "+status.getValue());




    rc = s_ope.ReadData(block_id, offset, data_size,?????, status); /// HERE NEED HELP
//  System.out.println("dataBuf_ref"+ a.getValue().replace(" ", "*"));

    if(rc != 0){
        System.out.println("Err Read_Data : "+rc+" - "+status.getValue());
    }
    err_code = status;
    return rc;
}`


推荐答案

我找到了您的答案网站上的问题。只需创建新的 StringByReference java类型,它就可以使用。

I found answer to your question on this site. Just create new StringByReference java type and it should work.

// This is a class that facilitates passing a String by reference to
// C library routines so that the string  may be modified by the C
// routine and the modifications is reflected on JAVA side as well
// Save this in a file StringByReference.java in current directory

import com.sun.jna.ptr.ByReference;

public class StringByReference extends ByReference {
    public StringByReference() {
        this(0);
    }

    public StringByReference(int size) {
        super(size < 4 ? 4 : size);
        getPointer().clear(size < 4 ? 4 : size);
    }

    public StringByReference(String str) {
        super(str.length() < 4 ? 4 : str.length() + 1);
        setValue(str);
    }

    private void setValue(String str) {
        getPointer().setString(0, str);
    }

    public String getValue() {
        return getPointer().getString(0);
    }
}

这篇关于JNA通过引用dll传递字符串,但不返回的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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