4 * Copyright 1995 Martin von Loewis
13 #include "wine/windef16.h"
18 #include "debugtools.h"
20 DEFAULT_DEBUG_CHANNEL(ole
);
22 /* This implementation of the BSTR API is 16-bit only. It
23 represents BSTR as a 16:16 far pointer, and the strings
26 /******************************************************************************
27 * BSTR_AllocBytes [Internal]
29 static BSTR16
BSTR_AllocBytes(int n
)
31 void *ptr
= SEGPTR_ALLOC(n
);
32 return (BSTR16
)SEGPTR_GET(ptr
);
35 /******************************************************************************
36 * BSTR_Free [INTERNAL]
38 static void BSTR_Free(BSTR16 in
)
40 SEGPTR_FREE( MapSL((SEGPTR
)in
) );
43 /******************************************************************************
44 * BSTR_GetAddr [INTERNAL]
46 static void* BSTR_GetAddr(BSTR16 in
)
48 return in
? MapSL((SEGPTR
)in
) : 0;
51 /******************************************************************************
52 * SysAllocString [OLE2DISP.2]
54 BSTR16 WINAPI
SysAllocString16(LPCOLESTR16 in
)
60 out
= BSTR_AllocBytes(strlen(in
)+1);
62 strcpy(BSTR_GetAddr(out
),in
);
66 /******************************************************************************
67 * SysAllocString [OLEAUT32.2]
69 BSTR WINAPI
SysAllocString(LPCOLESTR in
)
73 /* Delegate this to the SysAllocStringLen32 method. */
74 return SysAllocStringLen(in
, lstrlenW(in
));
77 /******************************************************************************
78 * SysReallocString [OLE2DISP.3]
80 INT16 WINAPI
SysReAllocString16(LPBSTR16 old
,LPCOLESTR16 in
)
82 BSTR16
new=SysAllocString16(in
);
88 /******************************************************************************
89 * SysReAllocString [OLEAUT32.3]
91 INT WINAPI
SysReAllocString(LPBSTR old
,LPCOLESTR in
)
100 * Make sure we free the old string.
106 * Allocate the new string
108 *old
= SysAllocString(in
);
113 /******************************************************************************
114 * SysAllocStringLen [OLE2DISP.4]
116 BSTR16 WINAPI
SysAllocStringLen16(const char *in
, int len
)
118 BSTR16 out
=BSTR_AllocBytes(len
+1);
124 * Copy the information in the buffer.
125 * Since it is valid to pass a NULL pointer here, we'll initialize the
126 * buffer to nul if it is the case.
129 strcpy(BSTR_GetAddr(out
),in
);
131 memset(BSTR_GetAddr(out
), 0, len
+1);
136 /******************************************************************************
137 * SysAllocStringLen [OLEAUT32.4]
139 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
140 * section, he describes the DWORD value placed *before* the BSTR data type.
141 * he describes it as a "DWORD count of characters". By experimenting with
142 * a windows application, this count seems to be a DWORD count of bytes in
143 * the string. Meaning that the count is double the number of wide
144 * characters in the string.
146 BSTR WINAPI
SysAllocStringLen(const OLECHAR
*in
, unsigned int len
)
153 * Find the length of the buffer passed-in in bytes.
155 bufferSize
= len
* sizeof (WCHAR
);
158 * Allocate a new buffer to hold the string.
159 * dont't forget to keep an empty spot at the beginning of the
160 * buffer for the character count and an extra character at the
163 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
165 bufferSize
+ sizeof(WCHAR
) + sizeof(DWORD
));
168 * If the memory allocation failed, return a null pointer.
174 * Copy the length of the string in the placeholder.
176 *newBuffer
= bufferSize
;
179 * Skip the byte count.
184 * Copy the information in the buffer.
185 * Since it is valid to pass a NULL pointer here, we'll initialize the
186 * buffer to nul if it is the case.
189 memcpy(newBuffer
, in
, bufferSize
);
191 memset(newBuffer
, 0, bufferSize
);
194 * Make sure that there is a nul character at the end of the
197 stringBuffer
= (WCHAR
*)newBuffer
;
198 stringBuffer
[len
] = L
'\0';
200 return (LPWSTR
)stringBuffer
;
203 /******************************************************************************
204 * SysReAllocStringLen [OLE2DISP.5]
206 int WINAPI
SysReAllocStringLen16(BSTR16
*old
,const char *in
,int len
)
208 BSTR16
new=SysAllocStringLen16(in
,len
);
215 /******************************************************************************
216 * SysReAllocStringLen [OLEAUT32.5]
218 int WINAPI
SysReAllocStringLen(BSTR
* old
, const OLECHAR
* in
, unsigned int len
)
227 * Make sure we free the old string.
233 * Allocate the new string
235 *old
= SysAllocStringLen(in
, len
);
240 /******************************************************************************
241 * SysFreeString [OLE2DISP.6]
243 void WINAPI
SysFreeString16(BSTR16 in
)
248 /******************************************************************************
249 * SysFreeString [OLEAUT32.6]
251 void WINAPI
SysFreeString(BSTR in
)
253 DWORD
* bufferPointer
;
255 /* NULL is a valid parameter */
259 * We have to be careful when we free a BSTR pointer, it points to
260 * the beginning of the string but it skips the byte count contained
263 bufferPointer
= (DWORD
*)in
;
268 * Free the memory from it's "real" origin.
270 HeapFree(GetProcessHeap(), 0, bufferPointer
);
273 /******************************************************************************
274 * SysStringLen [OLE2DISP.7]
276 int WINAPI
SysStringLen16(BSTR16 str
)
278 return strlen(BSTR_GetAddr(str
));
281 /******************************************************************************
282 * SysStringLen [OLEAUT32.7]
284 * The Windows documentation states that the length returned by this function
285 * is not necessarely the same as the length returned by the _lstrlenW method.
286 * It is the same number that was passed in as the "len" parameter if the
287 * string was allocated with a SysAllocStringLen method call.
289 int WINAPI
SysStringLen(BSTR str
)
291 DWORD
* bufferPointer
;
295 * The length of the string (in bytes) is contained in a DWORD placed
296 * just before the BSTR pointer
298 bufferPointer
= (DWORD
*)str
;
302 return (int)(*bufferPointer
/sizeof(WCHAR
));
305 /******************************************************************************
306 * SysStringByteLen [OLEAUT32.149]
308 * The Windows documentation states that the length returned by this function
309 * is not necessarely the same as the length returned by the _lstrlenW method.
310 * It is the same number that was passed in as the "len" parameter if the
311 * string was allocated with a SysAllocStringLen method call.
313 int WINAPI
SysStringByteLen(BSTR str
)
315 DWORD
* bufferPointer
;
319 * The length of the string (in bytes) is contained in a DWORD placed
320 * just before the BSTR pointer
322 bufferPointer
= (DWORD
*)str
;
326 return (int)(*bufferPointer
);
329 /******************************************************************************
330 * CreateDispTypeInfo [OLE2DISP.31]
332 HRESULT WINAPI
CreateDispTypeInfo16(
333 INTERFACEDATA
*pidata
,
337 FIXME("(%p,%ld,%p),stub\n",pidata
,lcid
,pptinfo
);
341 /******************************************************************************
342 * CreateDispTypeInfo [OLE2DISP.31]
344 HRESULT WINAPI
CreateDispTypeInfo(
345 INTERFACEDATA
*pidata
,
349 FIXME("(%p,%ld,%p),stub\n",pidata
,lcid
,pptinfo
);
353 /******************************************************************************
354 * CreateStdDispatch [OLE2DISP.32]
356 HRESULT WINAPI
CreateStdDispatch16(
360 IUnknown
** ppunkStdDisp
)
362 FIXME("(%p,%p,%p,%p),stub\n",punkOuter
, pvThis
, ptinfo
,
367 /******************************************************************************
368 * CreateStdDispatch [OLE2DISP.32]
370 HRESULT WINAPI
CreateStdDispatch(
374 IUnknown
** ppunkStdDisp
)
376 FIXME("(%p,%p,%p,%p),stub\n",punkOuter
, pvThis
, ptinfo
,
381 /******************************************************************************
382 * RegisterActiveObject [OLE2DISP.35]
384 HRESULT WINAPI
RegisterActiveObject16(
385 IUnknown
*punk
, REFCLSID rclsid
, DWORD dwFlags
, unsigned long *pdwRegister
387 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk
,debugstr_guid(rclsid
),dwFlags
,pdwRegister
);
391 /******************************************************************************
392 * OleTranslateColor [OLEAUT32.421]
394 * Converts an OLE_COLOR to a COLORREF.
395 * See the documentation for conversion rules.
396 * pColorRef can be NULL. In that case the user only wants to test the
399 HRESULT WINAPI
OleTranslateColor(
405 BYTE b
= HIBYTE(HIWORD(clr
));
407 TRACE("(%08lx, %d, %p):stub\n", clr
, hpal
, pColorRef
);
410 * In case pColorRef is NULL, provide our own to simplify the code.
412 if (pColorRef
== NULL
)
413 pColorRef
= &colorref
;
420 *pColorRef
= PALETTERGB(GetRValue(clr
),
435 * Validate the palette index.
437 if (GetPaletteEntries(hpal
, LOWORD(clr
), 1, &pe
) == 0)
452 int index
= LOBYTE(LOWORD(clr
));
455 * Validate GetSysColor index.
457 if ((index
< COLOR_SCROLLBAR
) || (index
> COLOR_GRADIENTINACTIVECAPTION
))
460 *pColorRef
= GetSysColor(index
);
472 /******************************************************************************
473 * SysAllocStringByteLen [OLEAUT32.150]
476 BSTR WINAPI
SysAllocStringByteLen(LPCSTR in
, UINT len
)
482 * Allocate a new buffer to hold the string.
483 * dont't forget to keep an empty spot at the begining of the
484 * buffer for the character count and an extra character at the
487 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
489 len
+ sizeof(WCHAR
) + sizeof(DWORD
));
492 * If the memory allocation failed, return a null pointer.
498 * Copy the length of the string in the placeholder.
503 * Skip the byte count.
508 * Copy the information in the buffer.
509 * Since it is valid to pass a NULL pointer here, we'll initialize the
510 * buffer to nul if it is the case.
513 memcpy(newBuffer
, in
, len
);
516 * Make sure that there is a nul character at the end of the
519 stringBuffer
= (char *)newBuffer
;
520 stringBuffer
[len
] = 0;
521 stringBuffer
[len
+1] = 0;
523 return (LPWSTR
)stringBuffer
;