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