1

I'm trying to detect the location of AppData\LocalLow work on Java with JNA on Windows 7. But the closest function available for the job is:

W32API.HRESULT SHGetFolderPath(W32API.HWND hwndOwner,int nFolder,W32API.HANDLE
                               hToken,W32API.DWORD dwFlags,char[] pszPath)

Here we have the Solution in C#

But in my case, JAVA + JNA, I'm wondering how I can use the LocalLow GUID with SHGetFolderPath only, or maybe I should look at the problem from a different angle (maybe JNI would be better here?)

If somebody can help on that, thanks

Cheers

EDIT: Ok, now I added SHGetKnownFolderPath, but here, it keeps returning me strings like that "?f"

static interface Shell32 extends Library {

    public static final int     MAX_PATH            = 260;
    public static final String  FOLDERID_APPDATALOW = "{A520A1A4-1780-4FF6-BD18-167343C5AF16}";

    static Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32",
                    Shell32.class, OPTIONS);

    public int SHGetKnownFolderPath(Guid.GUID rfid, int dwFlags, HANDLE hToken,
                    char[] pszPath);
}

public static void main(String[] args) {

   char[] pszPath = new char[Shell32.MAX_PATH];
   Guid.GUID localLowId = Ole32Util.getGUIDFromString(Shell32.FOLDERID_APPDATALOW);
   int hResult = Shell32.INSTANCE.SHGetKnownFolderPath(localLowId, 0, null, pszPath);

   if (hResult == 0) {
       String path = new String(pszPath);
       int len = path.indexOf('\0');
       path = path.substring(0, len);
       System.out.println(path);
   } else {
       System.err.println("Error: " + hResult);
   }

}

Community
  • 1
  • 1
Kyro
  • 51
  • 1
  • 5
  • You likely need to extend StdCallLibrary rather than Library, since w32 API uses the stdcall calling convention. – technomage Jul 08 '11 at 00:27

2 Answers2

1

This Question is Somewhat old However I managed to Solve this issue. allow me to explain why Kyro approach does not work quite well. This is due
SHGetKnownFolderPath uses a POINTER and not a String or A character array (that why show odd output) . therefore you need to use something like this:

public int SHGetKnownFolderPath(Guid.GUID rfid, int dwFlags, HANDLE hToken, PointerByReference pszPath); a pointer by reference...

so on this case i attach a example I used:

static interface Shell32 extends StdCallLibrary {
    public static final String  FOLDERID_APPDATALOW = "{A520A1A4-1780-4FF6-BD18-167343C5AF16}";

    static Shell32 INSTANCE = (Shell32) Native.loadLibrary("shell32",
                    Shell32.class, W32APIOptions.UNICODE_OPTIONS);

    public int SHGetKnownFolderPath(Guid.GUID rfid, int dwFlags, HANDLE hToken,
                    PointerByReference pszPath);
}

public static void main(String[] args) {
   Guid.GUID localLowId = Ole32Util.getGUIDFromString(Shell32.FOLDERID_APPDATALOW);
   PointerByReference e= new PointerByReference();
   int hResult = Shell32.INSTANCE.SHGetKnownFolderPath(localLowId, 0, null, e);

   if (hResult == 0) {
       char delim='\0';
       char Array[]=e.getValue().getCharArray(0,255);
       for (int i = 0; i < Array.length; i++) {
         if(Array[i]==delim){
             char temparr[]=new char[i];
             System.arraycopy(Array,0,temparr,0,i);
             Array=temparr;
             break;
         }  
       }
       /*dont forget to release the Pointer*/
       Ole32.INSTANCE.CoTaskMemFree(e.getValue());
       System.out.println(Array);
   } else {
       System.err.println("Error: " + hResult);
   }

}
private static interface Ole32 extends StdCallLibrary {

    Ole32 INSTANCE = (Ole32) Native.loadLibrary(
            "Ole32", Ole32.class, W32APIOptions.UNICODE_OPTIONS);

    void CoTaskMemFree(Pointer pv);
}
Sith Nazgûl Cat
  • 204
  • 2
  • 10
  • Thanks for your answer but I ended up using Cache.getCacheDir() from deploy.jar : http://javasourcecode.org/html/open-source/jdk/jdk-6u23/com/sun/deploy/cache/Cache.html (I mainly wanted access to LocalLow to get the java cache folder...) – Kyro Aug 28 '12 at 18:17
1

You can extend Shell32 (or create your own similar class) to get access to the SHGetKnownFolderPath API:

W32API.HRESULT SHGetKnownFolderPath(
                          Guid.GUID rfid, 
                          W32API.DWORD dwFlags, 
                          W32API.HANDLE hToken, 
                          char[] pszPath);
Michael Brewer-Davis
  • 14,018
  • 5
  • 37
  • 49