Don't define COBJMACROS in objbase.h.
[wine] / dlls / shell32 / shlfsbind.c
1 /*
2  * File System Bind Data object to use as parameter for the bind context to
3  * IShellFolder_ParseDisplayName
4  *
5  * Copyright 2003 Rolf Kalbermatter
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 "config.h"
23 #include "wine/port.h"
24
25 #include <stdarg.h>
26
27 #define COBJMACROS
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winreg.h"
32 #include "winuser.h"
33 #include "shlobj.h"
34 #include "shell32_main.h"
35
36 #include "debughlp.h"
37 #include "wine/debug.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL(pidl);
40
41 /***********************************************************************
42  * IFileSystemBindData implementation
43  */
44 typedef struct
45 {
46         IFileSystemBindDataVtbl *lpVtbl;
47         DWORD              ref;
48         WIN32_FIND_DATAW findFile;
49 } IFileSystemBindDataImpl;
50
51 static HRESULT WINAPI IFileSystemBindData_fnQueryInterface(IFileSystemBindData *iface, REFIID riid, LPVOID* ppvObj);
52 static ULONG WINAPI IFileSystemBindData_fnAddRef(IFileSystemBindData *iface);
53 static ULONG WINAPI IFileSystemBindData_fnRelease(IFileSystemBindData *iface);
54 static HRESULT WINAPI IFileSystemBindData_fnGetFindData(IFileSystemBindData *iface, WIN32_FIND_DATAW *pfd);
55 static HRESULT WINAPI IFileSystemBindData_fnSetFindData(IFileSystemBindData *iface, const WIN32_FIND_DATAW *pfd);
56
57 static struct IFileSystemBindDataVtbl sbvt =
58 {
59     IFileSystemBindData_fnQueryInterface,
60     IFileSystemBindData_fnAddRef,
61     IFileSystemBindData_fnRelease,
62     IFileSystemBindData_fnSetFindData,
63     IFileSystemBindData_fnGetFindData,
64 };
65
66 static const WCHAR wFileSystemBindData[] = {'F','i','l','e',' ','S','y','s','t','e','m',' ','B','i','n','d','D','a','t','a',0};
67
68 HRESULT WINAPI IFileSystemBindData_Constructor(const WIN32_FIND_DATAW *pfd, LPBC *ppV)
69 {
70         IFileSystemBindDataImpl *sb;
71         HRESULT ret = E_OUTOFMEMORY;
72
73         TRACE("%p, %p\n", pfd, ppV);
74
75         if (!ppV)
76           return E_INVALIDARG;
77
78         *ppV = NULL;
79
80         sb = (IFileSystemBindDataImpl*)HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IFileSystemBindDataImpl));
81         if (!sb)
82           return ret;
83
84         sb->lpVtbl = &sbvt;
85         sb->ref = 1;
86         IFileSystemBindData_fnSetFindData((IFileSystemBindData*)sb, pfd);
87
88         ret = CreateBindCtx(0, ppV);
89         if (SUCCEEDED(ret))
90         {
91           BIND_OPTS bindOpts;
92           bindOpts.cbStruct = sizeof(BIND_OPTS);
93           bindOpts.grfFlags = 0;
94           bindOpts.grfMode = STGM_CREATE;
95           bindOpts.dwTickCountDeadline = 0;
96           IBindCtx_SetBindOptions(*ppV, &bindOpts);
97           IBindCtx_RegisterObjectParam(*ppV, (LPOLESTR)wFileSystemBindData, (LPUNKNOWN)sb);
98
99           IFileSystemBindData_Release((IFileSystemBindData*)sb);
100         }
101         else
102           HeapFree(GetProcessHeap(), 0, sb);
103         return ret;
104 }
105
106 HRESULT WINAPI FileSystemBindData_GetFindData(LPBC pbc, WIN32_FIND_DATAW *pfd)
107 {
108         LPUNKNOWN pUnk;
109         IFileSystemBindData *pfsbd = NULL;
110         HRESULT ret;
111
112         TRACE("%p, %p\n", pbc, pfd);
113
114         if (!pfd)
115           return E_INVALIDARG;
116
117         ret = IBindCtx_GetObjectParam(pbc, (LPOLESTR)wFileSystemBindData, &pUnk);
118         if (SUCCEEDED(ret))
119         {
120           ret = IUnknown_QueryInterface(pUnk, &IID_IFileSystemBindData, (LPVOID *)&pfsbd);
121           if (SUCCEEDED(ret))
122           {
123             ret = IFileSystemBindData_GetFindData(pfsbd, pfd);
124             IFileSystemBindData_Release(pfsbd);
125           }
126           IUnknown_Release(pUnk);
127         }
128         return ret;
129 }
130
131 HRESULT WINAPI FileSystemBindData_SetFindData(LPBC pbc, const WIN32_FIND_DATAW *pfd)
132 {
133         LPUNKNOWN pUnk;
134         IFileSystemBindData *pfsbd = NULL;
135         HRESULT ret;
136         
137         TRACE("%p, %p\n", pbc, pfd);
138
139         ret = IBindCtx_GetObjectParam(pbc, (LPOLESTR)wFileSystemBindData, &pUnk);
140         if (SUCCEEDED(ret))
141         {
142           ret = IUnknown_QueryInterface(pUnk, &IID_IFileSystemBindData, (LPVOID *)&pfsbd);
143           if (SUCCEEDED(ret))
144           {
145             ret = IFileSystemBindData_SetFindData(pfsbd, pfd);
146             IFileSystemBindData_Release(pfsbd);
147           }
148           IUnknown_Release(pUnk);
149         }
150         return ret;}
151
152
153
154 static HRESULT WINAPI IFileSystemBindData_fnQueryInterface(IFileSystemBindData *iface, REFIID riid, LPVOID *ppV)
155 {
156         IFileSystemBindDataImpl *This = (IFileSystemBindDataImpl *)iface;
157         TRACE("(%p)->(\n\tIID:\t%s, %p)\n", This, debugstr_guid(riid), ppV);
158
159         *ppV = NULL;
160
161         if (IsEqualIID(riid, &IID_IUnknown))
162         {
163           *ppV = This;
164         }
165         else if (IsEqualIID(riid, &IID_IFileSystemBindData))
166         {
167           *ppV = (IFileSystemBindData*)This;
168         }
169
170         if (*ppV)
171         {
172           IUnknown_AddRef((IUnknown*)(*ppV));
173           TRACE("-- Interface: (%p)->(%p)\n", ppV, *ppV);
174           return S_OK;
175         }
176         TRACE("-- Interface: E_NOINTERFACE\n");
177         return E_NOINTERFACE;
178 }
179
180 static ULONG WINAPI IFileSystemBindData_fnAddRef(IFileSystemBindData *iface)
181 {
182         IFileSystemBindDataImpl *This = (IFileSystemBindDataImpl *)iface;
183         TRACE("(%p)\n", This);
184         return InterlockedIncrement(&This->ref);
185 }
186
187 static ULONG WINAPI IFileSystemBindData_fnRelease(IFileSystemBindData *iface)
188 {
189         IFileSystemBindDataImpl *This = (IFileSystemBindDataImpl *)iface;
190         TRACE("(%p)\n", This);
191
192         if (!InterlockedDecrement(&This->ref))
193         {
194           TRACE(" destroying ISFBindPidl(%p)\n",This);
195           HeapFree(GetProcessHeap(), 0, This);
196           return 0;
197         }
198         return This->ref;
199 }
200
201 static HRESULT WINAPI IFileSystemBindData_fnGetFindData(IFileSystemBindData *iface, WIN32_FIND_DATAW *pfd)
202 {
203         IFileSystemBindDataImpl *This = (IFileSystemBindDataImpl *)iface;
204         TRACE("(%p), %p\n", This, pfd);
205
206         if (!pfd)
207           return E_INVALIDARG;
208
209         memcpy(pfd, &This->findFile, sizeof(WIN32_FIND_DATAW));
210         return NOERROR;
211 }
212
213 static HRESULT WINAPI IFileSystemBindData_fnSetFindData(IFileSystemBindData *iface, const WIN32_FIND_DATAW *pfd)
214 {
215         IFileSystemBindDataImpl *This = (IFileSystemBindDataImpl *)iface;
216         TRACE("(%p), %p\n", This, pfd);
217
218         if (pfd)
219           memcpy(&This->findFile, pfd, sizeof(WIN32_FIND_DATAW));
220         else
221           memset(&This->findFile, 0, sizeof(WIN32_FIND_DATAW));
222         return NOERROR;
223 }