java 将字符串转换为 JNA 的指针

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10158582/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 23:51:34  来源:igfitidea点击:

Converting String to Pointer for JNA

javawinapijna

提问by Kevin K

I'm trying to use JNA to query the effective permissions for a file in Windows. Eventually, I plan on using the GetEffectiveRightsFromAcl function, but to do so, I need to provide a pointer to a populated TRUSTEE structure. The JNA Platform (platform.jar) doesn't appear define this struct, so I'm trying to define it myself instead. Here's what I have so far:

我正在尝试使用 JNA 来查询 Windows 中文件的有效权限。最后,我计划使用GetEffectiveRightsFromAcl 函数,但为此,我需要提供一个指向填充的TRUSTEE 结构的指针。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;
}

I'm trying to populate the structure like this:

我正在尝试填充这样的结构:

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?
}

This Google Groups threadrecommends using Stringfields in structures when a char *is called for. However, I don't think this is appropriate in my situation, considering the ptstrNamefield is allowed to point to different types of things, depending on the value of TrusteeForm. So, I think I somehow need to convert from Stringto Pointerinstead. I found the NativeStringclass in JNA, which would work, except it's a package-private class.

此 Google Groups 线程建议Stringchar *调用a 时使用结构中的字段。但是,我认为这不适合我的情况,考虑到ptstrName允许字段指向不同类型的事物,具体取决于TrusteeForm. 所以,我想我需要以某种方式从 转换StringPointer。我在 JNA 中找到了NativeString类,它可以工作,但它是一个包私有类。

What's the recommended way to convert a Java Stringto a native format and obtain a Pointerto it? Am I even using the right data type for the TRUSTEEstruct? I'm somewhat new to JNA, so please excuse me if I'm missing something obvious.

将 Java 转换String为本机格式并获得Pointer它的推荐方法是什么?我什至为TRUSTEE结构使用了正确的数据类型吗?我对 JNA 有点陌生,所以如果我遗漏了一些明显的东西,请原谅。

Update

更新

I found a solution to my problem, but if anyone has a better solution I'd still like to hear it.

我找到了解决我的问题的方法,但如果有人有更好的解决方案,我仍然希望听到它。

采纳答案by Kevin K

I solved the problem by copying the source code for package-private NativeStringclass and creating a public copy in my project. I had to make one minor alteration due to the use of a package-private method in the constructor.

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

Update:As @fragorl notes in the comments, the implementation of NativeString shown below is by now quite out-of-date.

更新:正如@fragorl 在评论中指出的那样,下面显示的 NativeString 的实现现在已经过时了。



Usage:

用法:

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:

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, [email protected]
 * @author [email protected]
 */
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());
    }
}

回答by technomage

Assuming you want char *on the native side (you may need more memory allocated if the string contains non-ascii characters),

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

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

You can then use mwherever you need to reference the "native" string.

然后,您可以m在需要引用“本机”字符串的任何地方使用。

For wide strings (wchar_t *),

对于宽字符串 ( wchar_t *),

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