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 * MSDN (October 2001) states that this returns a NULL value if the argument
77 * is a zero-length string. This does not appear to be true; certainly it
78 * returns a value under Win98 (Oleaut32.dll Ver 2.40.4515.0)
80 BSTR WINAPI SysAllocString(LPCOLESTR in)
84 /* Delegate this to the SysAllocStringLen32 method. */
85 return SysAllocStringLen(in, lstrlenW(in));
88 /******************************************************************************
89 * SysReallocString [OLE2DISP.3]
91 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
93 BSTR16 new=SysAllocString16(in);
99 /******************************************************************************
100 * SysReAllocString [OLEAUT32.3]
102 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
111 * Make sure we free the old string.
117 * Allocate the new string
119 *old = SysAllocString(in);
124 /******************************************************************************
125 * SysAllocStringLen [OLE2DISP.4]
127 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
129 BSTR16 out=BSTR_AllocBytes(len+1);
135 * Copy the information in the buffer.
136 * Since it is valid to pass a NULL pointer here, we'll initialize the
137 * buffer to nul if it is the case.
140 strcpy(BSTR_GetAddr(out),in);
142 memset(BSTR_GetAddr(out), 0, len+1);
147 /******************************************************************************
148 * SysAllocStringLen [OLEAUT32.4]
150 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
151 * section, he describes the DWORD value placed *before* the BSTR data type.
152 * he describes it as a "DWORD count of characters". By experimenting with
153 * a windows application, this count seems to be a DWORD count of bytes in
154 * the string. Meaning that the count is double the number of wide
155 * characters in the string.
157 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
164 * Find the length of the buffer passed-in in bytes.
166 bufferSize = len * sizeof (WCHAR);
169 * Allocate a new buffer to hold the string.
170 * dont't forget to keep an empty spot at the beginning of the
171 * buffer for the character count and an extra character at the
174 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
176 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
179 * If the memory allocation failed, return a null pointer.
185 * Copy the length of the string in the placeholder.
187 *newBuffer = bufferSize;
190 * Skip the byte count.
195 * Copy the information in the buffer.
196 * Since it is valid to pass a NULL pointer here, we'll initialize the
197 * buffer to nul if it is the case.
200 memcpy(newBuffer, in, bufferSize);
202 memset(newBuffer, 0, bufferSize);
205 * Make sure that there is a nul character at the end of the
208 stringBuffer = (WCHAR*)newBuffer;
209 stringBuffer[len] = L'\0';
211 return (LPWSTR)stringBuffer;
214 /******************************************************************************
215 * SysReAllocStringLen [OLE2DISP.5]
217 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
219 BSTR16 new=SysAllocStringLen16(in,len);
226 /******************************************************************************
227 * SysReAllocStringLen [OLEAUT32.5]
229 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
238 * Make sure we free the old string.
244 * Allocate the new string
246 *old = SysAllocStringLen(in, len);
251 /******************************************************************************
252 * SysFreeString [OLE2DISP.6]
254 void WINAPI SysFreeString16(BSTR16 in)
259 /******************************************************************************
260 * SysFreeString [OLEAUT32.6]
262 void WINAPI SysFreeString(BSTR in)
264 DWORD* bufferPointer;
266 /* NULL is a valid parameter */
270 * We have to be careful when we free a BSTR pointer, it points to
271 * the beginning of the string but it skips the byte count contained
274 bufferPointer = (DWORD*)in;
279 * Free the memory from its "real" origin.
281 HeapFree(GetProcessHeap(), 0, bufferPointer);
284 /******************************************************************************
285 * SysStringLen [OLE2DISP.7]
287 int WINAPI SysStringLen16(BSTR16 str)
289 return strlen(BSTR_GetAddr(str));
292 /******************************************************************************
293 * SysStringLen [OLEAUT32.7]
295 * The Windows documentation states that the length returned by this function
296 * is not necessarely the same as the length returned by the _lstrlenW method.
297 * It is the same number that was passed in as the "len" parameter if the
298 * string was allocated with a SysAllocStringLen method call.
300 int WINAPI SysStringLen(BSTR str)
302 DWORD* bufferPointer;
306 * The length of the string (in bytes) is contained in a DWORD placed
307 * just before the BSTR pointer
309 bufferPointer = (DWORD*)str;
313 return (int)(*bufferPointer/sizeof(WCHAR));
316 /******************************************************************************
317 * SysStringByteLen [OLEAUT32.149]
319 * The Windows documentation states that the length returned by this function
320 * is not necessarely the same as the length returned by the _lstrlenW method.
321 * It is the same number that was passed in as the "len" parameter if the
322 * string was allocated with a SysAllocStringLen method call.
324 int WINAPI SysStringByteLen(BSTR str)
326 DWORD* bufferPointer;
330 * The length of the string (in bytes) is contained in a DWORD placed
331 * just before the BSTR pointer
333 bufferPointer = (DWORD*)str;
337 return (int)(*bufferPointer);
340 /******************************************************************************
341 * CreateDispTypeInfo [OLE2DISP.31]
343 HRESULT WINAPI CreateDispTypeInfo16(
344 INTERFACEDATA *pidata,
348 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
352 /******************************************************************************
353 * CreateDispTypeInfo [OLEAUT32.31]
355 HRESULT WINAPI CreateDispTypeInfo(
356 INTERFACEDATA *pidata,
360 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
364 /******************************************************************************
365 * CreateStdDispatch [OLE2DISP.32]
367 HRESULT WINAPI CreateStdDispatch16(
371 IUnknown** ppunkStdDisp)
373 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
378 /******************************************************************************
379 * CreateStdDispatch [OLEAUT32.32]
381 HRESULT WINAPI CreateStdDispatch(
385 IUnknown** ppunkStdDisp)
387 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
392 /******************************************************************************
393 * RegisterActiveObject [OLE2DISP.35]
395 HRESULT WINAPI RegisterActiveObject16(
396 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
398 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
402 /******************************************************************************
403 * OleTranslateColor [OLEAUT32.421]
405 * Converts an OLE_COLOR to a COLORREF.
406 * See the documentation for conversion rules.
407 * pColorRef can be NULL. In that case the user only wants to test the
410 HRESULT WINAPI OleTranslateColor(
416 BYTE b = HIBYTE(HIWORD(clr));
418 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
421 * In case pColorRef is NULL, provide our own to simplify the code.
423 if (pColorRef == NULL)
424 pColorRef = &colorref;
431 *pColorRef = PALETTERGB(GetRValue(clr),
446 * Validate the palette index.
448 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
463 int index = LOBYTE(LOWORD(clr));
466 * Validate GetSysColor index.
468 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
471 *pColorRef = GetSysColor(index);
483 /******************************************************************************
484 * SysAllocStringByteLen [OLEAUT32.150]
487 BSTR WINAPI SysAllocStringByteLen(LPCSTR in, UINT len)
493 * Allocate a new buffer to hold the string.
494 * dont't forget to keep an empty spot at the beginning of the
495 * buffer for the character count and an extra character at the
498 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
500 len + sizeof(WCHAR) + sizeof(DWORD));
503 * If the memory allocation failed, return a null pointer.
509 * Copy the length of the string in the placeholder.
514 * Skip the byte count.
519 * Copy the information in the buffer.
520 * Since it is valid to pass a NULL pointer here, we'll initialize the
521 * buffer to nul if it is the case.
524 memcpy(newBuffer, in, len);
527 * Make sure that there is a nul character at the end of the
530 stringBuffer = (char *)newBuffer;
531 stringBuffer[len] = 0;
532 stringBuffer[len+1] = 0;
534 return (LPWSTR)stringBuffer;