static sal_Bool GetSpecialFolder(rtl_uString **strPath, int nFolder) { sal_Bool bRet = sal_False; HINSTANCE hLibrary; sal_Char PathA[_MAX_PATH]; sal_Unicode PathW[_MAX_PATH]; if ((hLibrary = LoadLibrary("shell32.dll")) != NULL) { BOOL (WINAPI *pSHGetSpecialFolderPathA)(HWND, LPSTR, int, BOOL); BOOL (WINAPI *pSHGetSpecialFolderPathW)(HWND, LPWSTR, int, BOOL); pSHGetSpecialFolderPathA = (BOOL (WINAPI *)(HWND, LPSTR, int, BOOL))GetProcAddress(hLibrary, "SHGetSpecialFolderPathA"); pSHGetSpecialFolderPathW = (BOOL (WINAPI *)(HWND, LPWSTR, int, BOOL))GetProcAddress(hLibrary, "SHGetSpecialFolderPathW"); if (pSHGetSpecialFolderPathA) { if (pSHGetSpecialFolderPathA(GetActiveWindow(), PathA, nFolder, TRUE)) { rtl_string2UString( strPath, PathA, (sal_Int32) strlen(PathA), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS); OSL_ASSERT(*strPath != NULL); bRet = sal_True; } } else if (pSHGetSpecialFolderPathW) { if (pSHGetSpecialFolderPathW(GetActiveWindow(), PathW, nFolder, TRUE)) { rtl_uString_newFromStr( strPath, PathW); bRet = sal_True; } } else { HRESULT (WINAPI *pSHGetSpecialFolderLocation)(HWND, int, LPITEMIDLIST *) = (HRESULT (WINAPI *)(HWND, int, LPITEMIDLIST *))GetProcAddress(hLibrary, "SHGetSpecialFolderLocation"); BOOL (WINAPI *pSHGetPathFromIDListA)(LPCITEMIDLIST, LPSTR) = (BOOL (WINAPI *)(LPCITEMIDLIST, LPSTR))GetProcAddress(hLibrary, "SHGetPathFromIDListA"); BOOL (WINAPI *pSHGetPathFromIDListW)(LPCITEMIDLIST, LPWSTR) = (BOOL (WINAPI *)(LPCITEMIDLIST, LPWSTR))GetProcAddress(hLibrary, "SHGetPathFromIDListW"); HRESULT (WINAPI *pSHGetMalloc)(LPMALLOC *) = (HRESULT (WINAPI *)(LPMALLOC *))GetProcAddress(hLibrary, "SHGetMalloc"); if (pSHGetSpecialFolderLocation && (pSHGetPathFromIDListA || pSHGetPathFromIDListW ) && pSHGetMalloc ) { LPITEMIDLIST pidl; LPMALLOC pMalloc; HRESULT hr; hr = pSHGetSpecialFolderLocation(GetActiveWindow(), nFolder, &pidl); /* Get SHGetSpecialFolderLocation fails if directory does not exists. */ /* If it fails we try to create the directory and redo the call */ if (! SUCCEEDED(hr)) { HKEY hRegKey; if (RegOpenKey(HKEY_CURRENT_USER, "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders", &hRegKey) == ERROR_SUCCESS) { LONG lRet; DWORD lSize = SAL_N_ELEMENTS(PathA); DWORD Type = REG_SZ; switch (nFolder) { case CSIDL_APPDATA: lRet = RegQueryValueEx(hRegKey, "AppData", NULL, &Type, (LPBYTE)PathA, &lSize); break; case CSIDL_PERSONAL: lRet = RegQueryValueEx(hRegKey, "Personal", NULL, &Type, (LPBYTE)PathA, &lSize); break; default: lRet = -1l; } if ((lRet == ERROR_SUCCESS) && (Type == REG_SZ)) { if (_access(PathA, 0) < 0) CreateDirectory(PathA, NULL); hr = pSHGetSpecialFolderLocation(GetActiveWindow(), nFolder, &pidl); } RegCloseKey(hRegKey); } } if (SUCCEEDED(hr)) { if (pSHGetPathFromIDListW && pSHGetPathFromIDListW(pidl, PathW)) { /* if directory does not exist, create it */ if (_waccess(PathW, 0) < 0) CreateDirectoryW(PathW, NULL); rtl_uString_newFromStr( strPath, PathW); bRet = sal_True; } else if (pSHGetPathFromIDListA && pSHGetPathFromIDListA(pidl, PathA)) { /* if directory does not exist, create it */ if (_access(PathA, 0) < 0) CreateDirectoryA(PathA, NULL); rtl_string2UString( strPath, PathA, (sal_Int32) strlen(PathA), osl_getThreadTextEncoding(), OUSTRING_TO_OSTRING_CVTFLAGS); OSL_ASSERT(*strPath != NULL); bRet = sal_True; } } if (SUCCEEDED(pSHGetMalloc(&pMalloc))) { pMalloc->lpVtbl->Free(pMalloc, pidl); pMalloc->lpVtbl->Release(pMalloc); } } } } FreeLibrary(hLibrary); return bRet; }
static void test_namespace(void) { static const WCHAR winetestW[] = {'w','i','n','e','t','e','s','t',0}; static const WCHAR backslashW[] = {'\\',0}; static const WCHAR clsidW[] = { ':',':','{','6','4','5','F','F','0','4','0','-','5','0','8','1','-', '1','0','1','B','-','9','F','0','8','-', '0','0','A','A','0','0','2','F','9','5','4','E','}',0}; static WCHAR tempW[MAX_PATH], curW[MAX_PATH]; WCHAR *long_pathW = NULL; HRESULT r; IShellDispatch *sd; Folder *folder; Folder2 *folder2; FolderItem *item; VARIANT var; BSTR title, item_path; int len; r = CoCreateInstance(&CLSID_Shell, NULL, CLSCTX_INPROC_SERVER, &IID_IShellDispatch, (LPVOID*)&sd); if (r == REGDB_E_CLASSNOTREG) /* NT4 */ { win_skip("skipping IShellDispatch tests\n"); return; } ok(SUCCEEDED(r), "CoCreateInstance failed: %08x\n", r); if (FAILED(r)) return; VariantInit(&var); folder = (void*)0xdeadbeef; r = IShellDispatch_NameSpace(sd, var, &folder); ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r); ok(folder == NULL, "expected NULL, got %p\n", folder); V_VT(&var) = VT_I4; V_I4(&var) = -1; folder = (void*)0xdeadbeef; r = IShellDispatch_NameSpace(sd, var, &folder); todo_wine { ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r); ok(folder == NULL, "got %p\n", folder); } V_VT(&var) = VT_I4; V_I4(&var) = ssfPROGRAMFILES; r = IShellDispatch_NameSpace(sd, var, &folder); ok(r == S_OK || broken(r == S_FALSE), /* NT4 */ "IShellDispatch::NameSpace failed: %08x\n", r); if (r == S_OK) { static WCHAR path[MAX_PATH]; if (pSHGetFolderPathW) { r = pSHGetFolderPathW(NULL, CSIDL_PROGRAM_FILES, NULL, SHGFP_TYPE_CURRENT, path); ok(r == S_OK, "SHGetFolderPath failed: %08x\n", r); } r = Folder_get_Title(folder, &title); todo_wine ok(r == S_OK, "Folder::get_Title failed: %08x\n", r); if (r == S_OK) { /* On Win2000-2003 title is equal to program files directory name in HKLM\Software\Microsoft\Windows\CurrentVersion\ProgramFilesDir. On newer Windows it seems constant and is not changed if the program files directory name is changed */ if (pSHGetSpecialFolderLocation && pSHGetNameFromIDList) { LPITEMIDLIST pidl; PWSTR name; r = pSHGetSpecialFolderLocation(NULL, CSIDL_PROGRAM_FILES, &pidl); ok(r == S_OK, "SHGetSpecialFolderLocation failed: %08x\n", r); r = pSHGetNameFromIDList(pidl, SIGDN_NORMALDISPLAY, &name); ok(r == S_OK, "SHGetNameFromIDList failed: %08x\n", r); todo_wine ok(!lstrcmpW(title, name), "expected %s, got %s\n", wine_dbgstr_w(name), wine_dbgstr_w(title)); CoTaskMemFree(name); CoTaskMemFree(pidl); } else if (pSHGetFolderPathW) { WCHAR *p; p = path + lstrlenW(path); while (path < p && *(p - 1) != '\\') p--; ok(!lstrcmpiW(title, p), "expected %s, got %s\n", wine_dbgstr_w(p), wine_dbgstr_w(title)); } else skip("skipping Folder::get_Title test\n"); SysFreeString(title); } r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2); ok(r == S_OK, "Folder::QueryInterface failed: %08x\n", r); if (r == S_OK) { r = Folder2_get_Self(folder2, &item); ok(r == S_OK, "Folder::get_Self failed: %08x\n", r); if (r == S_OK) { r = FolderItem_get_Path(item, &item_path); ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r); if (pSHGetFolderPathW) ok(!lstrcmpiW(item_path, path), "expected %s, got %s\n", wine_dbgstr_w(path), wine_dbgstr_w(item_path)); SysFreeString(item_path); FolderItem_Release(item); } Folder2_Release(folder2); } Folder_Release(folder); } V_VT(&var) = VT_I4; V_I4(&var) = ssfBITBUCKET; r = IShellDispatch_NameSpace(sd, var, &folder); ok(r == S_OK || broken(r == S_FALSE), /* NT4 */ "IShellDispatch::NameSpace failed: %08x\n", r); if (r == S_OK) { r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2); ok(r == S_OK || broken(r == E_NOINTERFACE), /* NT4 */ "Folder::QueryInterface failed: %08x\n", r); if (r == S_OK) { r = Folder2_get_Self(folder2, &item); ok(r == S_OK, "Folder::get_Self failed: %08x\n", r); if (r == S_OK) { r = FolderItem_get_Path(item, &item_path); todo_wine ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r); todo_wine ok(!lstrcmpW(item_path, clsidW), "expected %s, got %s\n", wine_dbgstr_w(clsidW), wine_dbgstr_w(item_path)); SysFreeString(item_path); FolderItem_Release(item); } Folder2_Release(folder2); } Folder_Release(folder); } GetTempPathW(MAX_PATH, tempW); GetCurrentDirectoryW(MAX_PATH, curW); SetCurrentDirectoryW(tempW); CreateDirectoryW(winetestW, NULL); V_VT(&var) = VT_BSTR; V_BSTR(&var) = SysAllocString(winetestW); r = IShellDispatch_NameSpace(sd, var, &folder); ok(r == S_FALSE, "expected S_FALSE, got %08x\n", r); SysFreeString(V_BSTR(&var)); GetFullPathNameW(winetestW, MAX_PATH, tempW, NULL); if (pGetLongPathNameW) { len = pGetLongPathNameW(tempW, NULL, 0); long_pathW = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)); if (long_pathW) pGetLongPathNameW(tempW, long_pathW, len); } V_VT(&var) = VT_BSTR; V_BSTR(&var) = SysAllocString(tempW); r = IShellDispatch_NameSpace(sd, var, &folder); ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r); if (r == S_OK) { r = Folder_get_Title(folder, &title); ok(r == S_OK, "Folder::get_Title failed: %08x\n", r); if (r == S_OK) { ok(!lstrcmpW(title, winetestW), "bad title: %s\n", wine_dbgstr_w(title)); SysFreeString(title); } r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2); ok(r == S_OK || broken(r == E_NOINTERFACE), /* NT4 */ "Folder::QueryInterface failed: %08x\n", r); if (r == S_OK) { r = Folder2_get_Self(folder2, &item); ok(r == S_OK, "Folder::get_Self failed: %08x\n", r); if (r == S_OK) { r = FolderItem_get_Path(item, &item_path); ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r); if (long_pathW) ok(!lstrcmpW(item_path, long_pathW), "expected %s, got %s\n", wine_dbgstr_w(long_pathW), wine_dbgstr_w(item_path)); SysFreeString(item_path); FolderItem_Release(item); } Folder2_Release(folder2); } Folder_Release(folder); } SysFreeString(V_BSTR(&var)); len = lstrlenW(tempW); if (len < MAX_PATH - 1) { lstrcatW(tempW, backslashW); V_VT(&var) = VT_BSTR; V_BSTR(&var) = SysAllocString(tempW); r = IShellDispatch_NameSpace(sd, var, &folder); ok(r == S_OK, "IShellDispatch::NameSpace failed: %08x\n", r); if (r == S_OK) { r = Folder_get_Title(folder, &title); ok(r == S_OK, "Folder::get_Title failed: %08x\n", r); if (r == S_OK) { ok(!lstrcmpW(title, winetestW), "bad title: %s\n", wine_dbgstr_w(title)); SysFreeString(title); } r = Folder_QueryInterface(folder, &IID_Folder2, (LPVOID*)&folder2); ok(r == S_OK || broken(r == E_NOINTERFACE), /* NT4 */ "Folder::QueryInterface failed: %08x\n", r); if (r == S_OK) { r = Folder2_get_Self(folder2, &item); ok(r == S_OK, "Folder::get_Self failed: %08x\n", r); if (r == S_OK) { r = FolderItem_get_Path(item, &item_path); ok(r == S_OK, "FolderItem::get_Path failed: %08x\n", r); if (long_pathW) ok(!lstrcmpW(item_path, long_pathW), "expected %s, got %s\n", wine_dbgstr_w(long_pathW), wine_dbgstr_w(item_path)); SysFreeString(item_path); FolderItem_Release(item); } Folder2_Release(folder2); } Folder_Release(folder); } SysFreeString(V_BSTR(&var)); } HeapFree(GetProcessHeap(), 0, long_pathW); RemoveDirectoryW(winetestW); SetCurrentDirectoryW(curW); IShellDispatch_Release(sd); }