转换字符串的指针JNA(Converting String to Pointer for JNA)

2019-06-23 15:16发布

我试图用JNA查询有效权限在Windows中的文件。 最后,我打算使用GetEffectiveRightsFromAcl功能 ,但要做到这一点,我需要提供一个指向填充受托人结构 。 该JNA平台(platform.jar)不出现定义这个结构,所以我想,而不是定义它自己。 这是我到目前为止有:

public static class TRUSTEE extends Structure {
    public TRUSTEE() {
        super();
    }
    public TRUSTEE(Pointer p) {
        super(p);
        read();
    }

    public Pointer pMultipleTrustee;
    public int MultipleTrusteeOperation;
    public int TrusteeForm;
    public int TrusteeType;
    public Pointer ptstrName;
}

我试图填充结构是这样的:

private TRUSTEE createTrusteeForCurrentUser() {
    TRUSTEE result = new TRUSTEE();
    result.TrusteeForm = TRUSTEE_FORM.TRUSTEE_IS_NAME;
    result.TrusteeType = TRUSTEE_TYPE.TRUSTEE_IS_USER;

    String strName = "CURRENT_USER";
    // How can I set result.ptstrName using strName?
}

这个谷歌组线程建议使用String中,当结构域char *被调用。 不过,我不认为这是我的情况适合,考虑到ptstrName场被允许指向不同类型的东西,这取决于价值TrusteeForm 。 所以,我觉得我莫名其妙地需要从转换StringPointer来代替。 我发现NativeString在JNA类,它会工作,但它是一个包私有类。

什么是一个Java转换推荐的方法String到原生格式,并获得一个Pointer呢? 难道我即使使用了正确的数据类型TRUSTEE结构? 我有点新的JNA,所以请原谅我,如果我失去了一些东西明显。

更新

我发现了一个解决我的问题,但如果有人有更好的解决办法,我仍想听到它。

Answer 1:

假设你要char *在本机端(您可能需要分配如果字符串包含非ASCII字符更多的内存),

String myString = "CURRENT_USER";
Pointer m = new Memory(myString.length() + 1); // WARNING: assumes ascii-only string
m.setString(0, myString); 

然后,您可以用m ,无论你需要引用“本土”的字符串。

对于宽字符串( wchar_t * ),

String myString = "CURRENT_USER";
Pointer m = new Memory(Native.WCHAR_SIZE * (myString.length() + 1));
m.setWideString(0, myString);


Answer 2:

我通过复制包私人的源代码解决了这个问题NativeString类,并在我的项目创建一个公共副本。 我不得不做出一个小的变化,由于在构造函数中使用包私有方法的。

更新:由于在评论@fragorl注释,如下图所示NativeString的实现方式是通过现在很外的日期。


用法:

private static TRUSTEE createTrusteeForCurrentUser() {
    TRUSTEE result = new TRUSTEE();
    result.TrusteeForm = TRUSTEE_FORM.TRUSTEE_IS_NAME;
    result.TrusteeType = TRUSTEE_TYPE.TRUSTEE_IS_USER;
    result.ptstrName = new NativeString("CURRENT_USER",true).getPointer();
    result.write();
    return result;
}

NativeString.java:

/** Provides a temporary allocation of an immutable C string 
 * (<code>const char*</code> or <code>const wchar_t*</code>) for use when 
 * converting a Java String into a native memory function argument.  
 *
 * @author  Todd Fast, todd.fast@sun.com
 * @author twall@users.sf.net
 */
public class NativeString implements CharSequence, Comparable {

    private Pointer pointer;
    private boolean wide;

    /** Create a native string (NUL-terminated array of <code>char</code>).<p>
     * If the system property <code>jna.encoding</code> is set, its value will
     * be used to encode the native string.  If not set or if the encoding
     * is unavailable, the default platform encoding will be used. 
     */
    public NativeString(String string) {
        this(string, false);
    }

    /** Create a native string as a NUL-terminated array of <code>wchar_t</code>
     * (if <code>wide</code> is true) or <code>char</code>.<p>
     * If the system property <code>jna.encoding</code> is set, its value will
     * be used to encode the native <code>char</code>string.  
     * If not set or if the encoding is unavailable, the default platform 
     * encoding will be used. 
     * 
     * @param string value to write to native memory
     * @param wide whether to store the String as <code>wchar_t</code>
     */
    public NativeString(String string, boolean wide) {
        if (string == null) {
            throw new NullPointerException("String must not be null");
        }
        // Allocate the memory to hold the string.  Note, we have to
        // make this 1 element longer in order to accommodate the terminating 
        // NUL (which is generated in Pointer.setString()).
        this.wide = wide;
        if (wide) {
            int len = (string.length() + 1 ) * Native.WCHAR_SIZE;
            pointer = new Memory(len);
            pointer.setString(0, string, true);
        }
        else {
            byte[] data = Native.toByteArray(string);
            pointer = new Memory(data.length + 1);
            pointer.write(0, data, 0, data.length);
            pointer.setByte(data.length, (byte)0);
        }
    }

    public int hashCode() {
        return toString().hashCode();
    }

    public boolean equals(Object other) {

        if (other instanceof CharSequence) {
            return compareTo(other) == 0;
        }
        return false;
    }

    public String toString() {
        String s = wide ? "const wchar_t*" : "const char*";
        s += "(" + pointer.getString(0, wide) + ")";
        return s;
    }

    public Pointer getPointer() {
        return pointer;
    }

    public char charAt(int index) {
        return toString().charAt(index);
    }

    public int length() {
        return toString().length();
    }

    public CharSequence subSequence(int start, int end) {
        return CharBuffer.wrap(toString()).subSequence(start, end);
    }

    public int compareTo(Object other) {

        if (other == null)
            return 1;

        return toString().compareTo(other.toString());
    }
}


Answer 3:

尝试使用指针类http://jna.java.net/javadoc/com/sun/jna/Pointer.html 。



文章来源: Converting String to Pointer for JNA
标签: java winapi jna