4 * Copyright 1995 Martin von Loewis
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(ole);
19 /* This implementation of the BSTR API is 16-bit only. It
20 represents BSTR as a 16:16 far pointer, and the strings
23 /******************************************************************************
24 * BSTR_AllocBytes [Internal]
26 static BSTR16 BSTR_AllocBytes(int n)
28 void *ptr = SEGPTR_ALLOC(n);
29 return (BSTR16)SEGPTR_GET(ptr);
32 /******************************************************************************
33 * BSTR_Free [INTERNAL]
35 static void BSTR_Free(BSTR16 in)
37 SEGPTR_FREE( PTR_SEG_TO_LIN(in) );
40 /******************************************************************************
41 * BSTR_GetAddr [INTERNAL]
43 static void* BSTR_GetAddr(BSTR16 in)
45 return in ? PTR_SEG_TO_LIN(in) : 0;
48 /******************************************************************************
49 * SysAllocString16 [OLE2DISP.2]
51 BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
53 BSTR16 out=BSTR_AllocBytes(strlen(in)+1);
55 strcpy(BSTR_GetAddr(out),in);
59 /******************************************************************************
60 * SysAllocString32 [OLEAUT32.2]
62 BSTR WINAPI SysAllocString(LPCOLESTR in)
64 /* Delegate this to the SysAllocStringLen32 method. */
65 return SysAllocStringLen(in, lstrlenW(in));
68 /******************************************************************************
69 * SysReAllocString16 [OLE2DISP.3]
71 INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
73 BSTR16 new=SysAllocString16(in);
79 /******************************************************************************
80 * SysReAllocString32 [OLEAUT32.3]
82 INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
91 * Make sure we free the old string.
97 * Allocate the new string
99 *old = SysAllocString(in);
104 /******************************************************************************
105 * SysAllocStringLen16 [OLE2DISP.4]
107 BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
109 BSTR16 out=BSTR_AllocBytes(len+1);
115 * Copy the information in the buffer.
116 * Since it is valid to pass a NULL pointer here, we'll initialize the
117 * buffer to nul if it is the case.
120 strcpy(BSTR_GetAddr(out),in);
122 memset(BSTR_GetAddr(out), 0, len+1);
127 /******************************************************************************
128 * SysAllocStringLen32 [OLEAUT32.4]
130 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
131 * section, he describes the DWORD value placed before the BSTR data type.
132 * he describes it as a "DWORD count of characters". By experimenting with
133 * a windows application, this count seems to be a DWORD count of bytes in
134 * the string. Meaning that the count is double the number of wide
135 * characters in the string.
137 BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
144 * Find the lenth of the buffer passed-in in bytes.
146 bufferSize = len * sizeof (WCHAR);
149 * Allocate a new buffer to hold the string.
150 * dont't forget to keep an empty spot at the begining of the
151 * buffer for the character count and an extra character at the
154 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
156 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
159 * If the memory allocation failed, return a null pointer.
165 * Copy the length of the string in the placeholder.
167 *newBuffer = bufferSize;
170 * Skip the byte count.
175 * Copy the information in the buffer.
176 * Since it is valid to pass a NULL pointer here, we'll initialize the
177 * buffer to nul if it is the case.
180 memcpy(newBuffer, in, bufferSize);
182 memset(newBuffer, 0, bufferSize);
185 * Make sure that there is a nul character at the end of the
188 stringBuffer = (WCHAR*)newBuffer;
189 stringBuffer[len] = L'\0';
191 return (LPWSTR)stringBuffer;
194 /******************************************************************************
195 * SysReAllocStringLen16 [OLE2DISP.5]
197 int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
199 BSTR16 new=SysAllocStringLen16(in,len);
206 /******************************************************************************
207 * SysReAllocStringLen32 [OLEAUT32.5]
209 int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
218 * Make sure we free the old string.
224 * Allocate the new string
226 *old = SysAllocStringLen(in, len);
231 /******************************************************************************
232 * SysFreeString16 [OLE2DISP.6]
234 void WINAPI SysFreeString16(BSTR16 in)
239 /******************************************************************************
240 * SysFreeString32 [OLEAUT32.6]
242 void WINAPI SysFreeString(BSTR in)
244 DWORD* bufferPointer;
246 /* NULL is a valid parameter */
250 * We have to be careful when we free a BSTR pointer, it points to
251 * the beginning of the string but it skips the byte count contained
254 bufferPointer = (DWORD*)in;
259 * Free the memory from it's "real" origin.
261 HeapFree(GetProcessHeap(), 0, bufferPointer);
264 /******************************************************************************
265 * SysStringLen16 [OLE2DISP.7]
267 int WINAPI SysStringLen16(BSTR16 str)
269 return strlen(BSTR_GetAddr(str));
272 /******************************************************************************
273 * SysStringLen32 [OLEAUT32.7]
275 * The Windows documentation states that the length returned by this function
276 * is not necessarely the same as the length returned by the _lstrlenW method.
277 * It is the same number that was passed in as the "len" parameter if the
278 * string was allocated with a SysAllocStringLen method call.
280 int WINAPI SysStringLen(BSTR str)
282 DWORD* bufferPointer;
286 * The length of the string (in bytes) is contained in a DWORD placed
287 * just before the BSTR pointer
289 bufferPointer = (DWORD*)str;
293 return (int)(*bufferPointer/sizeof(WCHAR));
296 /******************************************************************************
297 * SysStringByteLen [OLEAUT32.149]
299 * The Windows documentation states that the length returned by this function
300 * is not necessarely the same as the length returned by the _lstrlenW method.
301 * It is the same number that was passed in as the "len" parameter if the
302 * string was allocated with a SysAllocStringLen method call.
304 int WINAPI SysStringByteLen(BSTR str)
306 return SysStringLen(str)*sizeof(WCHAR);
309 /******************************************************************************
310 * CreateDispTypeInfo [OLE2DISP.31]
312 HRESULT WINAPI CreateDispTypeInfo16(
313 INTERFACEDATA *pidata,
317 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
321 /******************************************************************************
322 * RegisterActiveObject [OLE2DISP.35]
324 HRESULT WINAPI RegisterActiveObject16(
325 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
327 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
331 /******************************************************************************
332 * OleTranslateColor [OLEAUT32.421]
334 * Converts an OLE_COLOR to a COLORREF.
335 * See the documentation for conversion rules.
336 * pColorRef can be NULL. In that case the user only wants to test the
339 HRESULT WINAPI OleTranslateColor(
345 BYTE b = HIBYTE(HIWORD(clr));
347 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
350 * In case pColorRef is NULL, provide our own to simplify the code.
352 if (pColorRef == NULL)
353 pColorRef = &colorref;
360 *pColorRef = PALETTERGB(GetRValue(clr),
375 * Validate the palette index.
377 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
392 int index = LOBYTE(LOWORD(clr));
395 * Validate GetSysColor index.
397 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
400 *pColorRef = GetSysColor(index);
412 /******************************************************************************
413 * SysAllocStringByteLen [OLEAUT32.150]
416 BSTR WINAPI SysAllocStringByteLen(char *in, int len)
422 * Allocate a new buffer to hold the string.
423 * dont't forget to keep an empty spot at the begining of the
424 * buffer for the character count and an extra character at the
427 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
429 len + sizeof(WCHAR) + sizeof(DWORD));
432 * If the memory allocation failed, return a null pointer.
438 * Copy the length of the string in the placeholder.
443 * Skip the byte count.
448 * Copy the information in the buffer.
449 * Since it is valid to pass a NULL pointer here, we'll initialize the
450 * buffer to nul if it is the case.
453 memcpy(newBuffer, in, len);
456 * Make sure that there is a nul character at the end of the
459 stringBuffer = (char *)newBuffer;
460 stringBuffer[len] = 0;
461 stringBuffer[len+1] = 0;
463 return (LPWSTR)stringBuffer;