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