Make ole32 a delay-load import of shell32.
[wine] / dlls / shell32 / debughlp.c
1 /*
2  * Helper functions for debugging
3  *
4  * Copyright 1998, 2002 Juergen Schmied
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  */
20
21 #include <ctype.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include "windef.h"
26 #include "wingdi.h"
27 #include "pidl.h"
28 #include "shlguid.h"
29 #include "shldisp.h"
30 #include "wine/debug.h"
31 #include "debughlp.h"
32 #include "docobj.h"
33 #include "shell32_main.h"
34
35
36 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
37
38 LPITEMIDLIST _dbg_ILGetNext(LPCITEMIDLIST pidl)
39 {
40         WORD len;
41
42         if(pidl)
43         {
44           len =  pidl->mkid.cb;
45           if (len)
46           {
47             return (LPITEMIDLIST) (((LPBYTE)pidl)+len);
48           }
49         }
50         return NULL;
51 }
52
53 BOOL _dbg_ILIsDesktop(LPCITEMIDLIST pidl)
54 {
55         return ( !pidl || (pidl && pidl->mkid.cb == 0x00) );
56 }
57
58 LPPIDLDATA _dbg_ILGetDataPointer(LPCITEMIDLIST pidl)
59 {
60         if(pidl && pidl->mkid.cb != 0x00)
61           return (LPPIDLDATA) &(pidl->mkid.abID);
62         return NULL;
63 }
64
65 LPSTR _dbg_ILGetTextPointer(LPCITEMIDLIST pidl)
66 {
67         LPPIDLDATA pdata =_dbg_ILGetDataPointer(pidl);
68
69         if (pdata)
70         {
71           switch (pdata->type)
72           {
73             case PT_GUID:
74             case PT_SHELLEXT:
75               return NULL;
76
77             case PT_DRIVE:
78             case PT_DRIVE1:
79             case PT_DRIVE2:
80             case PT_DRIVE3:
81               return (LPSTR)&(pdata->u.drive.szDriveName);
82
83             case PT_FOLDER:
84             case PT_FOLDER1:
85             case PT_VALUE:
86             case PT_IESPECIAL1:
87             case PT_RAS_FOLDER:
88             case PT_IESPECIAL2:
89               return (LPSTR)&(pdata->u.file.szNames);
90
91             case PT_WORKGRP:
92             case PT_COMP:
93             case PT_NETWORK:
94             case PT_NETPROVIDER:
95             case PT_SHARE:
96               return (LPSTR)&(pdata->u.network.szNames);
97           }
98         }
99         return NULL;
100 }
101
102 LPSTR _dbg_ILGetSTextPointer(LPCITEMIDLIST pidl)
103 {
104         LPPIDLDATA pdata =_dbg_ILGetDataPointer(pidl);
105
106         if (pdata)
107         {
108           switch (pdata->type)
109           {
110             case PT_FOLDER:
111             case PT_VALUE:
112             case PT_IESPECIAL1:
113             case PT_RAS_FOLDER:
114             case PT_IESPECIAL2:
115               return (LPSTR)(pdata->u.file.szNames + strlen (pdata->u.file.szNames) + 1);
116
117             case PT_WORKGRP:
118               return (LPSTR)(pdata->u.network.szNames + strlen (pdata->u.network.szNames) + 1);
119           }
120         }
121         return NULL;
122 }
123
124 REFIID _dbg_ILGetGUIDPointer(LPCITEMIDLIST pidl)
125 {
126         LPPIDLDATA pdata =_ILGetDataPointer(pidl);
127
128         if (pdata)
129         {
130           switch (pdata->type)
131           {
132             case PT_SHELLEXT:
133             case PT_GUID:
134               return (REFIID) &(pdata->u.guid.guid);
135           }
136         }
137         return NULL;
138 }
139
140 DWORD _dbg_ILSimpleGetText (LPCITEMIDLIST pidl, LPSTR szOut, UINT uOutSize)
141 {
142         DWORD           dwReturn=0;
143         LPSTR           szSrc;
144         GUID const *    riid;
145         char szTemp[MAX_PATH];
146
147         if (!pidl) return 0;
148
149         if (szOut)
150           *szOut = 0;
151
152         if (_dbg_ILIsDesktop(pidl))
153         {
154          /* desktop */
155           if (szOut) strncpy(szOut, "Desktop", uOutSize);
156           dwReturn = strlen ("Desktop");
157         }
158         else if (( szSrc = _dbg_ILGetTextPointer(pidl) ))
159         {
160           /* filesystem */
161           if (szOut) strncpy(szOut, szSrc, uOutSize);
162           dwReturn = strlen(szSrc);
163         }
164         else if (( riid = _dbg_ILGetGUIDPointer(pidl) ))
165         {
166           if (szOut)
167             sprintf( szOut, "{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x}",
168                  riid->Data1, riid->Data2, riid->Data3,
169                  riid->Data4[0], riid->Data4[1], riid->Data4[2], riid->Data4[3],
170                  riid->Data4[4], riid->Data4[5], riid->Data4[6], riid->Data4[7] );
171           dwReturn = strlen (szTemp);
172         }
173         return dwReturn;
174 }
175
176
177
178
179 void pdump (LPCITEMIDLIST pidl)
180 {
181         LPCITEMIDLIST pidltemp = pidl;
182
183         if (!TRACE_ON(pidl)) return;
184
185         if (! pidltemp)
186         {
187           MESSAGE ("-------- pidl=NULL (Desktop)\n");
188         }
189         else
190         {
191           MESSAGE ("-------- pidl=%p\n", pidl);
192           if (pidltemp->mkid.cb)
193           {
194             do
195             {
196               DWORD dwAttrib = 0;
197               LPPIDLDATA pData   = _dbg_ILGetDataPointer(pidltemp);
198               DWORD type         = pData->type;
199               LPSTR szLongName   = _dbg_ILGetTextPointer(pidltemp);
200               LPSTR szShortName  = _dbg_ILGetSTextPointer(pidltemp);
201               char szName[MAX_PATH];
202
203               _dbg_ILSimpleGetText(pidltemp, szName, MAX_PATH);
204               if( PT_FOLDER == type || PT_VALUE == type)
205                 dwAttrib = pData->u.file.uFileAttribs;
206
207               MESSAGE ("[%p] size=%04u type=%lx attr=0x%08lx name=\"%s\" (%s,%s)\n",
208                        pidltemp, pidltemp->mkid.cb,type,dwAttrib,szName,debugstr_a(szLongName), debugstr_a(szShortName));
209
210               pidltemp = _dbg_ILGetNext(pidltemp);
211
212             } while (pidltemp->mkid.cb);
213           }
214           else
215           {
216             MESSAGE ("empty pidl (Desktop)\n");
217           }
218           pcheck(pidl);
219         }
220 }
221 #define BYTES_PRINTED 32
222 BOOL pcheck (LPCITEMIDLIST pidl)
223 {
224         DWORD type, ret=TRUE;
225         LPCITEMIDLIST pidltemp = pidl;
226
227         if (pidltemp && pidltemp->mkid.cb)
228         { do
229           { type   = _dbg_ILGetDataPointer(pidltemp)->type;
230             switch (type)
231             { case PT_CPLAPPLET:
232               case PT_GUID:
233               case PT_SHELLEXT:
234               case PT_DRIVE:
235               case PT_DRIVE1:
236               case PT_DRIVE2:
237               case PT_DRIVE3:
238               case PT_FOLDER:
239               case PT_VALUE:
240               case PT_FOLDER1:
241               case PT_WORKGRP:
242               case PT_COMP:
243               case PT_NETPROVIDER:
244               case PT_NETWORK:
245               case PT_IESPECIAL1:
246               case PT_RAS_FOLDER:
247               case PT_IESPECIAL2:
248               case PT_SHARE:
249                 break;
250               default:
251               {
252                 char szTemp[BYTES_PRINTED*4 + 1];
253                 int i;
254                 unsigned char c;
255
256                 memset(szTemp, ' ', BYTES_PRINTED*4 + 1);
257                 for ( i = 0; (i<pidltemp->mkid.cb) && (i<BYTES_PRINTED); i++)
258                 {
259                   c = ((unsigned char *)pidltemp)[i];
260
261                   szTemp[i*3+0] = ((c>>4)>9)? (c>>4)+55 : (c>>4)+48;
262                   szTemp[i*3+1] = ((0x0F&c)>9)? (0x0F&c)+55 : (0x0F&c)+48;
263                   szTemp[i*3+2] = ' ';
264                   szTemp[i+BYTES_PRINTED*3]  =  (c>=0x20 && c <=0x80) ? c : '.';
265                 }
266                 szTemp[BYTES_PRINTED*4] = 0x00;
267                 ERR("unknown IDLIST %p [%p] size=%u type=%lx\n%s\n",pidl, pidltemp, pidltemp->mkid.cb,type, szTemp);
268                 ret = FALSE;
269               }
270             }
271             pidltemp = _dbg_ILGetNext(pidltemp);
272           } while (pidltemp->mkid.cb);
273         }
274         return ret;
275 }
276
277 static char shdebugstr_buf1[100];
278 static char shdebugstr_buf2[100];
279 static char * shdebugstr_buf = shdebugstr_buf1;
280
281 static struct {
282         REFIID  riid;
283         char    *name;
284 } InterfaceDesc[] = {
285         {&IID_IUnknown,                 "IID_IUnknown"},
286         {&IID_IClassFactory,            "IID_IClassFactory"},
287         {&IID_IShellView,               "IID_IShellView"},
288         {&IID_IOleCommandTarget,        "IID_IOleCommandTarget"},
289         {&IID_IDropTarget,              "IID_IDropTarget"},
290         {&IID_IDropSource,              "IID_IDropSource"},
291         {&IID_IViewObject,              "IID_IViewObject"},
292         {&IID_IContextMenu,             "IID_IContextMenu"},
293         {&IID_IShellExtInit,            "IID_IShellExtInit"},
294         {&IID_IShellFolder,             "IID_IShellFolder"},
295         {&IID_IShellFolder2,            "IID_IShellFolder2"},
296         {&IID_IPersist,                 "IID_IPersist"},
297         {&IID_IPersistFolder,           "IID_IPersistFolder"},
298         {&IID_IPersistFolder2,          "IID_IPersistFolder2"},
299         {&IID_IPersistFolder3,          "IID_IPersistFolder3"},
300         {&IID_IExtractIconA,            "IID_IExtractIconA"},
301         {&IID_IExtractIconW,            "IID_IExtractIconW"},
302         {&IID_IDataObject,              "IID_IDataObject"},
303         {&IID_IAutoComplete,            "IID_IAutoComplete"},
304         {&IID_IAutoComplete2,           "IID_IAutoComplete2"},
305         {NULL,NULL}};
306
307 const char * shdebugstr_guid( const struct _GUID *id )
308 {
309         int i;
310         char* name = NULL;
311         char clsidbuf[100];
312
313         shdebugstr_buf = (shdebugstr_buf == shdebugstr_buf1) ? shdebugstr_buf2 : shdebugstr_buf1;
314
315         if (!id) {
316           strcpy (shdebugstr_buf, "(null)");
317         } else {
318             for (i=0;InterfaceDesc[i].riid && !name;i++) {
319                 if (IsEqualIID(InterfaceDesc[i].riid, id)) name = InterfaceDesc[i].name;
320             }
321             if (!name) {
322                 if (HCR_GetClassNameA(id, clsidbuf, 100))
323                     name = clsidbuf;
324             }
325
326             sprintf( shdebugstr_buf, "\n\t{%08lx-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x} (%s)",
327                  id->Data1, id->Data2, id->Data3,
328                  id->Data4[0], id->Data4[1], id->Data4[2], id->Data4[3],
329                  id->Data4[4], id->Data4[5], id->Data4[6], id->Data4[7], name ? name : "unknown" );
330         }
331         return shdebugstr_buf;
332 }