- move wine includes to precomp.h
[reactos.git] / reactos / 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 <precomp.h>
23
24 WINE_DEFAULT_DEBUG_CHANNEL(commdlg);
25
26 HINSTANCE COMDLG32_hInstance = 0;
27
28 static DWORD COMDLG32_TlsIndex = TLS_OUT_OF_INDEXES;
29
30 HINSTANCE SHELL32_hInstance = 0;
31 HINSTANCE SHFOLDER_hInstance = 0;
32
33 /* ITEMIDLIST */
34 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILClone) (LPCITEMIDLIST);
35 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILCombine)(LPCITEMIDLIST,LPCITEMIDLIST);
36 LPITEMIDLIST (WINAPI *COMDLG32_PIDL_ILGetNext)(LPITEMIDLIST);
37 BOOL (WINAPI *COMDLG32_PIDL_ILRemoveLastID)(LPCITEMIDLIST);
38 BOOL (WINAPI *COMDLG32_PIDL_ILIsEqual)(LPCITEMIDLIST, LPCITEMIDLIST);
39
40 /* SHELL */
41 LPVOID (WINAPI *COMDLG32_SHAlloc)(DWORD);
42 DWORD (WINAPI *COMDLG32_SHFree)(LPVOID);
43 BOOL (WINAPI *COMDLG32_SHGetFolderPathA)(HWND,int,HANDLE,DWORD,LPSTR);
44 BOOL (WINAPI *COMDLG32_SHGetFolderPathW)(HWND,int,HANDLE,DWORD,LPWSTR);
45
46 /***********************************************************************
47 * DllMain (COMDLG32.init)
48 *
49 * Initialization code for the COMDLG32 DLL
50 *
51 * RETURNS:
52 * FALSE if sibling could not be loaded or instantiated twice, TRUE
53 * otherwise.
54 */
55 static const char * GPA_string = "Failed to get entry point %s for hinst = 0x%08x\n";
56 #define GPA(dest, hinst, name) \
57 if(!(dest = (void*)GetProcAddress(hinst,name)))\
58 { \
59 ERR((LPSTR)GPA_string, debugstr_a(name), hinst); \
60 return FALSE; \
61 }
62
63 BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD Reason, LPVOID Reserved)
64 {
65 TRACE("(%p, %ld, %p)\n", hInstance, Reason, Reserved);
66
67 switch(Reason)
68 {
69 case DLL_PROCESS_ATTACH:
70 COMDLG32_hInstance = hInstance;
71 DisableThreadLibraryCalls(hInstance);
72
73 SHELL32_hInstance = GetModuleHandleA("SHELL32.DLL");
74
75 if (!SHELL32_hInstance)
76 {
77 ERR("loading of shell32 failed\n");
78 return FALSE;
79 }
80
81 /* ITEMIDLIST */
82 GPA(COMDLG32_PIDL_ILIsEqual, SHELL32_hInstance, (LPCSTR)21L);
83 GPA(COMDLG32_PIDL_ILCombine, SHELL32_hInstance, (LPCSTR)25L);
84 GPA(COMDLG32_PIDL_ILGetNext, SHELL32_hInstance, (LPCSTR)153L);
85 GPA(COMDLG32_PIDL_ILClone, SHELL32_hInstance, (LPCSTR)18L);
86 GPA(COMDLG32_PIDL_ILRemoveLastID, SHELL32_hInstance, (LPCSTR)17L);
87
88 /* SHELL */
89
90 GPA(COMDLG32_SHAlloc, SHELL32_hInstance, (LPCSTR)196L);
91 GPA(COMDLG32_SHFree, SHELL32_hInstance, (LPCSTR)195L);
92 /* for the first versions of shell32 SHGetFolderPathA is in SHFOLDER.DLL */
93 COMDLG32_SHGetFolderPathA = (void*)GetProcAddress(SHELL32_hInstance,"SHGetFolderPathA");
94 if (!COMDLG32_SHGetFolderPathA)
95 {
96 SHFOLDER_hInstance = LoadLibraryA("SHFOLDER.DLL");
97 GPA(COMDLG32_SHGetFolderPathA, SHFOLDER_hInstance,"SHGetFolderPathA");
98 }
99
100 /* for the first versions of shell32 SHGetFolderPathW is in SHFOLDER.DLL */
101 COMDLG32_SHGetFolderPathW = (void*)GetProcAddress(SHELL32_hInstance,"SHGetFolderPathW");
102 if (!COMDLG32_SHGetFolderPathW)
103 {
104 SHFOLDER_hInstance = LoadLibraryA("SHFOLDER.DLL");
105 GPA(COMDLG32_SHGetFolderPathW, SHFOLDER_hInstance,"SHGetFolderPathW");
106 }
107
108 break;
109
110 case DLL_PROCESS_DETACH:
111 if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES) TlsFree(COMDLG32_TlsIndex);
112 if(SHFOLDER_hInstance) FreeLibrary(SHFOLDER_hInstance);
113 break;
114 }
115 return TRUE;
116 }
117 #undef GPA
118
119 /***********************************************************************
120 * COMDLG32_AllocMem (internal)
121 * Get memory for internal datastructure plus stringspace etc.
122 * RETURNS
123 * Success: Pointer to a heap block
124 * Failure: null
125 */
126 LPVOID COMDLG32_AllocMem(
127 int size /* [in] Block size to allocate */
128 ) {
129 LPVOID ptr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, size);
130 if(!ptr)
131 {
132 COMDLG32_SetCommDlgExtendedError(CDERR_MEMALLOCFAILURE);
133 return NULL;
134 }
135 return ptr;
136 }
137
138
139 /***********************************************************************
140 * COMDLG32_SetCommDlgExtendedError (internal)
141 *
142 * Used to set the thread's local error value if a comdlg32 function fails.
143 */
144 void COMDLG32_SetCommDlgExtendedError(DWORD err)
145 {
146 TRACE("(%08lx)\n", err);
147 if (COMDLG32_TlsIndex == TLS_OUT_OF_INDEXES)
148 COMDLG32_TlsIndex = TlsAlloc();
149 if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES)
150 TlsSetValue(COMDLG32_TlsIndex, (LPVOID)(DWORD_PTR)err);
151 else
152 FIXME("No Tls Space\n");
153 }
154
155
156 /***********************************************************************
157 * CommDlgExtendedError (COMDLG32.@)
158 * CommDlgExtendedError (COMMDLG.26)
159 *
160 * Get the thread's local error value if a comdlg32 function fails.
161 * RETURNS
162 * Current error value which might not be valid
163 * if a previous call succeeded.
164 */
165 DWORD WINAPI CommDlgExtendedError(void)
166 {
167 if (COMDLG32_TlsIndex != TLS_OUT_OF_INDEXES)
168 return (DWORD_PTR)TlsGetValue(COMDLG32_TlsIndex);
169 else
170 return 0; /* we never set an error, so there isn't one */
171 }