2012-04-15 13 views
5

JNAを使用して、Windowsのファイルの有効なアクセス許可を照会しようとしています。最終的にはGetEffectiveRightsFromAcl functionを使用する予定ですが、そのためには、移植されたTRUSTEE structureへのポインタを提供する必要があります。 JNAプラットフォーム(platform.jar)はこの構造体を定義していないので、代わりに自分自身で定義しようとしています。私はこのような構造を移入しようとしている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; 
} 

:ここで私がこれまで持っているものだ

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 threadchar *がために呼び出されたときの構造でStringフィールドを使用することをお勧めします。しかし、TrusteeFormの値に応じて、ptstrNameフィールドがさまざまな種類のものを指すことが許可されていることを考慮すると、これは私の状況では適切ではないと私は考えています。だから、私は何とかStringからPointerに変換する必要があると思う。私はNativeStringクラスがJNAにあることを発見しました。パッケージプライベートクラス以外は動作します。

Java Stringをネイティブフォーマットに変換してPointerを取得するには、どのような方法が推奨されますか?私はTRUSTEE構造体に正しいデータ型を使用していますか?私はJNAに若干新しくなっていますので、何か明白でないものがあれば私には容赦してください。

更新

は、私は私の問題の解決策を見つけましたが、誰もがよりよい解決策を持っている場合、私はまだそれを聞きたいと思います。

答えて

2

package-private NativeStringクラスのソースコードをコピーし、私のプロジェクトで公開コピーを作成することで問題を解決しました。私は、コンストラクタでパッケージプライベートメソッドを使用するため、1つのマイナーな変更を加えなければなりませんでした。

更新:コメントに@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, [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

ありがとう、これはそれを行うための "正しい"方法と思われる。 1つの質問 - 2-argの代わりに1-arg NativeStringコンストラクタを使用しなかったのはなぜですか? – fragorl

+0

@fragorl私のアプリケーションではワイド文字(Unicode)文字列を使用していたので、 'wide'パラメータを' true'に設定する必要がありました。 1-argコンストラクタは、それを 'false'に設定します。 –

+0

ああ、私は1-argコンストラクタを変更したjnaの最新バージョンを見ていました。これで、this(string、Native.getDefaultStringEncoding());が読み込まれます。しかし、ここには古いバージョンのソースコードがあります。もちろん、あなたの投稿は2012年のものです。< – fragorl

0

http://jna.java.net/javadoc/com/sun/jna/Pointer.htmlのポインタクラスを試してみてください。

+0

私は 'Pointer'を必要と知って、私は質問がネイティブ形式にJavaの' STRING'を変換すると 'Pointer'を取得する方法の詳細だと思いそれに反対する。 –

9

あなたがネイティブ側に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); 
+0

setString(offset、value)はsetString(offset、value、Native.getDefaultStringEncoding())を呼び出します。 Native.getDefaultStringEncoding()は、文字ごとに1バイトしか使用しない形式を返すと仮定するのは安全ではないようです。 – fragorl

+0

あなたは正しいです、適切に更新された回答。 – technomage

+0

ポインタm = newメモリ(Native.WCHAR_SIZE *(myString.length()+ 1); には括弧がありません ポインタm =新しいメモリ(Native.WCHAR_SIZE *(myString.length )+ 1)); ? – fragorl

関連する問題