4 * Copyright 1995 Martin von Loewis
11 #include "wine/windef16.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
31 /******************************************************************************
32 * BSTR_AllocBytes [Internal]
34 static BSTR16 BSTR_AllocBytes(int n)
36 void *ptr = HeapAlloc( GetProcessHeap(), 0, n );
37 return (BSTR16)MapLS(ptr);
40 /******************************************************************************
41 * BSTR_Free [INTERNAL]
43 static void BSTR_Free(BSTR16 in)
45 void *ptr = MapSL( (SEGPTR)in );
46 UnMapLS( (SEGPTR)in );
47 HeapFree( GetProcessHeap(), 0, ptr );
50 /******************************************************************************
51 * BSTR_GetAddr [INTERNAL]
53 static void* BSTR_GetAddr(BSTR16 in)
55 return in ? MapSL((SEGPTR)in) : 0;
58 /******************************************************************************
59 * SysAllocString [OLE2DISP.2]
61 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
67 out = BSTR_AllocBytes(strlen(in)+1);
69 strcpy(BSTR_GetAddr(out),in);
73 /******************************************************************************
74 * SysAllocString [OLEAUT32.2]
76 BSTR WINAPI SysAllocString(LPCOLESTR in)
80 /* Delegate this to the SysAllocStringLen32 method. */
81 return SysAllocStringLen(in, lstrlenW(in));
84 /******************************************************************************
85 * SysReallocString [OLE2DISP.3]
87 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
89 BSTR16 new=SysAllocString16(in);
95 /******************************************************************************
96 * SysReAllocString [OLEAUT32.3]
98 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
107 * Make sure we free the old string.
113 * Allocate the new string
115 *old = SysAllocString(in);
120 /******************************************************************************
121 * SysAllocStringLen [OLE2DISP.4]
123 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
125 BSTR16 out=BSTR_AllocBytes(len+1);
131 * Copy the information in the buffer.
132 * Since it is valid to pass a NULL pointer here, we'll initialize the
133 * buffer to nul if it is the case.
136 strcpy(BSTR_GetAddr(out),in);
138 memset(BSTR_GetAddr(out), 0, len+1);
143 /******************************************************************************
144 * SysAllocStringLen [OLEAUT32.4]
146 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
147 * section, he describes the DWORD value placed *before* the BSTR data type.
148 * he describes it as a "DWORD count of characters". By experimenting with
149 * a windows application, this count seems to be a DWORD count of bytes in
150 * the string. Meaning that the count is double the number of wide
151 * characters in the string.
153 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
160 * Find the length of the buffer passed-in in bytes.
162 bufferSize = len * sizeof (WCHAR);
165 * Allocate a new buffer to hold the string.
166 * dont't forget to keep an empty spot at the beginning of the
167 * buffer for the character count and an extra character at the
170 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
172 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
175 * If the memory allocation failed, return a null pointer.
181 * Copy the length of the string in the placeholder.
183 *newBuffer = bufferSize;
186 * Skip the byte count.
191 * Copy the information in the buffer.
192 * Since it is valid to pass a NULL pointer here, we'll initialize the
193 * buffer to nul if it is the case.
196 memcpy(newBuffer, in, bufferSize);
198 memset(newBuffer, 0, bufferSize);
201 * Make sure that there is a nul character at the end of the
204 stringBuffer = (WCHAR*)newBuffer;
205 stringBuffer[len] = L'\0';
207 return (LPWSTR)stringBuffer;
210 /******************************************************************************
211 * SysReAllocStringLen [OLE2DISP.5]
213 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
215 BSTR16 new=SysAllocStringLen16(in,len);
222 /******************************************************************************
223 * SysReAllocStringLen [OLEAUT32.5]
225 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
234 * Make sure we free the old string.
240 * Allocate the new string
242 *old = SysAllocStringLen(in, len);
247 /******************************************************************************
248 * SysFreeString [OLE2DISP.6]
250 void WINAPI SysFreeString16(BSTR16 in)
255 /******************************************************************************
256 * SysFreeString [OLEAUT32.6]
258 void WINAPI SysFreeString(BSTR in)
260 DWORD* bufferPointer;
262 /* NULL is a valid parameter */
266 * We have to be careful when we free a BSTR pointer, it points to
267 * the beginning of the string but it skips the byte count contained
270 bufferPointer = (DWORD*)in;
275 * Free the memory from its "real" origin.
277 HeapFree(GetProcessHeap(), 0, bufferPointer);
280 /******************************************************************************
281 * SysStringLen [OLE2DISP.7]
283 int WINAPI SysStringLen16(BSTR16 str)
285 return strlen(BSTR_GetAddr(str));
288 /******************************************************************************
289 * SysStringLen [OLEAUT32.7]
291 * The Windows documentation states that the length returned by this function
292 * is not necessarely the same as the length returned by the _lstrlenW method.
293 * It is the same number that was passed in as the "len" parameter if the
294 * string was allocated with a SysAllocStringLen method call.
296 int WINAPI SysStringLen(BSTR str)
298 DWORD* bufferPointer;
302 * The length of the string (in bytes) is contained in a DWORD placed
303 * just before the BSTR pointer
305 bufferPointer = (DWORD*)str;
309 return (int)(*bufferPointer/sizeof(WCHAR));
312 /******************************************************************************
313 * SysStringByteLen [OLEAUT32.149]
315 * The Windows documentation states that the length returned by this function
316 * is not necessarely the same as the length returned by the _lstrlenW method.
317 * It is the same number that was passed in as the "len" parameter if the
318 * string was allocated with a SysAllocStringLen method call.
320 int WINAPI SysStringByteLen(BSTR str)
322 DWORD* bufferPointer;
326 * The length of the string (in bytes) is contained in a DWORD placed
327 * just before the BSTR pointer
329 bufferPointer = (DWORD*)str;
333 return (int)(*bufferPointer);
336 /******************************************************************************
337 * CreateDispTypeInfo [OLE2DISP.31]
339 HRESULT WINAPI CreateDispTypeInfo16(
340 INTERFACEDATA *pidata,
344 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
348 /******************************************************************************
349 * CreateDispTypeInfo [OLEAUT32.31]
351 HRESULT WINAPI CreateDispTypeInfo(
352 INTERFACEDATA *pidata,
356 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
360 /******************************************************************************
361 * CreateStdDispatch [OLE2DISP.32]
363 HRESULT WINAPI CreateStdDispatch16(
367 IUnknown** ppunkStdDisp)
369 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
374 /******************************************************************************
375 * CreateStdDispatch [OLEAUT32.32]
377 HRESULT WINAPI CreateStdDispatch(
381 IUnknown** ppunkStdDisp)
383 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
388 /******************************************************************************
389 * RegisterActiveObject [OLE2DISP.35]
391 HRESULT WINAPI RegisterActiveObject16(
392 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
394 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
398 /******************************************************************************
399 * OleTranslateColor [OLEAUT32.421]
401 * Converts an OLE_COLOR to a COLORREF.
402 * See the documentation for conversion rules.
403 * pColorRef can be NULL. In that case the user only wants to test the
406 HRESULT WINAPI OleTranslateColor(
412 BYTE b = HIBYTE(HIWORD(clr));
414 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
417 * In case pColorRef is NULL, provide our own to simplify the code.
419 if (pColorRef == NULL)
420 pColorRef = &colorref;
427 *pColorRef = PALETTERGB(GetRValue(clr),
442 * Validate the palette index.
444 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
459 int index = LOBYTE(LOWORD(clr));
462 * Validate GetSysColor index.
464 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
467 *pColorRef = GetSysColor(index);
479 /******************************************************************************
480 * SysAllocStringByteLen [OLEAUT32.150]
483 BSTR WINAPI SysAllocStringByteLen(LPCSTR in, UINT len)
489 * Allocate a new buffer to hold the string.
490 * dont't forget to keep an empty spot at the beginning of the
491 * buffer for the character count and an extra character at the
494 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
496 len + sizeof(WCHAR) + sizeof(DWORD));
499 * If the memory allocation failed, return a null pointer.
505 * Copy the length of the string in the placeholder.
510 * Skip the byte count.
515 * Copy the information in the buffer.
516 * Since it is valid to pass a NULL pointer here, we'll initialize the
517 * buffer to nul if it is the case.
520 memcpy(newBuffer, in, len);
523 * Make sure that there is a nul character at the end of the
526 stringBuffer = (char *)newBuffer;
527 stringBuffer[len] = 0;
528 stringBuffer[len+1] = 0;
530 return (LPWSTR)stringBuffer;