4 * Copyright 1995 Martin von Loewis
11 #include "wine/windef16.h"
16 #include "debugtools.h"
18 DEFAULT_DEBUG_CHANNEL(ole);
20 /* This implementation of the BSTR API is 16-bit only. It
21 represents BSTR as a 16:16 far pointer, and the strings
24 /******************************************************************************
25 * BSTR_AllocBytes [Internal]
27 static BSTR16 BSTR_AllocBytes(int n)
29 void *ptr = SEGPTR_ALLOC(n);
30 return (BSTR16)SEGPTR_GET(ptr);
33 /******************************************************************************
34 * BSTR_Free [INTERNAL]
36 static void BSTR_Free(BSTR16 in)
38 SEGPTR_FREE( MapSL((SEGPTR)in) );
41 /******************************************************************************
42 * BSTR_GetAddr [INTERNAL]
44 static void* BSTR_GetAddr(BSTR16 in)
46 return in ? MapSL((SEGPTR)in) : 0;
49 /******************************************************************************
50 * SysAllocString16 [OLE2DISP.2]
52 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
58 out = BSTR_AllocBytes(strlen(in)+1);
60 strcpy(BSTR_GetAddr(out),in);
64 /******************************************************************************
65 * SysAllocString [OLEAUT32.2]
67 BSTR WINAPI SysAllocString(LPCOLESTR in)
71 /* Delegate this to the SysAllocStringLen32 method. */
72 return SysAllocStringLen(in, lstrlenW(in));
75 /******************************************************************************
76 * SysReAllocString16 [OLE2DISP.3]
78 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
80 BSTR16 new=SysAllocString16(in);
86 /******************************************************************************
87 * SysReAllocString [OLEAUT32.3]
89 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
98 * Make sure we free the old string.
104 * Allocate the new string
106 *old = SysAllocString(in);
111 /******************************************************************************
112 * SysAllocStringLen16 [OLE2DISP.4]
114 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
116 BSTR16 out=BSTR_AllocBytes(len+1);
122 * Copy the information in the buffer.
123 * Since it is valid to pass a NULL pointer here, we'll initialize the
124 * buffer to nul if it is the case.
127 strcpy(BSTR_GetAddr(out),in);
129 memset(BSTR_GetAddr(out), 0, len+1);
134 /******************************************************************************
135 * SysAllocStringLen [OLEAUT32.4]
137 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
138 * section, he describes the DWORD value placed *before* the BSTR data type.
139 * he describes it as a "DWORD count of characters". By experimenting with
140 * a windows application, this count seems to be a DWORD count of bytes in
141 * the string. Meaning that the count is double the number of wide
142 * characters in the string.
144 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
151 * Find the length of the buffer passed-in in bytes.
153 bufferSize = len * sizeof (WCHAR);
156 * Allocate a new buffer to hold the string.
157 * dont't forget to keep an empty spot at the beginning of the
158 * buffer for the character count and an extra character at the
161 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
163 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
166 * If the memory allocation failed, return a null pointer.
172 * Copy the length of the string in the placeholder.
174 *newBuffer = bufferSize;
177 * Skip the byte count.
182 * Copy the information in the buffer.
183 * Since it is valid to pass a NULL pointer here, we'll initialize the
184 * buffer to nul if it is the case.
187 memcpy(newBuffer, in, bufferSize);
189 memset(newBuffer, 0, bufferSize);
192 * Make sure that there is a nul character at the end of the
195 stringBuffer = (WCHAR*)newBuffer;
196 stringBuffer[len] = L'\0';
198 return (LPWSTR)stringBuffer;
201 /******************************************************************************
202 * SysReAllocStringLen16 [OLE2DISP.5]
204 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
206 BSTR16 new=SysAllocStringLen16(in,len);
213 /******************************************************************************
214 * SysReAllocStringLen [OLEAUT32.5]
216 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
225 * Make sure we free the old string.
231 * Allocate the new string
233 *old = SysAllocStringLen(in, len);
238 /******************************************************************************
239 * SysFreeString16 [OLE2DISP.6]
241 void WINAPI SysFreeString16(BSTR16 in)
246 /******************************************************************************
247 * SysFreeString [OLEAUT32.6]
249 void WINAPI SysFreeString(BSTR in)
251 DWORD* bufferPointer;
253 /* NULL is a valid parameter */
257 * We have to be careful when we free a BSTR pointer, it points to
258 * the beginning of the string but it skips the byte count contained
261 bufferPointer = (DWORD*)in;
266 * Free the memory from it's "real" origin.
268 HeapFree(GetProcessHeap(), 0, bufferPointer);
271 /******************************************************************************
272 * SysStringLen16 [OLE2DISP.7]
274 int WINAPI SysStringLen16(BSTR16 str)
276 return strlen(BSTR_GetAddr(str));
279 /******************************************************************************
280 * SysStringLen [OLEAUT32.7]
282 * The Windows documentation states that the length returned by this function
283 * is not necessarely the same as the length returned by the _lstrlenW method.
284 * It is the same number that was passed in as the "len" parameter if the
285 * string was allocated with a SysAllocStringLen method call.
287 int WINAPI SysStringLen(BSTR str)
289 DWORD* bufferPointer;
293 * The length of the string (in bytes) is contained in a DWORD placed
294 * just before the BSTR pointer
296 bufferPointer = (DWORD*)str;
300 return (int)(*bufferPointer/sizeof(WCHAR));
303 /******************************************************************************
304 * SysStringByteLen [OLEAUT32.149]
306 * The Windows documentation states that the length returned by this function
307 * is not necessarely the same as the length returned by the _lstrlenW method.
308 * It is the same number that was passed in as the "len" parameter if the
309 * string was allocated with a SysAllocStringLen method call.
311 int WINAPI SysStringByteLen(BSTR str)
313 DWORD* bufferPointer;
317 * The length of the string (in bytes) is contained in a DWORD placed
318 * just before the BSTR pointer
320 bufferPointer = (DWORD*)str;
324 return (int)(*bufferPointer);
327 /******************************************************************************
328 * CreateDispTypeInfo16 [OLE2DISP.31]
330 HRESULT WINAPI CreateDispTypeInfo16(
331 INTERFACEDATA *pidata,
335 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
339 /******************************************************************************
340 * CreateDispTypeInfo [OLE2DISP.31]
342 HRESULT WINAPI CreateDispTypeInfo(
343 INTERFACEDATA *pidata,
347 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
351 /******************************************************************************
352 * CreateStdDispatch16 [OLE2DISP.32]
354 HRESULT WINAPI CreateStdDispatch16(
358 IUnknown** ppunkStdDisp)
360 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
365 /******************************************************************************
366 * CreateStdDispatch [OLE2DISP.32]
368 HRESULT WINAPI CreateStdDispatch(
372 IUnknown** ppunkStdDisp)
374 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
379 /******************************************************************************
380 * RegisterActiveObject [OLE2DISP.35]
382 HRESULT WINAPI RegisterActiveObject16(
383 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
385 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
389 /******************************************************************************
390 * OleTranslateColor [OLEAUT32.421]
392 * Converts an OLE_COLOR to a COLORREF.
393 * See the documentation for conversion rules.
394 * pColorRef can be NULL. In that case the user only wants to test the
397 HRESULT WINAPI OleTranslateColor(
403 BYTE b = HIBYTE(HIWORD(clr));
405 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
408 * In case pColorRef is NULL, provide our own to simplify the code.
410 if (pColorRef == NULL)
411 pColorRef = &colorref;
418 *pColorRef = PALETTERGB(GetRValue(clr),
433 * Validate the palette index.
435 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
450 int index = LOBYTE(LOWORD(clr));
453 * Validate GetSysColor index.
455 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
458 *pColorRef = GetSysColor(index);
470 /******************************************************************************
471 * SysAllocStringByteLen [OLEAUT32.150]
474 BSTR WINAPI SysAllocStringByteLen(LPCSTR in, UINT len)
480 * Allocate a new buffer to hold the string.
481 * dont't forget to keep an empty spot at the begining of the
482 * buffer for the character count and an extra character at the
485 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
487 len + sizeof(WCHAR) + sizeof(DWORD));
490 * If the memory allocation failed, return a null pointer.
496 * Copy the length of the string in the placeholder.
501 * Skip the byte count.
506 * Copy the information in the buffer.
507 * Since it is valid to pass a NULL pointer here, we'll initialize the
508 * buffer to nul if it is the case.
511 memcpy(newBuffer, in, len);
514 * Make sure that there is a nul character at the end of the
517 stringBuffer = (char *)newBuffer;
518 stringBuffer[len] = 0;
519 stringBuffer[len+1] = 0;
521 return (LPWSTR)stringBuffer;