widl: Properly align name table entries.
[wine.git] / dlls / ole2disp.dll16 / ole2disp.c
blob8318ae863e51319a1bc252a0e7fe74a75000ee12
1 /*
2 * OLE2DISP library
4 * Copyright 1995 Martin von Loewis
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.
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.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include <stdarg.h>
22 #include <string.h>
24 #include "windef.h"
25 #include "winbase.h"
26 #include "wingdi.h"
27 #include "winuser.h"
28 #include "ole2.h"
29 #include "oleauto.h"
30 #include "winerror.h"
31 #include "wownt32.h"
32 #include "wine/windef16.h"
33 #include "wine/winbase16.h"
35 #include "ole2disp.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41 #define E_OUTOFMEMORY16 MAKE_SCODE(SEVERITY_ERROR, FACILITY_NULL, 2)
42 #define E_INVALIDARG16 MAKE_SCODE(SEVERITY_ERROR, FACILITY_NULL, 3)
44 /* 16-bit SAFEARRAY implementation */
45 typedef struct tagSAFEARRAYBOUND16
47 ULONG cElements;
48 LONG lLbound;
49 } SAFEARRAYBOUND16;
51 typedef struct tagSAFEARRAY16
53 USHORT cDims;
54 USHORT fFeatures;
55 USHORT cbElements;
56 USHORT cLocks;
57 ULONG handle;
58 SEGPTR pvData;
59 SAFEARRAYBOUND16 rgsabound[1];
60 } SAFEARRAY16;
62 static SEGPTR safearray_alloc(ULONG size)
64 HANDLE16 h;
65 return WOWGlobalAllocLock16(GPTR, size, &h);
68 static void safearray_free(SEGPTR ptr)
70 WOWGlobalUnlockFree16(ptr);
73 static ULONG safearray_getcellcount(const SAFEARRAY16 *sa)
75 const SAFEARRAYBOUND16 *sab = sa->rgsabound;
76 USHORT count = sa->cDims;
77 ULONG cells = 1;
79 while (count--)
81 if (!sab->cElements)
82 return 0;
83 cells *= sab->cElements;
84 sab++;
87 return cells;
90 static HRESULT safearray_lock(SAFEARRAY16 *sa)
92 if (sa->cLocks == 0xffff)
93 return E_UNEXPECTED;
95 sa->cLocks++;
96 return S_OK;
99 /******************************************************************************
100 * SafeArrayGetDim [OLE2DISP.17]
102 USHORT WINAPI SafeArrayGetDim16(SAFEARRAY16 *sa)
104 TRACE("(%p)\n", sa);
105 return sa->cDims;
108 /******************************************************************************
109 * SafeArrayGetElemsize [OLE2DISP.18]
111 USHORT WINAPI SafeArrayGetElemsize16(SAFEARRAY16 *sa)
113 TRACE("(%p)\n", sa);
114 return sa->cbElements;
117 /******************************************************************************
118 * SafeArrayGetUBound [OLE2DISP.19]
120 HRESULT WINAPI SafeArrayGetUBound16(SAFEARRAY16 *sa, UINT16 dim, LONG *ubound)
122 TRACE("(%p, %u, %p)\n", sa, dim, ubound);
124 if (!sa)
125 return E_INVALIDARG16;
127 if (!dim || dim > sa->cDims)
128 return DISP_E_BADINDEX;
130 *ubound = sa->rgsabound[sa->cDims - dim].lLbound + sa->rgsabound[sa->cDims - dim].cElements - 1;
132 return S_OK;
135 /******************************************************************************
136 * SafeArrayGetLBound [OLE2DISP.20]
138 HRESULT WINAPI SafeArrayGetLBound16(SAFEARRAY16 *sa, UINT16 dim, LONG *lbound)
140 TRACE("(%p, %u, %p)\n", sa, dim, lbound);
142 if (!sa)
143 return E_INVALIDARG16;
145 if (!dim || dim > sa->cDims)
146 return DISP_E_BADINDEX;
148 *lbound = sa->rgsabound[sa->cDims - dim].lLbound;
150 return S_OK;
153 /******************************************************************************
154 * SafeArrayLock [OLE2DISP.21]
156 HRESULT WINAPI SafeArrayLock16(SAFEARRAY16 *sa)
158 TRACE("(%p)\n", sa);
160 if (!sa)
161 return E_INVALIDARG16;
163 return safearray_lock(sa);
166 /******************************************************************************
167 * SafeArrayUnlock [OLE2DISP.22]
169 HRESULT WINAPI SafeArrayUnlock16(SAFEARRAY16 *sa)
171 TRACE("(%p)\n", sa);
173 if (!sa)
174 return E_INVALIDARG16;
176 if (sa->cLocks == 0)
177 return E_UNEXPECTED;
179 sa->cLocks--;
180 return S_OK;
183 /******************************************************************************
184 * SafeArrayAccessData [OLE2DISP.23]
186 HRESULT WINAPI SafeArrayAccessData16(SAFEARRAY16 *sa, SEGPTR *data)
188 HRESULT hr;
190 TRACE("(%p, %p)\n", sa, data);
192 /* arguments are not tested, it crashes if any of them is NULL */
194 hr = safearray_lock(sa);
195 if (FAILED(hr))
196 return hr;
198 *data = sa->pvData;
199 return S_OK;
202 /******************************************************************************
203 * SafeArrayUnaccessData [OLE2DISP.24]
205 HRESULT WINAPI SafeArrayUnaccessData16(SAFEARRAY16 *sa)
207 TRACE("(%p)\n", sa);
208 return SafeArrayUnlock16(sa);
211 /******************************************************************************
212 * SafeArrayAllocDescriptor [OLE2DISP.38]
214 HRESULT WINAPI SafeArrayAllocDescriptor16(UINT16 dims, SEGPTR *ret)
216 SAFEARRAY16 *sa;
217 ULONG size;
219 TRACE("%u, %p\n", dims, ret);
221 if (!dims)
222 return E_INVALIDARG16;
224 size = sizeof(SAFEARRAY16) + sizeof(SAFEARRAYBOUND16) * (dims - 1);
225 *ret = safearray_alloc(size);
226 if (!*ret)
227 return E_OUTOFMEMORY16;
229 sa = MapSL(*ret);
230 sa->cDims = dims;
231 return S_OK;
234 /******************************************************************************
235 * SafeArrayAllocData [OLE2DISP.39]
237 HRESULT WINAPI SafeArrayAllocData16(SAFEARRAY16 *sa)
239 ULONG size;
241 TRACE("%p\n", sa);
243 if (!sa)
244 return E_INVALIDARG16;
246 size = safearray_getcellcount(sa);
247 sa->pvData = safearray_alloc(size * sa->cbElements);
248 return sa->pvData ? S_OK : E_OUTOFMEMORY16;
251 /******************************************************************************
252 * SafeArrayDestroyDescriptor [OLE2DISP.40]
254 HRESULT WINAPI SafeArrayDestroyDescriptor16(SEGPTR s)
256 TRACE("0x%08lx\n", s);
258 if (s)
260 SAFEARRAY16 *sa = MapSL(s);
262 if (sa->cLocks)
263 return DISP_E_ARRAYISLOCKED;
265 safearray_free(s);
268 return S_OK;
271 /******************************************************************************
272 * SafeArrayDestroyData [OLE2DISP.41]
274 HRESULT WINAPI SafeArrayDestroyData16(SAFEARRAY16 *sa)
276 TRACE("%p\n", sa);
278 if (!sa)
279 return S_OK;
281 if (sa->cLocks)
282 return DISP_E_ARRAYISLOCKED;
284 if (!(sa->fFeatures & FADF_STATIC))
285 safearray_free(sa->pvData);
287 return S_OK;
290 /* This implementation of the BSTR API is 16-bit only. It
291 represents BSTR as a 16:16 far pointer, and the strings
292 as ISO-8859 */
294 /******************************************************************************
295 * BSTR_AllocBytes [Internal]
297 static BSTR16 BSTR_AllocBytes(int n)
299 void *ptr = HeapAlloc( GetProcessHeap(), 0, n );
300 return (BSTR16)MapLS(ptr);
303 /******************************************************************************
304 * BSTR_Free [INTERNAL]
306 static void BSTR_Free(BSTR16 in)
308 void *ptr = MapSL( (SEGPTR)in );
309 UnMapLS( (SEGPTR)in );
310 HeapFree( GetProcessHeap(), 0, ptr );
313 /******************************************************************************
314 * BSTR_GetAddr [INTERNAL]
316 static void* BSTR_GetAddr(BSTR16 in)
318 return in ? MapSL((SEGPTR)in) : 0;
321 /******************************************************************************
322 * SysAllocString [OLE2DISP.2]
324 * Create a BSTR16 from an OLESTR16 (16 Bit).
326 * PARAMS
327 * oleStr [I] Source to create BSTR16 from
329 * RETURNS
330 * Success: A BSTR16 allocated with SysAllocStringLen16().
331 * Failure: NULL, if oleStr is NULL.
333 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 oleStr)
335 BSTR16 out;
337 if (!oleStr) return 0;
339 out = BSTR_AllocBytes(strlen(oleStr)+1);
340 if (!out) return 0;
341 strcpy(BSTR_GetAddr(out),oleStr);
342 return out;
345 /******************************************************************************
346 * SysReallocString [OLE2DISP.3]
348 * Change the length of a previously created BSTR16 (16 Bit).
350 * PARAMS
351 * pbstr [I] BSTR16 to change the length of
352 * oleStr [I] New source for pbstr
354 * RETURNS
355 * Success: 1
356 * Failure: 0.
358 * NOTES
359 * SysAllocStringStringLen16().
361 INT16 WINAPI SysReAllocString16(LPBSTR16 pbstr,LPCOLESTR16 oleStr)
363 BSTR16 new=SysAllocString16(oleStr);
364 BSTR_Free(*pbstr);
365 *pbstr=new;
366 return 1;
369 /******************************************************************************
370 * SysAllocStringLen [OLE2DISP.4]
372 * Create a BSTR16 from an OLESTR16 of a given character length (16 Bit).
374 * PARAMS
375 * oleStr [I] Source to create BSTR16 from
376 * len [I] Length of oleStr in wide characters
378 * RETURNS
379 * Success: A newly allocated BSTR16 from SysAllocStringByteLen16()
380 * Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
382 * NOTES
383 * See SysAllocStringByteLen16().
385 BSTR16 WINAPI SysAllocStringLen16(const char *oleStr, int len)
387 BSTR16 out=BSTR_AllocBytes(len+1);
389 if (!out)
390 return 0;
393 * Copy the information in the buffer.
394 * Since it is valid to pass a NULL pointer here, we'll initialize the
395 * buffer to nul if it is the case.
397 if (oleStr != 0)
398 strcpy(BSTR_GetAddr(out),oleStr);
399 else
400 memset(BSTR_GetAddr(out), 0, len+1);
402 return out;
405 /******************************************************************************
406 * SysReAllocStringLen [OLE2DISP.5]
408 * Change the length of a previously created BSTR16 (16 Bit).
410 * PARAMS
411 * pbstr [I] BSTR16 to change the length of
412 * oleStr [I] New source for pbstr
413 * len [I] Length of oleStr in characters
415 * RETURNS
416 * Success: 1. The size of pbstr is updated.
417 * Failure: 0, if len >= 0x8000 or memory allocation fails.
419 * NOTES
420 * See SysAllocStringByteLen16().
421 * *pbstr may be changed by this function.
423 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
425 /* FIXME: Check input length */
426 BSTR16 new=SysAllocStringLen16(in,len);
427 BSTR_Free(*old);
428 *old=new;
429 return 1;
432 /******************************************************************************
433 * SysFreeString [OLE2DISP.6]
435 * Free a BSTR16 (16 Bit).
437 * PARAMS
438 * str [I] String to free.
440 * RETURNS
441 * Nothing.
443 void WINAPI SysFreeString16(BSTR16 str)
445 BSTR_Free(str);
448 /******************************************************************************
449 * SysStringLen [OLE2DISP.7]
451 * Get the allocated length of a BSTR16 in characters (16 Bit).
453 * PARAMS
454 * str [I] BSTR16 to find the length of
456 * RETURNS
457 * The allocated length of str, or 0 if str is NULL.
459 int WINAPI SysStringLen16(BSTR16 str)
461 return strlen(BSTR_GetAddr(str));
464 /******************************************************************************
465 * VariantChangeType [OLE2DISP.12]
467 HRESULT WINAPI VariantChangeType16(VARIANTARG *vargDest, VARIANTARG *varSrc, unsigned short flags, VARTYPE vt)
469 FIXME("stub: (%p, %p, %d, %d)\n", vargDest, varSrc, flags, vt);
470 return E_NOTIMPL;
474 /******************************************************************************
475 * CreateDispTypeInfo [OLE2DISP.31]
477 HRESULT WINAPI CreateDispTypeInfo16(
478 INTERFACEDATA *pidata,
479 LCID lcid,
480 ITypeInfo **pptinfo)
482 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
483 return E_NOTIMPL;
486 /******************************************************************************
487 * CreateStdDispatch [OLE2DISP.32]
489 HRESULT WINAPI CreateStdDispatch16(
490 IUnknown* punkOuter,
491 void* pvThis,
492 ITypeInfo* ptinfo,
493 IUnknown** ppunkStdDisp)
495 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
496 ppunkStdDisp);
497 return 0;
500 /******************************************************************************
501 * RegisterActiveObject [OLE2DISP.35]
503 HRESULT WINAPI RegisterActiveObject16(
504 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
506 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
507 return E_NOTIMPL;
510 /******************************************************************************
511 * VariantChangeTypeEx [OLE2DISP.108]
513 HRESULT WINAPI VariantChangeTypeEx16(VARIANTARG *dest, const VARIANTARG *src, LCID lcid, USHORT flags, VARTYPE vt)
515 FIXME("stub: %p %p %ld %d %d\n", dest, src, lcid, flags, vt);
516 return E_INVALIDARG;
519 /******************************************************************************
520 * SetErrorInfo [OLE2DISP.110]
522 HRESULT WINAPI SetErrorInfo16(ULONG dwReserved, IErrorInfo *perrinfo)
524 FIXME("stub: (%ld, %p)\n", dwReserved, perrinfo);
525 return E_INVALIDARG;
528 /******************************************************************************
529 * VariantInit [OLE2DISP.8]
531 void WINAPI VariantInit16(VARIANTARG16 *v)
533 TRACE("(%p)\n", v);
534 v->vt = VT_EMPTY;