1 /* 2 * Copyright 2003 Martin Fuchs 3 * 4 * This library is free software; you can redistribute it and/or 5 * modify it under the terms of the GNU Lesser General Public 6 * License as published by the Free Software Foundation; either 7 * version 2.1 of the License, or (at your option) any later version. 8 * 9 * This library is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 12 * Lesser General Public License for more details. 13 * 14 * You should have received a copy of the GNU Lesser General Public 15 * License along with this library; if not, write to the Free Software 16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 17 */ 18 19 20 // 21 // shelltests.cpp 22 // 23 // Examples for usage of shellclasses.cpp, shellclasses.h 24 // 25 // Martin Fuchs, 20.07.2003 26 // 27 28 29 //#define WIN32_LEAN_AND_MEAN 30 //#define WIN32_EXTRA_LEAN 31 //#include <windows.h> 32 33 #include "utility.h" // for String 34 #include "shellclasses.h" 35 36 37 static void dump_shell_namespace(ShellFolder& folder) 38 { 39 ShellItemEnumerator enumerator(folder, SHCONTF_FOLDERS|SHCONTF_NONFOLDERS|SHCONTF_INCLUDEHIDDEN|SHCONTF_SHAREABLE|SHCONTF_STORAGE); 40 41 LPITEMIDLIST pidl; 42 HRESULT hr = S_OK; 43 44 do { 45 ULONG cnt = 0; 46 47 HRESULT hr = enumerator->Next(1, &pidl, &cnt); 48 49 if (!SUCCEEDED(hr)) 50 break; 51 52 if (hr == S_FALSE) // no more entries? 53 break; 54 55 if (pidl) { 56 ULONG attribs = -1; 57 58 HRESULT hr = folder->GetAttributesOf(1, (LPCITEMIDLIST*)&pidl, &attribs); 59 60 if (SUCCEEDED(hr)) { 61 if (attribs == -1) 62 attribs = 0; 63 64 const String& name = folder.get_name(pidl); 65 66 if (attribs & (SFGAO_FOLDER|SFGAO_HASSUBFOLDER)) 67 cout << "folder: "; 68 else 69 cout << "file: "; 70 71 cout << "\"" << name << "\"\n attribs=" << hex << attribs << endl; 72 } 73 } 74 } while(SUCCEEDED(hr)); 75 } 76 77 78 int main() 79 { 80 // initialize COM 81 ComInit usingCOM; 82 83 84 HWND hwnd = 0; 85 86 87 try { 88 89 // example for retrieval of special folder paths 90 91 SpecialFolderFSPath programs(CSIDL_PROGRAM_FILES, hwnd); 92 SpecialFolderFSPath autostart(CSIDL_STARTUP, hwnd); 93 94 cout << "program files path = " << (LPCTSTR)programs << endl; 95 cout << "autostart folder path = " << (LPCTSTR)autostart << endl; 96 97 cout << endl; 98 99 100 // example for enumerating shell namespace objects 101 102 cout << "Desktop:\n"; 103 dump_shell_namespace(GetDesktopFolder()); 104 cout << endl; 105 106 cout << "C:\\\n"; 107 dump_shell_namespace(ShellPath("C:\\").get_folder()); 108 cout << endl; 109 110 111 // example for calling a browser dialog for the whole desktop 112 113 FolderBrowser desktop_browser(hwnd, 114 BIF_RETURNONLYFSDIRS|BIF_EDITBOX|BIF_NEWDIALOGSTYLE, 115 TEXT("Please select the path:")); 116 117 if (desktop_browser.IsOK()) 118 MessageBox(hwnd, desktop_browser, TEXT("Your selected path"), MB_OK); 119 120 121 // example for calling a rooted browser dialog 122 123 ShellPath browseRoot("C:\\"); 124 FolderBrowser rooted_browser(hwnd, 125 BIF_RETURNONLYFSDIRS|BIF_EDITBOX|BIF_VALIDATE, 126 TEXT("Please select the path:"), 127 browseRoot); 128 129 if (rooted_browser.IsOK()) 130 MessageBox(hwnd, rooted_browser, TEXT("Your selected path"), MB_OK); 131 132 } catch(COMException& e) { 133 134 //HandleException(e, hwnd); 135 cerr << e.ErrorMessage() << endl; 136 137 } 138 139 return 0; 140 } 141