oleaut32: Use a saner calling convention for the marshaller asm thunks.
[wine] / dlls / oledb32 / datainit.c
1 /*
2  * Copyright (C) 2012 Alistair Leslie-Hughes
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
17  */
18 #include <stdarg.h>
19
20 #define COBJMACROS
21 #define NONAMELESSUNION
22 #define NONAMELESSSTRUCT
23
24 #include "windef.h"
25 #include "winbase.h"
26 #include "winnls.h"
27 #include "ole2.h"
28 #include "msdasc.h"
29 #include "oledberr.h"
30 #include "initguid.h"
31 #include "oledb_private.h"
32
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35
36 WINE_DEFAULT_DEBUG_CHANNEL(oledb);
37
38 DEFINE_GUID(DBPROPSET_DBINIT, 0xc8b522bc, 0x5cf3, 0x11ce, 0xad, 0xe5, 0x00, 0xaa, 0x00, 0x44, 0x77, 0x3d);
39
40 typedef struct datainit
41 {
42     IDataInitialize IDataInitialize_iface;
43
44     LONG ref;
45 } datainit;
46
47 static inline datainit *impl_from_IDataInitialize(IDataInitialize *iface)
48 {
49     return CONTAINING_RECORD(iface, datainit, IDataInitialize_iface);
50 }
51
52 typedef struct
53 {
54     IDBInitialize IDBInitialize_iface;
55     IDBProperties IDBProperties_iface;
56
57     LONG ref;
58 } dbinit;
59
60 static inline dbinit *impl_from_IDBInitialize(IDBInitialize *iface)
61 {
62     return CONTAINING_RECORD(iface, dbinit, IDBInitialize_iface);
63 }
64
65 static inline dbinit *impl_from_IDBProperties(IDBProperties *iface)
66 {
67     return CONTAINING_RECORD(iface, dbinit, IDBProperties_iface);
68 }
69
70 static HRESULT WINAPI dbprops_QueryInterface(IDBProperties *iface, REFIID riid, void **ppvObject)
71 {
72     dbinit *This = impl_from_IDBProperties(iface);
73
74     return IDBInitialize_QueryInterface(&This->IDBInitialize_iface, riid, ppvObject);
75 }
76
77 static ULONG WINAPI dbprops_AddRef(IDBProperties *iface)
78 {
79     dbinit *This = impl_from_IDBProperties(iface);
80
81     return IDBInitialize_AddRef(&This->IDBInitialize_iface);
82 }
83
84 static ULONG WINAPI dbprops_Release(IDBProperties *iface)
85 {
86     dbinit *This = impl_from_IDBProperties(iface);
87
88     return IDBInitialize_Release(&This->IDBInitialize_iface);
89 }
90
91 static HRESULT WINAPI dbprops_GetProperties(IDBProperties *iface, ULONG cPropertyIDSets,
92             const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertySets, DBPROPSET **prgPropertySets)
93 {
94     dbinit *This = impl_from_IDBProperties(iface);
95
96     FIXME("(%p)->(%d %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertySets, prgPropertySets);
97
98     return E_NOTIMPL;
99 }
100
101 static HRESULT WINAPI dbprops_GetPropertyInfo(IDBProperties *iface, ULONG cPropertyIDSets,
102             const DBPROPIDSET rgPropertyIDSets[], ULONG *pcPropertyInfoSets,
103             DBPROPINFOSET **prgPropertyInfoSets, OLECHAR **ppDescBuffer)
104 {
105     dbinit *This = impl_from_IDBProperties(iface);
106
107     FIXME("(%p)->(%d %p %p %p %p)\n", This, cPropertyIDSets, rgPropertyIDSets, pcPropertyInfoSets,
108                 prgPropertyInfoSets, ppDescBuffer);
109
110     return E_NOTIMPL;
111 }
112
113 static HRESULT WINAPI dbprops_SetProperties(IDBProperties *iface, ULONG cPropertySets,
114             DBPROPSET rgPropertySets[])
115 {
116     dbinit *This = impl_from_IDBProperties(iface);
117
118     FIXME("(%p)->(%d %p)\n", This, cPropertySets, rgPropertySets);
119
120     return E_NOTIMPL;
121 }
122
123 static const struct IDBPropertiesVtbl dbprops_vtbl =
124 {
125     dbprops_QueryInterface,
126     dbprops_AddRef,
127     dbprops_Release,
128     dbprops_GetProperties,
129     dbprops_GetPropertyInfo,
130     dbprops_SetProperties
131 };
132
133 static HRESULT WINAPI dbinit_QueryInterface(IDBInitialize *iface, REFIID riid, void **obj)
134 {
135     dbinit *This = impl_from_IDBInitialize(iface);
136     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
137
138     *obj = NULL;
139
140     if(IsEqualIID(riid, &IID_IUnknown) ||
141        IsEqualIID(riid, &IID_IDBInitialize))
142     {
143         *obj = iface;
144     }
145     else if(IsEqualIID(riid, &IID_IDBProperties))
146     {
147         *obj = &This->IDBProperties_iface;
148     }
149     else
150     {
151         FIXME("interface %s not implemented\n", debugstr_guid(riid));
152         return E_NOINTERFACE;
153     }
154
155     IDBInitialize_AddRef(iface);
156     return S_OK;
157 }
158
159 static ULONG WINAPI dbinit_AddRef(IDBInitialize *iface)
160 {
161     dbinit *This = impl_from_IDBInitialize(iface);
162     TRACE("(%p)\n", This);
163
164     return InterlockedIncrement(&This->ref);
165 }
166
167 static ULONG WINAPI dbinit_Release(IDBInitialize *iface)
168 {
169     dbinit *This = impl_from_IDBInitialize(iface);
170     LONG ref;
171
172     TRACE("(%p)\n", This);
173
174     ref = InterlockedDecrement(&This->ref);
175     if(ref == 0)
176     {
177         HeapFree(GetProcessHeap(), 0, This);
178     }
179
180     return ref;
181 }
182
183 static HRESULT WINAPI dbinit_Initialize(IDBInitialize *iface)
184 {
185     dbinit *This = impl_from_IDBInitialize(iface);
186
187     FIXME("(%p) stub\n", This);
188
189     return S_OK;
190 }
191
192 static HRESULT WINAPI dbinit_Uninitialize(IDBInitialize *iface)
193 {
194     dbinit *This = impl_from_IDBInitialize(iface);
195
196     FIXME("(%p) stub\n", This);
197
198     return S_OK;
199 }
200
201 static const IDBInitializeVtbl dbinit_vtbl =
202 {
203     dbinit_QueryInterface,
204     dbinit_AddRef,
205     dbinit_Release,
206     dbinit_Initialize,
207     dbinit_Uninitialize
208 };
209
210 static HRESULT create_db_init(void **obj)
211 {
212     dbinit *This;
213
214     TRACE("()\n");
215
216     *obj = NULL;
217
218     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
219     if(!This) return E_OUTOFMEMORY;
220
221     This->IDBInitialize_iface.lpVtbl = &dbinit_vtbl;
222     This->IDBProperties_iface.lpVtbl = &dbprops_vtbl;
223     This->ref = 1;
224
225     *obj = &This->IDBInitialize_iface;
226
227     return S_OK;
228 }
229
230 static HRESULT WINAPI datainit_QueryInterface(IDataInitialize *iface, REFIID riid, void **obj)
231 {
232     datainit *This = impl_from_IDataInitialize(iface);
233     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), obj);
234
235     *obj = NULL;
236
237     if(IsEqualIID(riid, &IID_IUnknown) ||
238        IsEqualIID(riid, &IID_IDataInitialize))
239     {
240         *obj = &This->IDataInitialize_iface;
241     }
242     else
243     {
244         FIXME("interface %s not implemented\n", debugstr_guid(riid));
245         return E_NOINTERFACE;
246     }
247
248     IDataInitialize_AddRef(iface);
249     return S_OK;
250 }
251
252 static ULONG WINAPI datainit_AddRef(IDataInitialize *iface)
253 {
254     datainit *This = impl_from_IDataInitialize(iface);
255     TRACE("(%p)\n", This);
256
257     return InterlockedIncrement(&This->ref);
258 }
259
260 static ULONG WINAPI datainit_Release(IDataInitialize *iface)
261 {
262     datainit *This = impl_from_IDataInitialize(iface);
263     LONG ref;
264
265     TRACE("(%p)\n", This);
266
267     ref = InterlockedDecrement(&This->ref);
268     if(ref == 0)
269     {
270         HeapFree(GetProcessHeap(), 0, This);
271     }
272
273     return ref;
274 }
275
276 /*** IDataInitialize methods ***/
277 static HRESULT WINAPI datainit_GetDataSource(IDataInitialize *iface, IUnknown *pUnkOuter, DWORD dwClsCtx,
278                                 LPWSTR pwszInitializationString, REFIID riid, IUnknown **ppDataSource)
279 {
280     datainit *This = impl_from_IDataInitialize(iface);
281
282     FIXME("(%p)->(%p %d %s %s %p)\n", This, pUnkOuter, dwClsCtx, debugstr_w(pwszInitializationString),
283             debugstr_guid(riid), ppDataSource);
284
285     if(IsEqualIID(riid, &IID_IDBInitialize))
286     {
287         return create_db_init( (LPVOID*)ppDataSource);
288     }
289
290     return E_NOTIMPL;
291 }
292
293 /* returns character length of string representation */
294 static int get_propvalue_length(DBPROP *prop)
295 {
296     VARIANT str;
297     HRESULT hr;
298
299     if (V_VT(&prop->vValue) == VT_BSTR) return SysStringLen(V_BSTR(&prop->vValue));
300
301     VariantInit(&str);
302     hr = VariantChangeType(&str, &prop->vValue, 0, VT_BSTR);
303     if (hr == S_OK)
304     {
305         int len = SysStringLen(V_BSTR(&str));
306         VariantClear(&str);
307         return len;
308     }
309
310     return 0;
311 }
312
313 static void write_propvalue_str(WCHAR *str, DBPROP *prop)
314 {
315     VARIANT *v = &prop->vValue;
316     VARIANT vstr;
317     HRESULT hr;
318
319     if (V_VT(v) == VT_BSTR)
320     {
321         strcatW(str, V_BSTR(v));
322         return;
323     }
324
325     VariantInit(&vstr);
326     hr = VariantChangeType(&vstr, v, VARIANT_ALPHABOOL, VT_BSTR);
327     if (hr == S_OK)
328     {
329         strcatW(str, V_BSTR(&vstr));
330         VariantClear(&vstr);
331     }
332 }
333
334 static WCHAR *get_propinfo_descr(DBPROP *prop, DBPROPINFOSET *propinfoset)
335 {
336     int i;
337
338     for (i = 0; i < propinfoset->cPropertyInfos; i++)
339         if (propinfoset->rgPropertyInfos[i].dwPropertyID == prop->dwPropertyID)
340             return propinfoset->rgPropertyInfos[i].pwszDescription;
341
342     return NULL;
343 }
344
345 static void free_dbpropset(ULONG count, DBPROPSET *propset)
346 {
347     int i;
348
349     for (i = 0; i < count; i++)
350     {
351         int p;
352
353         for (p = 0; p < propset[i].cProperties; p++)
354             VariantClear(&propset[i].rgProperties[p].vValue);
355
356         CoTaskMemFree(propset[i].rgProperties);
357     }
358     CoTaskMemFree(propset);
359 }
360
361 static void free_dbpropinfoset(ULONG count, DBPROPINFOSET *propinfoset)
362 {
363     int i;
364
365     for (i = 0; i < count; i++)
366     {
367         int p;
368
369         for (p = 0; p < propinfoset[i].cPropertyInfos; p++)
370             VariantClear(&propinfoset[i].rgPropertyInfos[p].vValues);
371
372         CoTaskMemFree(propinfoset[i].rgPropertyInfos);
373     }
374     CoTaskMemFree(propinfoset);
375 }
376
377 static HRESULT WINAPI datainit_GetInitializationString(IDataInitialize *iface, IUnknown *datasource,
378                                 boolean include_pass, LPWSTR *init_string)
379 {
380     static const WCHAR provW[] = {'P','r','o','v','i','d','e','r','=',0};
381     static const WCHAR colW[] = {';',0};
382     datainit *This = impl_from_IDataInitialize(iface);
383     DBPROPINFOSET *propinfoset;
384     IDBProperties *props;
385     DBPROPIDSET propidset;
386     ULONG count, infocount;
387     WCHAR *progid, *desc;
388     DBPROPSET *propset;
389     IPersist *persist;
390     HRESULT hr;
391     CLSID clsid;
392     int i, len;
393
394     TRACE("(%p)->(%p %d %p)\n", This, datasource, include_pass, init_string);
395
396     /* IPersist support is mandatory for data sources */
397     hr = IUnknown_QueryInterface(datasource, &IID_IPersist, (void**)&persist);
398     if (FAILED(hr)) return hr;
399
400     memset(&clsid, 0, sizeof(clsid));
401     hr = IPersist_GetClassID(persist, &clsid);
402     IPersist_Release(persist);
403     if (FAILED(hr)) return hr;
404
405     progid = NULL;
406     ProgIDFromCLSID(&clsid, &progid);
407     TRACE("clsid=%s, progid=%s\n", debugstr_guid(&clsid), debugstr_w(progid));
408
409     /* now get initialization properties */
410     hr = IUnknown_QueryInterface(datasource, &IID_IDBProperties, (void**)&props);
411     if (FAILED(hr))
412     {
413         WARN("IDBProperties not supported\n");
414         CoTaskMemFree(progid);
415         return hr;
416     }
417
418     propidset.rgPropertyIDs = NULL;
419     propidset.cPropertyIDs = 0;
420     propidset.guidPropertySet = DBPROPSET_DBINIT;
421     propset = NULL;
422     count = 0;
423     hr = IDBProperties_GetProperties(props, 1, &propidset, &count, &propset);
424     if (FAILED(hr))
425     {
426         WARN("failed to get data source properties, 0x%08x\n", hr);
427         CoTaskMemFree(progid);
428         return hr;
429     }
430
431     infocount = 0;
432     IDBProperties_GetPropertyInfo(props, 1, &propidset, &infocount, &propinfoset, &desc);
433     IDBProperties_Release(props);
434
435     /* check if we need to skip password */
436     len = strlenW(progid) + strlenW(provW) + 1; /* including ';' */
437     for (i = 0; i < count; i++)
438     {
439         WCHAR *descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
440         if (descr)
441         {
442             /* include '=' and ';' */
443             len += strlenW(descr) + 2;
444             len += get_propvalue_length(&propset->rgProperties[i]);
445         }
446
447         if ((propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PERSIST_SENSITIVE_AUTHINFO) &&
448             (V_BOOL(&propset->rgProperties[i].vValue) == VARIANT_FALSE))
449            include_pass = FALSE;
450     }
451
452     len *= sizeof(WCHAR);
453     *init_string = CoTaskMemAlloc(len);
454     *init_string[0] = 0;
455
456     /* provider name */
457     strcatW(*init_string, provW);
458     strcatW(*init_string, progid);
459     strcatW(*init_string, colW);
460     CoTaskMemFree(progid);
461
462     for (i = 0; i < count; i++)
463     {
464         WCHAR *descr;
465
466         if (!include_pass && propset->rgProperties[i].dwPropertyID == DBPROP_AUTH_PASSWORD) continue;
467
468         descr = get_propinfo_descr(&propset->rgProperties[i], propinfoset);
469         if (descr)
470         {
471             static const WCHAR eqW[] = {'=',0};
472             strcatW(*init_string, descr);
473             strcatW(*init_string, eqW);
474             write_propvalue_str(*init_string, &propset->rgProperties[i]);
475             strcatW(*init_string, colW);
476         }
477     }
478
479     free_dbpropset(count, propset);
480     free_dbpropinfoset(infocount, propinfoset);
481     CoTaskMemFree(desc);
482
483     if (!include_pass)
484         TRACE("%s\n", debugstr_w(*init_string));
485     return S_OK;
486 }
487
488 static HRESULT WINAPI datainit_CreateDBInstance(IDataInitialize *iface, REFCLSID provider,
489                                 IUnknown *outer, DWORD clsctx, LPWSTR reserved, REFIID riid,
490                                 IUnknown **datasource)
491 {
492     datainit *This = impl_from_IDataInitialize(iface);
493
494     TRACE("(%p)->(%s %p 0x%08x %s %s %p)\n", This, debugstr_guid(provider), outer, clsctx, debugstr_w(reserved),
495         debugstr_guid(riid), datasource);
496
497     return CoCreateInstance(provider, outer, clsctx, riid, (void**)datasource);
498 }
499
500 static HRESULT WINAPI datainit_RemoteCreateDBInstanceEx(IDataInitialize *iface, REFCLSID clsidProvider,
501                                 IUnknown *pUnkOuter, DWORD dwClsCtx, LPWSTR pwszReserved, COSERVERINFO *pServerInfo,
502                                 DWORD cmq, GUID **rgpIID, IUnknown **rgpItf, HRESULT *rghr)
503 {
504     datainit *This = impl_from_IDataInitialize(iface);
505
506     FIXME("(%p)->()\n", This);
507
508     return E_NOTIMPL;
509 }
510
511 static HRESULT WINAPI datainit_LoadStringFromStorage(IDataInitialize *iface, LPWSTR pwszFileName,
512                                 LPWSTR *ppwszInitializationString)
513 {
514     datainit *This = impl_from_IDataInitialize(iface);
515
516     FIXME("(%p)->(%s %p)\n", This, debugstr_w(pwszFileName), ppwszInitializationString);
517
518     return E_NOTIMPL;
519 }
520
521 static HRESULT WINAPI datainit_WriteStringToStorage(IDataInitialize *iface, LPWSTR pwszFileName,
522                                 LPWSTR pwszInitializationString, DWORD dwCreationDisposition)
523 {
524     datainit *This = impl_from_IDataInitialize(iface);
525
526     FIXME("(%p)->(%s %s %d)\n", This, debugstr_w(pwszFileName), debugstr_w(pwszInitializationString), dwCreationDisposition);
527
528     return E_NOTIMPL;
529 }
530
531
532 static const struct IDataInitializeVtbl datainit_vtbl =
533 {
534     datainit_QueryInterface,
535     datainit_AddRef,
536     datainit_Release,
537     datainit_GetDataSource,
538     datainit_GetInitializationString,
539     datainit_CreateDBInstance,
540     datainit_RemoteCreateDBInstanceEx,
541     datainit_LoadStringFromStorage,
542     datainit_WriteStringToStorage
543 };
544
545 HRESULT create_data_init(IUnknown *outer, void **obj)
546 {
547     datainit *This;
548
549     TRACE("(%p)\n", obj);
550
551     if(outer) return CLASS_E_NOAGGREGATION;
552
553     *obj = NULL;
554
555     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
556     if(!This) return E_OUTOFMEMORY;
557
558     This->IDataInitialize_iface.lpVtbl = &datainit_vtbl;
559     This->ref = 1;
560
561     *obj = &This->IDataInitialize_iface;
562
563     return S_OK;
564 }