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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 #include "wine/windef16.h"
37 #include "wine/debug.h"
39 WINE_DEFAULT_DEBUG_CHANNEL(ole);
41 /* This implementation of the BSTR API is 16-bit only. It
42 represents BSTR as a 16:16 far pointer, and the strings
45 /******************************************************************************
46 * BSTR_AllocBytes [Internal]
48 static BSTR16 BSTR_AllocBytes(int n)
50 void *ptr = HeapAlloc( GetProcessHeap(), 0, n );
51 return (BSTR16)MapLS(ptr);
54 /******************************************************************************
55 * BSTR_Free [INTERNAL]
57 static void BSTR_Free(BSTR16 in)
59 void *ptr = MapSL( (SEGPTR)in );
60 UnMapLS( (SEGPTR)in );
61 HeapFree( GetProcessHeap(), 0, ptr );
64 /******************************************************************************
65 * BSTR_GetAddr [INTERNAL]
67 static void* BSTR_GetAddr(BSTR16 in)
69 return in ? MapSL((SEGPTR)in) : 0;
72 /******************************************************************************
73 * SysAllocString [OLE2DISP.2]
75 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
81 out = BSTR_AllocBytes(strlen(in)+1);
83 strcpy(BSTR_GetAddr(out),in);
87 /******************************************************************************
88 * SysAllocString [OLEAUT32.2]
90 * MSDN (October 2001) states that this returns a NULL value if the argument
91 * is a zero-length string. This does not appear to be true; certainly it
92 * returns a value under Win98 (Oleaut32.dll Ver 2.40.4515.0)
94 BSTR WINAPI SysAllocString(LPCOLESTR in)
98 /* Delegate this to the SysAllocStringLen32 method. */
99 return SysAllocStringLen(in, lstrlenW(in));
102 /******************************************************************************
103 * SysReallocString [OLE2DISP.3]
105 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
107 BSTR16 new=SysAllocString16(in);
113 /******************************************************************************
114 * SysReAllocString [OLEAUT32.3]
116 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
125 * Make sure we free the old string.
131 * Allocate the new string
133 *old = SysAllocString(in);
138 /******************************************************************************
139 * SysAllocStringLen [OLE2DISP.4]
141 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
143 BSTR16 out=BSTR_AllocBytes(len+1);
149 * Copy the information in the buffer.
150 * Since it is valid to pass a NULL pointer here, we'll initialize the
151 * buffer to nul if it is the case.
154 strcpy(BSTR_GetAddr(out),in);
156 memset(BSTR_GetAddr(out), 0, len+1);
161 /******************************************************************************
162 * SysAllocStringLen [OLEAUT32.4]
164 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
165 * section, he describes the DWORD value placed *before* the BSTR data type.
166 * he describes it as a "DWORD count of characters". By experimenting with
167 * a windows application, this count seems to be a DWORD count of bytes in
168 * the string. Meaning that the count is double the number of wide
169 * characters in the string.
171 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
178 * Find the length of the buffer passed-in in bytes.
180 bufferSize = len * sizeof (WCHAR);
183 * Allocate a new buffer to hold the string.
184 * dont't forget to keep an empty spot at the beginning of the
185 * buffer for the character count and an extra character at the
188 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
190 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
193 * If the memory allocation failed, return a null pointer.
199 * Copy the length of the string in the placeholder.
201 *newBuffer = bufferSize;
204 * Skip the byte count.
209 * Copy the information in the buffer.
210 * Since it is valid to pass a NULL pointer here, we'll initialize the
211 * buffer to nul if it is the case.
214 memcpy(newBuffer, in, bufferSize);
216 memset(newBuffer, 0, bufferSize);
219 * Make sure that there is a nul character at the end of the
222 stringBuffer = (WCHAR*)newBuffer;
223 stringBuffer[len] = L'\0';
225 return (LPWSTR)stringBuffer;
228 /******************************************************************************
229 * SysReAllocStringLen [OLE2DISP.5]
231 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
233 BSTR16 new=SysAllocStringLen16(in,len);
240 /******************************************************************************
241 * SysReAllocStringLen [OLEAUT32.5]
243 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
252 * Make sure we free the old string.
258 * Allocate the new string
260 *old = SysAllocStringLen(in, len);
265 /******************************************************************************
266 * SysFreeString [OLE2DISP.6]
268 void WINAPI SysFreeString16(BSTR16 in)
273 /******************************************************************************
274 * SysFreeString [OLEAUT32.6]
276 void WINAPI SysFreeString(BSTR in)
278 DWORD* bufferPointer;
280 /* NULL is a valid parameter */
284 * We have to be careful when we free a BSTR pointer, it points to
285 * the beginning of the string but it skips the byte count contained
288 bufferPointer = (DWORD*)in;
293 * Free the memory from its "real" origin.
295 HeapFree(GetProcessHeap(), 0, bufferPointer);
298 /******************************************************************************
299 * SysStringLen [OLE2DISP.7]
301 int WINAPI SysStringLen16(BSTR16 str)
303 return strlen(BSTR_GetAddr(str));
306 /******************************************************************************
307 * SysStringLen [OLEAUT32.7]
309 * The Windows documentation states that the length returned by this function
310 * is not necessarely the same as the length returned by the _lstrlenW method.
311 * It is the same number that was passed in as the "len" parameter if the
312 * string was allocated with a SysAllocStringLen method call.
314 int WINAPI SysStringLen(BSTR str)
316 DWORD* bufferPointer;
320 * The length of the string (in bytes) is contained in a DWORD placed
321 * just before the BSTR pointer
323 bufferPointer = (DWORD*)str;
327 return (int)(*bufferPointer/sizeof(WCHAR));
330 /******************************************************************************
331 * SysStringByteLen [OLEAUT32.149]
333 * The Windows documentation states that the length returned by this function
334 * is not necessarely the same as the length returned by the _lstrlenW method.
335 * It is the same number that was passed in as the "len" parameter if the
336 * string was allocated with a SysAllocStringLen method call.
338 int WINAPI SysStringByteLen(BSTR str)
340 DWORD* bufferPointer;
344 * The length of the string (in bytes) is contained in a DWORD placed
345 * just before the BSTR pointer
347 bufferPointer = (DWORD*)str;
351 return (int)(*bufferPointer);
354 /******************************************************************************
355 * CreateDispTypeInfo [OLEAUT32.31]
357 HRESULT WINAPI CreateDispTypeInfo(
358 INTERFACEDATA *pidata,
362 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
366 /******************************************************************************
367 * CreateDispTypeInfo [OLE2DISP.31]
369 HRESULT WINAPI CreateDispTypeInfo16(
370 INTERFACEDATA *pidata,
374 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
378 /******************************************************************************
379 * CreateStdDispatch [OLE2DISP.32]
381 HRESULT WINAPI CreateStdDispatch16(
385 IUnknown** ppunkStdDisp)
387 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
392 /******************************************************************************
393 * CreateStdDispatch [OLEAUT32.32]
395 HRESULT WINAPI CreateStdDispatch(
399 IUnknown** ppunkStdDisp)
401 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
406 /******************************************************************************
407 * RegisterActiveObject [OLE2DISP.35]
409 HRESULT WINAPI RegisterActiveObject16(
410 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
412 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
416 /******************************************************************************
417 * OleTranslateColor [OLEAUT32.421]
419 * Converts an OLE_COLOR to a COLORREF.
420 * See the documentation for conversion rules.
421 * pColorRef can be NULL. In that case the user only wants to test the
424 HRESULT WINAPI OleTranslateColor(
430 BYTE b = HIBYTE(HIWORD(clr));
432 TRACE("(%08lx, %p, %p):stub\n", clr, hpal, pColorRef);
435 * In case pColorRef is NULL, provide our own to simplify the code.
437 if (pColorRef == NULL)
438 pColorRef = &colorref;
445 *pColorRef = PALETTERGB(GetRValue(clr),
460 * Validate the palette index.
462 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
477 int index = LOBYTE(LOWORD(clr));
480 * Validate GetSysColor index.
482 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
485 *pColorRef = GetSysColor(index);
497 /******************************************************************************
498 * SysAllocStringByteLen [OLEAUT32.150]
501 BSTR WINAPI SysAllocStringByteLen(LPCSTR in, UINT len)
507 * Allocate a new buffer to hold the string.
508 * dont't forget to keep an empty spot at the beginning of the
509 * buffer for the character count and an extra character at the
512 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
514 len + sizeof(WCHAR) + sizeof(DWORD));
517 * If the memory allocation failed, return a null pointer.
523 * Copy the length of the string in the placeholder.
528 * Skip the byte count.
533 * Copy the information in the buffer.
534 * Since it is valid to pass a NULL pointer here, we'll initialize the
535 * buffer to nul if it is the case.
538 memcpy(newBuffer, in, len);
541 * Make sure that there is a nul character at the end of the
544 stringBuffer = (char *)newBuffer;
545 stringBuffer[len] = 0;
546 stringBuffer[len+1] = 0;
548 return (LPWSTR)stringBuffer;