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