2a1a621411fbb93a16cbb9596192db43432c1af8
[reactos.git] / dll / win32 / comdlg32 / cdlg32.c
1 /*
2 * Common Dialog Boxes interface (32 bit)
3 * Find/Replace
4 *
5 * Copyright 1999 Bertho A. Stultiens
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 */
21
22 #include "cdlg.h"
23
24 DECLSPEC_HIDDEN HINSTANCE COMDLG32_hInstance = 0;
25
26 static DWORD COMDLG32_TlsIndex = TLS_OUT_OF_INDEXES;
27
28 static HINSTANCE SHELL32_hInstance;
29 static HINSTANCE SHFOLDER_hInstance;
30
31 /* ITEMIDLIST */
32 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILClone) (LPCITEMIDLIST) DECLSPEC_HIDDEN;
33 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILCombine)(LPCITEMIDLIST,LPCITEMIDLIST) DECLSPEC_HIDDEN;
34 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILGetNext)(LPITEMIDLIST) DECLSPEC_HIDDEN;
35 BOOL (WINAPI *COMDLG32_PIDL_ILRemoveLastID)(LPCITEMIDLIST) DECLSPEC_HIDDEN;
36 BOOL (WINAPI *COMDLG32_PIDL_ILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST) DECLSPEC_HIDDEN;
37 UINT (WINAPI *COMDLG32_PIDL_ILGetSize)(LPCITEMIDLIST) DECLSPEC_HIDDEN;
38
39 /* SHELL */
40 LPVOID (WINAPI *COMDLG32_SHAlloc)(DWORD) DECLSPEC_HIDDEN;
41 DWORD (WINAPI *COMDLG32_SHFree)(LPVOID) DECLSPEC_HIDDEN;
42 BOOL (WINAPI *COMDLG32_SHGetFolderPathW)(HWND,int,HANDLE,DWORD,LPWSTR) DECLSPEC_HIDDEN;
43 LPITEMIDLIST (WINAPI *COMDLG32_SHSimpleIDListFromPathAW)(LPCVOID) DECLSPEC_HIDDEN;
44
45 /***********************************************************************
46 * DllMain (COMDLG32.init)
47 *
48 * Initialization code for the COMDLG32 DLL
49 *
50 * RETURNS:
51 * FALSE if sibling could not be loaded or instantiated twice, TRUE
52 * otherwise.
53 */
54 static const char GPA_string[] = "Failed to get entry point %s for hinst = %p\n";
55 #define GPA(dest, hinst, name) \
56 if(!(dest = (void*)GetProcAddress(hinst,name)))\
57 { \
58 ERR(GPA_string, debugstr_a(name), hinst); \
59 return FALSE; \
60 }
61
62 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD Reason, LPVOID Reserved)
63 {
64 TRACE("(%p, %d, %p)\n", hInstance, Reason, Reserved);
65
66 switch(Reason)
67 {
68 case DLL_PROCESS_ATTACH:
69 COMDLG32_hInstance = hInstance;
70 DisableThreadLibraryCalls(hInstance);
71
72 SHELL32_hInstance = GetModuleHandleA("SHELL32.DLL");
73
74 /* ITEMIDLIST */
75 GPA(COMDLG32_PIDL_ILIsEqual, SHELL32_hInstance, (LPCSTR)21L);
76 GPA(COMDLG32_PIDL_ILCombine, SHELL32_hInstance, (LPCSTR)25L);
77 GPA(COMDLG32_PIDL_ILGetNext, SHELL32_hInstance, (LPCSTR)153L);
78 GPA(COMDLG32_PIDL_ILClone, SHELL32_hInstance, (LPCSTR)18L);
79 GPA(COMDLG32_PIDL_ILRemoveLastID, SHELL32_hInstance, (LPCSTR)17L);
80 GPA(COMDLG32_PIDL_ILGetSize, SHELL32_hInstance, (LPCSTR)152L);
81
82 /* SHELL */
83 GPA(COMDLG32_SHSimpleIDListFromPathAW, SHELL32_hInstance, (LPCSTR)162);
84 GPA(COMDLG32_SHAlloc, SHELL32_hInstance, (LPCSTR)196L);
85 GPA(COMDLG32_SHFree, SHELL32_hInstance, (LPCSTR)195L);
86
87 /* for the first versions of shell32 SHGetFolderPathW is in SHFOLDER.DLL */
88 COMDLG32_SHGetFolderPathW = (void*)GetProcAddress(SHELL32_hInstance,"SHGetFolderPathW");
89 if (!COMDLG32_SHGetFolderPathW)
90 {
91 SHFOLDER_hInstance = LoadLibraryA("SHFOLDER.DLL");
92 GPA(COMDLG32_SHGetFolderPathW, SHFOLDER_hInstance,"SHGetFolderPathW");
93 }
94
95 break;
96
97 case DLL_PROCESS_DETACH:
98 if (Reserved) break;
99 if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES) TlsFree(COMDLG32_TlsIndex);
100 if(SHFOLDER_hInstance) FreeLibrary(SHFOLDER_hInstance);
101 break;
102 }
103 return TRUE;
104 }
105 #undef GPA
106
107 /***********************************************************************
108 * COMDLG32_AllocMem (internal)
109 * Get memory for internal datastructure plus stringspace etc.
110 * RETURNS
111 * Success: Pointer to a heap block
112 * Failure: null
113 */
114 LPVOID COMDLG32_AllocMem(
115 int size /* [in] Block size to allocate */
116 ) {
117 LPVOID ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
118 if(!ptr)
119 {
120 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
121 return NULL;
122 }
123 return ptr;
124 }
125
126
127 /***********************************************************************
128 * COMDLG32_SetCommDlgExtendedError (internal)
129 *
130 * Used to set the thread's local error value if a comdlg32 function fails.
131 */
132 void COMDLG32_SetCommDlgExtendedError(DWORD err)
133 {
134 TRACE("(%08x)\n", err);
135 if (COMDLG32_TlsIndex == TLS_OUT_OF_INDEXES)
136 COMDLG32_TlsIndex = TlsAlloc();
137 if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES)
138 TlsSetValue(COMDLG32_TlsIndex, (LPVOID)(DWORD_PTR)err);
139 else
140 FIXME("No Tls Space\n");
141 }
142
143
144 /***********************************************************************
145 * CommDlgExtendedError (COMDLG32.@)
146 *
147 * Get the thread's local error value if a comdlg32 function fails.
148 * RETURNS
149 * Current error value which might not be valid
150 * if a previous call succeeded.
151 */
152 DWORD WINAPI CommDlgExtendedError(void)
153 {
154 if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES)
155 return (DWORD_PTR)TlsGetValue(COMDLG32_TlsIndex);
156 else
157 return 0; /* we never set an error, so there isn't one */
158 }
159
160 #ifndef __REACTOS__ /* Win 7 */
161
162 /*************************************************************************
163 * Implement the CommDlg32 class factory
164 *
165 * (Taken from shdocvw/factory.c; based on implementation in
166 * ddraw/main.c)
167 */
168 typedef struct
169 {
170 IClassFactory IClassFactory_iface;
171 HRESULT (*cf)(IUnknown*, REFIID, void**);
172 } IClassFactoryImpl;
173
174 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
175 {
176 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
177 }
178
179 /*************************************************************************
180 * CDLGCF_QueryInterface (IUnknown)
181 */
182 static HRESULT WINAPI CDLGCF_QueryInterface(IClassFactory* iface,
183 REFIID riid, void **ppobj)
184 {
185 TRACE("%p (%s %p)\n", iface, debugstr_guid(riid), ppobj);
186
187 if(!ppobj)
188 return E_POINTER;
189
190 if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IClassFactory, riid))
191 {
192 *ppobj = iface;
193 IClassFactory_AddRef(iface);
194 return S_OK;
195 }
196
197 WARN("Interface not supported.\n");
198
199 *ppobj = NULL;
200 return E_NOINTERFACE;
201 }
202
203 /*************************************************************************
204 * CDLGCF_AddRef (IUnknown)
205 */
206 static ULONG WINAPI CDLGCF_AddRef(IClassFactory *iface)
207 {
208 return 2; /* non-heap based object */
209 }
210
211 /*************************************************************************
212 * CDLGCF_Release (IUnknown)
213 */
214 static ULONG WINAPI CDLGCF_Release(IClassFactory *iface)
215 {
216 return 1; /* non-heap based object */
217 }
218
219 /*************************************************************************
220 * CDLGCF_CreateInstance (IClassFactory)
221 */
222 static HRESULT WINAPI CDLGCF_CreateInstance(IClassFactory *iface, IUnknown *pOuter,
223 REFIID riid, void **ppobj)
224 {
225 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
226 return This->cf(pOuter, riid, ppobj);
227 }
228
229 /*************************************************************************
230 * CDLGCF_LockServer (IClassFactory)
231 */
232 static HRESULT WINAPI CDLGCF_LockServer(IClassFactory *iface, BOOL dolock)
233 {
234 TRACE("%p (%d)\n", iface, dolock);
235 return S_OK;
236 }
237
238 static const IClassFactoryVtbl CDLGCF_Vtbl =
239 {
240 CDLGCF_QueryInterface,
241 CDLGCF_AddRef,
242 CDLGCF_Release,
243 CDLGCF_CreateInstance,
244 CDLGCF_LockServer
245 };
246
247 /*************************************************************************
248 * DllGetClassObject (COMMDLG32.@)
249 */
250 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, void **ppv)
251 {
252 static IClassFactoryImpl FileOpenDlgClassFactory = {{&CDLGCF_Vtbl}, FileOpenDialog_Constructor};
253 static IClassFactoryImpl FileSaveDlgClassFactory = {{&CDLGCF_Vtbl}, FileSaveDialog_Constructor};
254
255 TRACE("%s, %s, %p\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
256
257 if(IsEqualGUID(&CLSID_FileOpenDialog, rclsid))
258 return IClassFactory_QueryInterface(&FileOpenDlgClassFactory.IClassFactory_iface, riid, ppv);
259
260 if(IsEqualGUID(&CLSID_FileSaveDialog, rclsid))
261 return IClassFactory_QueryInterface(&FileSaveDlgClassFactory.IClassFactory_iface, riid, ppv);
262
263 return CLASS_E_CLASSNOTAVAILABLE;
264 }
265
266 /***********************************************************************
267 * DllRegisterServer (COMMDLG32.@)
268 */
269 HRESULT WINAPI DllRegisterServer(void)
270 {
271 #ifdef __REACTOS__
272 return E_FAIL; // FIXME: __wine_register_resources(COMDLG32_hInstance);
273 #else
274 return __wine_register_resources(COMDLG32_hInstance);
275 #endif
276 }
277
278 /***********************************************************************
279 * DllUnregisterServer (COMMDLG32.@)
280 */
281 HRESULT WINAPI DllUnregisterServer(void)
282 {
283 #ifdef __REACTOS__
284 return E_FAIL; // FIXME: __wine_unregister_resources(COMDLG32_hInstance);
285 #else
286 return __wine_unregister_resources(COMDLG32_hInstance);
287 #endif
288 }
289
290 #endif /* Win 7 */