Cosmetics.
[wine/wine64.git] / dlls / oleaut32 / ole2disp.c
blob72a41084a885aa1775110562cc549c4b5c05efbe
1 /*
2 * OLE2DISP library
4 * Copyright 1995 Martin von Loewis
5 */
7 #include "config.h"
9 #include <string.h>
11 #include "ole2.h"
12 #include "oleauto.h"
13 #include "windef.h"
14 #include "winbase.h"
15 #include "winerror.h"
16 #include "wingdi.h"
17 #include "winuser.h"
19 #include "heap.h"
20 #include "ole2disp.h"
21 #include "olectl.h"
23 #include "debugtools.h"
25 DEFAULT_DEBUG_CHANNEL(ole);
27 /* This implementation of the BSTR API is 16-bit only. It
28 represents BSTR as a 16:16 far pointer, and the strings
29 as ISO-8859 */
31 /******************************************************************************
32 * BSTR_AllocBytes [Internal]
34 static BSTR16 BSTR_AllocBytes(int n)
36 void *ptr = SEGPTR_ALLOC(n);
37 return (BSTR16)SEGPTR_GET(ptr);
40 /******************************************************************************
41 * BSTR_Free [INTERNAL]
43 static void BSTR_Free(BSTR16 in)
45 SEGPTR_FREE( MapSL((SEGPTR)in) );
48 /******************************************************************************
49 * BSTR_GetAddr [INTERNAL]
51 static void* BSTR_GetAddr(BSTR16 in)
53 return in ? MapSL((SEGPTR)in) : 0;
56 /******************************************************************************
57 * SysAllocString [OLE2DISP.2]
59 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
61 BSTR16 out;
63 if (!in) return 0;
65 out = BSTR_AllocBytes(strlen(in)+1);
66 if (!out) return 0;
67 strcpy(BSTR_GetAddr(out),in);
68 return out;
71 /******************************************************************************
72 * SysAllocString [OLEAUT32.2]
74 BSTR WINAPI SysAllocString(LPCOLESTR in)
76 if (!in) return 0;
78 /* Delegate this to the SysAllocStringLen32 method. */
79 return SysAllocStringLen(in, lstrlenW(in));
82 /******************************************************************************
83 * SysReallocString [OLE2DISP.3]
85 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
87 BSTR16 new=SysAllocString16(in);
88 BSTR_Free(*old);
89 *old=new;
90 return 1;
93 /******************************************************************************
94 * SysReAllocString [OLEAUT32.3]
96 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
99 * Sanity check
101 if (old==NULL)
102 return 0;
105 * Make sure we free the old string.
107 if (*old!=NULL)
108 SysFreeString(*old);
111 * Allocate the new string
113 *old = SysAllocString(in);
115 return 1;
118 /******************************************************************************
119 * SysAllocStringLen [OLE2DISP.4]
121 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
123 BSTR16 out=BSTR_AllocBytes(len+1);
125 if (!out)
126 return 0;
129 * Copy the information in the buffer.
130 * Since it is valid to pass a NULL pointer here, we'll initialize the
131 * buffer to nul if it is the case.
133 if (in != 0)
134 strcpy(BSTR_GetAddr(out),in);
135 else
136 memset(BSTR_GetAddr(out), 0, len+1);
138 return out;
141 /******************************************************************************
142 * SysAllocStringLen [OLEAUT32.4]
144 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
145 * section, he describes the DWORD value placed *before* the BSTR data type.
146 * he describes it as a "DWORD count of characters". By experimenting with
147 * a windows application, this count seems to be a DWORD count of bytes in
148 * the string. Meaning that the count is double the number of wide
149 * characters in the string.
151 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
153 DWORD bufferSize;
154 DWORD* newBuffer;
155 WCHAR* stringBuffer;
158 * Find the length of the buffer passed-in in bytes.
160 bufferSize = len * sizeof (WCHAR);
163 * Allocate a new buffer to hold the string.
164 * dont't forget to keep an empty spot at the beginning of the
165 * buffer for the character count and an extra character at the
166 * end for the NULL.
168 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
170 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
173 * If the memory allocation failed, return a null pointer.
175 if (newBuffer==0)
176 return 0;
179 * Copy the length of the string in the placeholder.
181 *newBuffer = bufferSize;
184 * Skip the byte count.
186 newBuffer++;
189 * Copy the information in the buffer.
190 * Since it is valid to pass a NULL pointer here, we'll initialize the
191 * buffer to nul if it is the case.
193 if (in != 0)
194 memcpy(newBuffer, in, bufferSize);
195 else
196 memset(newBuffer, 0, bufferSize);
199 * Make sure that there is a nul character at the end of the
200 * string.
202 stringBuffer = (WCHAR*)newBuffer;
203 stringBuffer[len] = L'\0';
205 return (LPWSTR)stringBuffer;
208 /******************************************************************************
209 * SysReAllocStringLen [OLE2DISP.5]
211 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
213 BSTR16 new=SysAllocStringLen16(in,len);
214 BSTR_Free(*old);
215 *old=new;
216 return 1;
220 /******************************************************************************
221 * SysReAllocStringLen [OLEAUT32.5]
223 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
226 * Sanity check
228 if (old==NULL)
229 return 0;
232 * Make sure we free the old string.
234 if (*old!=NULL)
235 SysFreeString(*old);
238 * Allocate the new string
240 *old = SysAllocStringLen(in, len);
242 return 1;
245 /******************************************************************************
246 * SysFreeString [OLE2DISP.6]
248 void WINAPI SysFreeString16(BSTR16 in)
250 BSTR_Free(in);
253 /******************************************************************************
254 * SysFreeString [OLEAUT32.6]
256 void WINAPI SysFreeString(BSTR in)
258 DWORD* bufferPointer;
260 /* NULL is a valid parameter */
261 if(!in) return;
264 * We have to be careful when we free a BSTR pointer, it points to
265 * the beginning of the string but it skips the byte count contained
266 * before the string.
268 bufferPointer = (DWORD*)in;
270 bufferPointer--;
273 * Free the memory from its "real" origin.
275 HeapFree(GetProcessHeap(), 0, bufferPointer);
278 /******************************************************************************
279 * SysStringLen [OLE2DISP.7]
281 int WINAPI SysStringLen16(BSTR16 str)
283 return strlen(BSTR_GetAddr(str));
286 /******************************************************************************
287 * SysStringLen [OLEAUT32.7]
289 * The Windows documentation states that the length returned by this function
290 * is not necessarely the same as the length returned by the _lstrlenW method.
291 * It is the same number that was passed in as the "len" parameter if the
292 * string was allocated with a SysAllocStringLen method call.
294 int WINAPI SysStringLen(BSTR str)
296 DWORD* bufferPointer;
298 if (!str) return 0;
300 * The length of the string (in bytes) is contained in a DWORD placed
301 * just before the BSTR pointer
303 bufferPointer = (DWORD*)str;
305 bufferPointer--;
307 return (int)(*bufferPointer/sizeof(WCHAR));
310 /******************************************************************************
311 * SysStringByteLen [OLEAUT32.149]
313 * The Windows documentation states that the length returned by this function
314 * is not necessarely the same as the length returned by the _lstrlenW method.
315 * It is the same number that was passed in as the "len" parameter if the
316 * string was allocated with a SysAllocStringLen method call.
318 int WINAPI SysStringByteLen(BSTR str)
320 DWORD* bufferPointer;
322 if (!str) return 0;
324 * The length of the string (in bytes) is contained in a DWORD placed
325 * just before the BSTR pointer
327 bufferPointer = (DWORD*)str;
329 bufferPointer--;
331 return (int)(*bufferPointer);
334 /******************************************************************************
335 * CreateDispTypeInfo [OLE2DISP.31]
337 HRESULT WINAPI CreateDispTypeInfo16(
338 INTERFACEDATA *pidata,
339 LCID lcid,
340 ITypeInfo **pptinfo)
342 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
343 return 0;
346 /******************************************************************************
347 * CreateDispTypeInfo [OLEAUT32.31]
349 HRESULT WINAPI CreateDispTypeInfo(
350 INTERFACEDATA *pidata,
351 LCID lcid,
352 ITypeInfo **pptinfo)
354 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
355 return 0;
358 /******************************************************************************
359 * CreateStdDispatch [OLE2DISP.32]
361 HRESULT WINAPI CreateStdDispatch16(
362 IUnknown* punkOuter,
363 void* pvThis,
364 ITypeInfo* ptinfo,
365 IUnknown** ppunkStdDisp)
367 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
368 ppunkStdDisp);
369 return 0;
372 /******************************************************************************
373 * CreateStdDispatch [OLEAUT32.32]
375 HRESULT WINAPI CreateStdDispatch(
376 IUnknown* punkOuter,
377 void* pvThis,
378 ITypeInfo* ptinfo,
379 IUnknown** ppunkStdDisp)
381 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
382 ppunkStdDisp);
383 return 0;
386 /******************************************************************************
387 * RegisterActiveObject [OLE2DISP.35]
389 HRESULT WINAPI RegisterActiveObject16(
390 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
392 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
393 return 0;
396 /******************************************************************************
397 * OleTranslateColor [OLEAUT32.421]
399 * Converts an OLE_COLOR to a COLORREF.
400 * See the documentation for conversion rules.
401 * pColorRef can be NULL. In that case the user only wants to test the
402 * conversion.
404 HRESULT WINAPI OleTranslateColor(
405 OLE_COLOR clr,
406 HPALETTE hpal,
407 COLORREF* pColorRef)
409 COLORREF colorref;
410 BYTE b = HIBYTE(HIWORD(clr));
412 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
415 * In case pColorRef is NULL, provide our own to simplify the code.
417 if (pColorRef == NULL)
418 pColorRef = &colorref;
420 switch (b)
422 case 0x00:
424 if (hpal != 0)
425 *pColorRef = PALETTERGB(GetRValue(clr),
426 GetGValue(clr),
427 GetBValue(clr));
428 else
429 *pColorRef = clr;
431 break;
434 case 0x01:
436 if (hpal != 0)
438 PALETTEENTRY pe;
440 * Validate the palette index.
442 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
443 return E_INVALIDARG;
446 *pColorRef = clr;
448 break;
451 case 0x02:
452 *pColorRef = clr;
453 break;
455 case 0x80:
457 int index = LOBYTE(LOWORD(clr));
460 * Validate GetSysColor index.
462 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
463 return E_INVALIDARG;
465 *pColorRef = GetSysColor(index);
467 break;
470 default:
471 return E_INVALIDARG;
474 return S_OK;
477 /******************************************************************************
478 * SysAllocStringByteLen [OLEAUT32.150]
481 BSTR WINAPI SysAllocStringByteLen(LPCSTR in, UINT len)
483 DWORD* newBuffer;
484 char* stringBuffer;
487 * Allocate a new buffer to hold the string.
488 * dont't forget to keep an empty spot at the beginning of the
489 * buffer for the character count and an extra character at the
490 * end for the NULL.
492 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
494 len + sizeof(WCHAR) + sizeof(DWORD));
497 * If the memory allocation failed, return a null pointer.
499 if (newBuffer==0)
500 return 0;
503 * Copy the length of the string in the placeholder.
505 *newBuffer = len;
508 * Skip the byte count.
510 newBuffer++;
513 * Copy the information in the buffer.
514 * Since it is valid to pass a NULL pointer here, we'll initialize the
515 * buffer to nul if it is the case.
517 if (in != 0)
518 memcpy(newBuffer, in, len);
521 * Make sure that there is a nul character at the end of the
522 * string.
524 stringBuffer = (char *)newBuffer;
525 stringBuffer[len] = 0;
526 stringBuffer[len+1] = 0;
528 return (LPWSTR)stringBuffer;