-
Notifications
You must be signed in to change notification settings - Fork 1.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added Shell32.SHGetKnownFolderPath and corresponding constants #334
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,10 +15,14 @@ | |
import junit.framework.TestCase; | ||
|
||
import com.sun.jna.Native; | ||
import com.sun.jna.platform.win32.Guid.GUID; | ||
import com.sun.jna.platform.win32.ShellAPI.APPBARDATA; | ||
import com.sun.jna.platform.win32.WinDef.DWORD; | ||
import com.sun.jna.platform.win32.WinDef.LPVOID; | ||
import com.sun.jna.platform.win32.WinDef.RECT; | ||
import com.sun.jna.platform.win32.WinDef.UINT_PTR; | ||
import com.sun.jna.platform.win32.WinNT.HANDLE; | ||
import com.sun.jna.platform.win32.WinNT.HRESULT; | ||
import com.sun.jna.ptr.PointerByReference; | ||
|
||
|
||
|
@@ -127,4 +131,16 @@ public void testResizeDesktopFromTop() throws InterruptedException { | |
|
||
} | ||
|
||
public void testSHGetKnownFolderPath() | ||
{ | ||
int flags = ShlObj.KNOWN_FOLDER_FLAG.NONE.getFlag(); | ||
PointerByReference outPath = new PointerByReference(); | ||
HANDLE token = null; | ||
GUID guid = KnownFolders.FOLDERID_Fonts; | ||
HRESULT hr = Shell32.INSTANCE.SHGetKnownFolderPath(guid, flags, token, outPath); | ||
|
||
Ole32.INSTANCE.CoTaskMemFree(new LPVOID(outPath.getPointer().getLong(0))); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe LPVOID should have a LPVOID(Pointer p) constructor? Or maybe CoTaskMemFree should take a Pointer? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Does declaring a Otherwise I would declare another version of There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This test could retrieve a well known path and should examine the string result. For example, FOLDERID_Cookies always ends with Cookies (even on non-English systems I believe). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. About testing: Unfortunately, most folder names depend on the language. Also, many of them can be changed by the user (e.g. home folder), so we might have to rely on the CoTaskMemFree: It also works with a plain There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like the definition of LPVOID is incorrect. From WinDef.h, the definition is
Which means it should be of JNA type On May 25, 2014, at 4:02 AM, Martin Steiger notifications@github.com wrote:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you want me to change that or prefer doing that yourself? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You can make the correction, I think it’s appropriate for your patch. It’ll fix your “initialize from pointer” issue, although it does introduce an incompatible binary change if someone happens to be doing anything with LPVOID other than passing it around. On May 25, 2014, at 8:10 AM, Martin Steiger notifications@github.com wrote:
|
||
|
||
assertTrue(W32Errors.SUCCEEDED(hr.intValue())); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,4 +35,42 @@ public void testGetFolderPath() { | |
public final void testGetSpecialFolderPath() { | ||
assertFalse(Shell32Util.getSpecialFolderPath(ShlObj.CSIDL_APPDATA, false).isEmpty()); | ||
} | ||
|
||
public void testGetKnownFolderPath() | ||
{ | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Fonts)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Desktop)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Startup)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Programs)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_StartMenu)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Recent)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_SendTo)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Documents)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Favorites)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_NetHood)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_PrintHood)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Templates)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_CommonStartup)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_CommonAdminTools)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_CDBurning)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Music)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_SavedGames)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_SavedSearches)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_AdminTools)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_ProgramFiles)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_ProgramData)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_ProgramFilesCommon)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_ProgramFilesCommonX86)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Programs)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Windows)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Public)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_PublicDesktop)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Links)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_LocalAppData)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_Libraries)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_RoamingAppData)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_UserProfiles)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_UserProgramFiles)); | ||
assertNotNull(Shell32Util.getKnownFolderPath(KnownFolders.FOLDERID_UserProgramFilesCommon)); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe there's a way to enumerate those via reflection? :) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure, but unfortunately, it won't help much. Many constants don't point a unique folder and getKnownFolderPath() fails for them. I manually picked those who work on Windows 7. The constants were converted automatically using reg-exp search & replace, so I assume that either all of them are correct or none. |
||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wanted to use LPSTR.ByReference here, but the returned string was garbage. Can't really explain why ..