4 * Copyright 1995 Martin von Loewis
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
= SEGPTR_ALLOC(n
);
37 return (BSTR16
)SEGPTR_GET(ptr
);
40 /******************************************************************************
41 * BSTR_Free [INTERNAL]
43 static void BSTR_Free(BSTR16 in
)
45 SEGPTR_FREE( MapSL((SEGPTR
)in
) );
48 /******************************************************************************
49 * BSTR_GetAddr [INTERNAL]
51 static void* BSTR_GetAddr(BSTR16 in
)
53 return in
? MapSL((SEGPTR
)in
) : 0;
56 /******************************************************************************
57 * SysAllocString [OLE2DISP.2]
59 BSTR16 WINAPI
SysAllocString16(LPCOLESTR16 in
)
65 out
= BSTR_AllocBytes(strlen(in
)+1);
67 strcpy(BSTR_GetAddr(out
),in
);
71 /******************************************************************************
72 * SysAllocString [OLEAUT32.2]
74 BSTR WINAPI
SysAllocString(LPCOLESTR in
)
78 /* Delegate this to the SysAllocStringLen32 method. */
79 return SysAllocStringLen(in
, lstrlenW(in
));
82 /******************************************************************************
83 * SysReallocString [OLE2DISP.3]
85 INT16 WINAPI
SysReAllocString16(LPBSTR16 old
,LPCOLESTR16 in
)
87 BSTR16
new=SysAllocString16(in
);
93 /******************************************************************************
94 * SysReAllocString [OLEAUT32.3]
96 INT WINAPI
SysReAllocString(LPBSTR old
,LPCOLESTR in
)
105 * Make sure we free the old string.
111 * Allocate the new string
113 *old
= SysAllocString(in
);
118 /******************************************************************************
119 * SysAllocStringLen [OLE2DISP.4]
121 BSTR16 WINAPI
SysAllocStringLen16(const char *in
, int len
)
123 BSTR16 out
=BSTR_AllocBytes(len
+1);
129 * Copy the information in the buffer.
130 * Since it is valid to pass a NULL pointer here, we'll initialize the
131 * buffer to nul if it is the case.
134 strcpy(BSTR_GetAddr(out
),in
);
136 memset(BSTR_GetAddr(out
), 0, len
+1);
141 /******************************************************************************
142 * SysAllocStringLen [OLEAUT32.4]
144 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
145 * section, he describes the DWORD value placed *before* the BSTR data type.
146 * he describes it as a "DWORD count of characters". By experimenting with
147 * a windows application, this count seems to be a DWORD count of bytes in
148 * the string. Meaning that the count is double the number of wide
149 * characters in the string.
151 BSTR WINAPI
SysAllocStringLen(const OLECHAR
*in
, unsigned int len
)
158 * Find the length of the buffer passed-in in bytes.
160 bufferSize
= len
* sizeof (WCHAR
);
163 * Allocate a new buffer to hold the string.
164 * dont't forget to keep an empty spot at the beginning of the
165 * buffer for the character count and an extra character at the
168 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
170 bufferSize
+ sizeof(WCHAR
) + sizeof(DWORD
));
173 * If the memory allocation failed, return a null pointer.
179 * Copy the length of the string in the placeholder.
181 *newBuffer
= bufferSize
;
184 * Skip the byte count.
189 * Copy the information in the buffer.
190 * Since it is valid to pass a NULL pointer here, we'll initialize the
191 * buffer to nul if it is the case.
194 memcpy(newBuffer
, in
, bufferSize
);
196 memset(newBuffer
, 0, bufferSize
);
199 * Make sure that there is a nul character at the end of the
202 stringBuffer
= (WCHAR
*)newBuffer
;
203 stringBuffer
[len
] = L
'\0';
205 return (LPWSTR
)stringBuffer
;
208 /******************************************************************************
209 * SysReAllocStringLen [OLE2DISP.5]
211 int WINAPI
SysReAllocStringLen16(BSTR16
*old
,const char *in
,int len
)
213 BSTR16
new=SysAllocStringLen16(in
,len
);
220 /******************************************************************************
221 * SysReAllocStringLen [OLEAUT32.5]
223 int WINAPI
SysReAllocStringLen(BSTR
* old
, const OLECHAR
* in
, unsigned int len
)
232 * Make sure we free the old string.
238 * Allocate the new string
240 *old
= SysAllocStringLen(in
, len
);
245 /******************************************************************************
246 * SysFreeString [OLE2DISP.6]
248 void WINAPI
SysFreeString16(BSTR16 in
)
253 /******************************************************************************
254 * SysFreeString [OLEAUT32.6]
256 void WINAPI
SysFreeString(BSTR in
)
258 DWORD
* bufferPointer
;
260 /* NULL is a valid parameter */
264 * We have to be careful when we free a BSTR pointer, it points to
265 * the beginning of the string but it skips the byte count contained
268 bufferPointer
= (DWORD
*)in
;
273 * Free the memory from its "real" origin.
275 HeapFree(GetProcessHeap(), 0, bufferPointer
);
278 /******************************************************************************
279 * SysStringLen [OLE2DISP.7]
281 int WINAPI
SysStringLen16(BSTR16 str
)
283 return strlen(BSTR_GetAddr(str
));
286 /******************************************************************************
287 * SysStringLen [OLEAUT32.7]
289 * The Windows documentation states that the length returned by this function
290 * is not necessarely the same as the length returned by the _lstrlenW method.
291 * It is the same number that was passed in as the "len" parameter if the
292 * string was allocated with a SysAllocStringLen method call.
294 int WINAPI
SysStringLen(BSTR str
)
296 DWORD
* bufferPointer
;
300 * The length of the string (in bytes) is contained in a DWORD placed
301 * just before the BSTR pointer
303 bufferPointer
= (DWORD
*)str
;
307 return (int)(*bufferPointer
/sizeof(WCHAR
));
310 /******************************************************************************
311 * SysStringByteLen [OLEAUT32.149]
313 * The Windows documentation states that the length returned by this function
314 * is not necessarely the same as the length returned by the _lstrlenW method.
315 * It is the same number that was passed in as the "len" parameter if the
316 * string was allocated with a SysAllocStringLen method call.
318 int WINAPI
SysStringByteLen(BSTR str
)
320 DWORD
* bufferPointer
;
324 * The length of the string (in bytes) is contained in a DWORD placed
325 * just before the BSTR pointer
327 bufferPointer
= (DWORD
*)str
;
331 return (int)(*bufferPointer
);
334 /******************************************************************************
335 * CreateDispTypeInfo [OLE2DISP.31]
337 HRESULT WINAPI
CreateDispTypeInfo16(
338 INTERFACEDATA
*pidata
,
342 FIXME("(%p,%ld,%p),stub\n",pidata
,lcid
,pptinfo
);
346 /******************************************************************************
347 * CreateDispTypeInfo [OLEAUT32.31]
349 HRESULT WINAPI
CreateDispTypeInfo(
350 INTERFACEDATA
*pidata
,
354 FIXME("(%p,%ld,%p),stub\n",pidata
,lcid
,pptinfo
);
358 /******************************************************************************
359 * CreateStdDispatch [OLE2DISP.32]
361 HRESULT WINAPI
CreateStdDispatch16(
365 IUnknown
** ppunkStdDisp
)
367 FIXME("(%p,%p,%p,%p),stub\n",punkOuter
, pvThis
, ptinfo
,
372 /******************************************************************************
373 * CreateStdDispatch [OLEAUT32.32]
375 HRESULT WINAPI
CreateStdDispatch(
379 IUnknown
** ppunkStdDisp
)
381 FIXME("(%p,%p,%p,%p),stub\n",punkOuter
, pvThis
, ptinfo
,
386 /******************************************************************************
387 * RegisterActiveObject [OLE2DISP.35]
389 HRESULT WINAPI
RegisterActiveObject16(
390 IUnknown
*punk
, REFCLSID rclsid
, DWORD dwFlags
, unsigned long *pdwRegister
392 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk
,debugstr_guid(rclsid
),dwFlags
,pdwRegister
);
396 /******************************************************************************
397 * OleTranslateColor [OLEAUT32.421]
399 * Converts an OLE_COLOR to a COLORREF.
400 * See the documentation for conversion rules.
401 * pColorRef can be NULL. In that case the user only wants to test the
404 HRESULT WINAPI
OleTranslateColor(
410 BYTE b
= HIBYTE(HIWORD(clr
));
412 TRACE("(%08lx, %d, %p):stub\n", clr
, hpal
, pColorRef
);
415 * In case pColorRef is NULL, provide our own to simplify the code.
417 if (pColorRef
== NULL
)
418 pColorRef
= &colorref
;
425 *pColorRef
= PALETTERGB(GetRValue(clr
),
440 * Validate the palette index.
442 if (GetPaletteEntries(hpal
, LOWORD(clr
), 1, &pe
) == 0)
457 int index
= LOBYTE(LOWORD(clr
));
460 * Validate GetSysColor index.
462 if ((index
< COLOR_SCROLLBAR
) || (index
> COLOR_GRADIENTINACTIVECAPTION
))
465 *pColorRef
= GetSysColor(index
);
477 /******************************************************************************
478 * SysAllocStringByteLen [OLEAUT32.150]
481 BSTR WINAPI
SysAllocStringByteLen(LPCSTR in
, UINT len
)
487 * Allocate a new buffer to hold the string.
488 * dont't forget to keep an empty spot at the beginning of the
489 * buffer for the character count and an extra character at the
492 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
494 len
+ sizeof(WCHAR
) + sizeof(DWORD
));
497 * If the memory allocation failed, return a null pointer.
503 * Copy the length of the string in the placeholder.
508 * Skip the byte count.
513 * Copy the information in the buffer.
514 * Since it is valid to pass a NULL pointer here, we'll initialize the
515 * buffer to nul if it is the case.
518 memcpy(newBuffer
, in
, len
);
521 * Make sure that there is a nul character at the end of the
524 stringBuffer
= (char *)newBuffer
;
525 stringBuffer
[len
] = 0;
526 stringBuffer
[len
+1] = 0;
528 return (LPWSTR
)stringBuffer
;