4 * Copyright 1995 Martin von Loewis
11 #include "wine/windef16.h"
17 #include "debugtools.h"
19 DEFAULT_DEBUG_CHANNEL(ole);
21 /* This implementation of the BSTR API is 16-bit only. It
22 represents BSTR as a 16:16 far pointer, and the strings
25 /******************************************************************************
26 * BSTR_AllocBytes [Internal]
28 static BSTR16 BSTR_AllocBytes(int n)
30 void *ptr = SEGPTR_ALLOC(n);
31 return (BSTR16)SEGPTR_GET(ptr);
34 /******************************************************************************
35 * BSTR_Free [INTERNAL]
37 static void BSTR_Free(BSTR16 in)
39 SEGPTR_FREE( PTR_SEG_TO_LIN(in) );
42 /******************************************************************************
43 * BSTR_GetAddr [INTERNAL]
45 static void* BSTR_GetAddr(BSTR16 in)
47 return in ? PTR_SEG_TO_LIN(in) : 0;
50 /******************************************************************************
51 * SysAllocString16 [OLE2DISP.2]
53 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
59 out = BSTR_AllocBytes(strlen(in)+1);
61 strcpy(BSTR_GetAddr(out),in);
65 /******************************************************************************
66 * SysAllocString [OLEAUT32.2]
68 BSTR WINAPI SysAllocString(LPCOLESTR in)
72 /* Delegate this to the SysAllocStringLen32 method. */
73 return SysAllocStringLen(in, lstrlenW(in));
76 /******************************************************************************
77 * SysReAllocString16 [OLE2DISP.3]
79 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
81 BSTR16 new=SysAllocString16(in);
87 /******************************************************************************
88 * SysReAllocString [OLEAUT32.3]
90 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
99 * Make sure we free the old string.
105 * Allocate the new string
107 *old = SysAllocString(in);
112 /******************************************************************************
113 * SysAllocStringLen16 [OLE2DISP.4]
115 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
117 BSTR16 out=BSTR_AllocBytes(len+1);
123 * Copy the information in the buffer.
124 * Since it is valid to pass a NULL pointer here, we'll initialize the
125 * buffer to nul if it is the case.
128 strcpy(BSTR_GetAddr(out),in);
130 memset(BSTR_GetAddr(out), 0, len+1);
135 /******************************************************************************
136 * SysAllocStringLen [OLEAUT32.4]
138 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
139 * section, he describes the DWORD value placed before the BSTR data type.
140 * he describes it as a "DWORD count of characters". By experimenting with
141 * a windows application, this count seems to be a DWORD count of bytes in
142 * the string. Meaning that the count is double the number of wide
143 * characters in the string.
145 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
152 * Find the lenth of the buffer passed-in in bytes.
154 bufferSize = len * sizeof (WCHAR);
157 * Allocate a new buffer to hold the string.
158 * dont't forget to keep an empty spot at the begining of the
159 * buffer for the character count and an extra character at the
162 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
164 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
167 * If the memory allocation failed, return a null pointer.
173 * Copy the length of the string in the placeholder.
175 *newBuffer = bufferSize;
178 * Skip the byte count.
183 * Copy the information in the buffer.
184 * Since it is valid to pass a NULL pointer here, we'll initialize the
185 * buffer to nul if it is the case.
188 memcpy(newBuffer, in, bufferSize);
190 memset(newBuffer, 0, bufferSize);
193 * Make sure that there is a nul character at the end of the
196 stringBuffer = (WCHAR*)newBuffer;
197 stringBuffer[len] = L'\0';
199 return (LPWSTR)stringBuffer;
202 /******************************************************************************
203 * SysReAllocStringLen16 [OLE2DISP.5]
205 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
207 BSTR16 new=SysAllocStringLen16(in,len);
214 /******************************************************************************
215 * SysReAllocStringLen [OLEAUT32.5]
217 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
226 * Make sure we free the old string.
232 * Allocate the new string
234 *old = SysAllocStringLen(in, len);
239 /******************************************************************************
240 * SysFreeString16 [OLE2DISP.6]
242 void WINAPI SysFreeString16(BSTR16 in)
247 /******************************************************************************
248 * SysFreeString [OLEAUT32.6]
250 void WINAPI SysFreeString(BSTR in)
252 DWORD* bufferPointer;
254 /* NULL is a valid parameter */
258 * We have to be careful when we free a BSTR pointer, it points to
259 * the beginning of the string but it skips the byte count contained
262 bufferPointer = (DWORD*)in;
267 * Free the memory from it's "real" origin.
269 HeapFree(GetProcessHeap(), 0, bufferPointer);
272 /******************************************************************************
273 * SysStringLen16 [OLE2DISP.7]
275 int WINAPI SysStringLen16(BSTR16 str)
277 return strlen(BSTR_GetAddr(str));
280 /******************************************************************************
281 * SysStringLen [OLEAUT32.7]
283 * The Windows documentation states that the length returned by this function
284 * is not necessarely the same as the length returned by the _lstrlenW method.
285 * It is the same number that was passed in as the "len" parameter if the
286 * string was allocated with a SysAllocStringLen method call.
288 int WINAPI SysStringLen(BSTR str)
290 DWORD* bufferPointer;
294 * The length of the string (in bytes) is contained in a DWORD placed
295 * just before the BSTR pointer
297 bufferPointer = (DWORD*)str;
301 return (int)(*bufferPointer/sizeof(WCHAR));
304 /******************************************************************************
305 * SysStringByteLen [OLEAUT32.149]
307 * The Windows documentation states that the length returned by this function
308 * is not necessarely the same as the length returned by the _lstrlenW method.
309 * It is the same number that was passed in as the "len" parameter if the
310 * string was allocated with a SysAllocStringLen method call.
312 int WINAPI SysStringByteLen(BSTR str)
314 DWORD* bufferPointer;
318 * The length of the string (in bytes) is contained in a DWORD placed
319 * just before the BSTR pointer
321 bufferPointer = (DWORD*)str;
325 return (int)(*bufferPointer);
328 /******************************************************************************
329 * CreateDispTypeInfo16 [OLE2DISP.31]
331 HRESULT WINAPI CreateDispTypeInfo16(
332 INTERFACEDATA *pidata,
336 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
340 /******************************************************************************
341 * CreateDispTypeInfo [OLE2DISP.31]
343 HRESULT WINAPI CreateDispTypeInfo(
344 INTERFACEDATA *pidata,
348 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
352 /******************************************************************************
353 * CreateStdDispatch16 [OLE2DISP.32]
355 HRESULT WINAPI CreateStdDispatch16(
359 IUnknown** ppunkStdDisp)
361 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
366 /******************************************************************************
367 * CreateStdDispatch [OLE2DISP.32]
369 HRESULT WINAPI CreateStdDispatch(
373 IUnknown** ppunkStdDisp)
375 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
380 /******************************************************************************
381 * RegisterActiveObject [OLE2DISP.35]
383 HRESULT WINAPI RegisterActiveObject16(
384 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
386 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
390 /******************************************************************************
391 * OleTranslateColor [OLEAUT32.421]
393 * Converts an OLE_COLOR to a COLORREF.
394 * See the documentation for conversion rules.
395 * pColorRef can be NULL. In that case the user only wants to test the
398 HRESULT WINAPI OleTranslateColor(
404 BYTE b = HIBYTE(HIWORD(clr));
406 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
409 * In case pColorRef is NULL, provide our own to simplify the code.
411 if (pColorRef == NULL)
412 pColorRef = &colorref;
419 *pColorRef = PALETTERGB(GetRValue(clr),
434 * Validate the palette index.
436 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
451 int index = LOBYTE(LOWORD(clr));
454 * Validate GetSysColor index.
456 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
459 *pColorRef = GetSysColor(index);
471 /******************************************************************************
472 * SysAllocStringByteLen [OLEAUT32.150]
475 BSTR WINAPI SysAllocStringByteLen(LPCSTR in, UINT len)
481 * Allocate a new buffer to hold the string.
482 * dont't forget to keep an empty spot at the begining of the
483 * buffer for the character count and an extra character at the
486 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
488 len + sizeof(WCHAR) + sizeof(DWORD));
491 * If the memory allocation failed, return a null pointer.
497 * Copy the length of the string in the placeholder.
502 * Skip the byte count.
507 * Copy the information in the buffer.
508 * Since it is valid to pass a NULL pointer here, we'll initialize the
509 * buffer to nul if it is the case.
512 memcpy(newBuffer, in, len);
515 * Make sure that there is a nul character at the end of the
518 stringBuffer = (char *)newBuffer;
519 stringBuffer[len] = 0;
520 stringBuffer[len+1] = 0;
522 return (LPWSTR)stringBuffer;