2012-04-15 29 views
5

Estoy tratando de usar JNA para consultar los permisos efectivos para un archivo en Windows. Eventualmente, planeo usar el GetEffectiveRightsFromAcl function, pero para hacerlo, necesito proporcionar un puntero a un TRUSTEE structure poblado. La plataforma JNA (platform.jar) no parece definir esta estructura, así que estoy tratando de definirla yo mismo. Esto es lo que tengo hasta ahora:Conversión de cadena a puntero para JNA

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

Estoy tratando de poblar la estructura como esta:

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 thread recomienda el uso de String campos en las estructuras cuando un char * se pide. Sin embargo, no creo que esto sea apropiado en mi situación, ya que el campo ptstrName tiene permitido apuntar a diferentes tipos de cosas, dependiendo del valor de TrusteeForm. Por lo tanto, creo que de alguna manera necesito convertir de String a Pointer en su lugar. Encontré la clase NativeString en JNA, que funcionaría, excepto que es una clase privada de paquete.

¿Cuál es la forma recomendada de convertir un Java String a un formato nativo y obtener un Pointer a él? ¿Estoy utilizando el tipo de datos correcto para la estructura TRUSTEE? Soy algo nuevo para JNA, así que discúlpeme si me falta algo obvio.

actualización

he encontrado una solución a mi problema, pero si alguien tiene una solución mejor todavía me gustaría escucharlo.

Respuesta

2

Resolví el problema copiando el código fuente para la clase paquete privado NativeString y creando una copia pública en mi proyecto. Tuve que hacer una pequeña alteración debido al uso de un método privado de paquete en el constructor.

Actualización: Como notas de @fragorl en los comentarios, la implementación de NativeString que se muestra a continuación está por ahora bastante desactualizada.


Uso:

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, [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()); 
    } 
} 
+0

Gracias, esta parece ser la forma "correcta" de hacerlo. Una pregunta: ¿por qué no usaste el constructor 1-arg NativeString, en lugar del de 2 arg? – fragorl

+0

@fragorl Para mi aplicación estaba usando cadenas de caracteres anchos (Unicode), así que necesitaba establecer el parámetro 'ancho' en' verdadero'. El constructor 1-arg lo establece en 'falso'. –

+0

Ahh mi mal, estaba buscando la última versión de jna, donde cambiaron el constructor 1-arg. Ahora se lee: this (string, Native.getDefaultStringEncoding()) ;. Pero aquí tienes el código fuente de una versión anterior; por supuesto, tu publicación es de 2012, woops>< – fragorl

9

Suponiendo que desea char * en el lado nativo (es posible que necesite más memoria asignada si la cadena contiene caracteres no ASCII),

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

Luego puede usar m donde sea que necesite hacer referencia a la cadena "nativa".

Para las cadenas de ancho (wchar_t *),

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

setString (desplazamiento, valor) llamadas setString (desplazamiento, valor, Native.getDefaultStringEncoding()). Parece inseguro suponer que Native.getDefaultStringEncoding() siempre devuelve un formato que usa solo 1 byte por carácter, ¿cuál es el que asigna? – fragorl

+0

Está en lo correcto, la respuesta se actualizó adecuadamente. – technomage

+0

Pointer m = new Memoria (Native.WCHAR_SIZE * (myString.length() + 1); falta de un soporte, es esta destinado a ser Pointer m = new Memoria (Native.WCHAR_SIZE * (myString.length () + 1)); ? – fragorl

Cuestiones relacionadas