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
)
57 out
= BSTR_AllocBytes(strlen(in
)+1);
59 strcpy(BSTR_GetAddr(out
),in
);
63 /******************************************************************************
64 * SysAllocString [OLEAUT32.2]
66 BSTR WINAPI
SysAllocString(LPCOLESTR in
)
70 /* Delegate this to the SysAllocStringLen32 method. */
71 return SysAllocStringLen(in
, lstrlenW(in
));
74 /******************************************************************************
75 * SysReAllocString16 [OLE2DISP.3]
77 INT16 WINAPI
SysReAllocString16(LPBSTR16 old
,LPCOLESTR16 in
)
79 BSTR16
new=SysAllocString16(in
);
85 /******************************************************************************
86 * SysReAllocString [OLEAUT32.3]
88 INT WINAPI
SysReAllocString(LPBSTR old
,LPCOLESTR in
)
97 * Make sure we free the old string.
103 * Allocate the new string
105 *old
= SysAllocString(in
);
110 /******************************************************************************
111 * SysAllocStringLen16 [OLE2DISP.4]
113 BSTR16 WINAPI
SysAllocStringLen16(const char *in
, int len
)
115 BSTR16 out
=BSTR_AllocBytes(len
+1);
121 * Copy the information in the buffer.
122 * Since it is valid to pass a NULL pointer here, we'll initialize the
123 * buffer to nul if it is the case.
126 strcpy(BSTR_GetAddr(out
),in
);
128 memset(BSTR_GetAddr(out
), 0, len
+1);
133 /******************************************************************************
134 * SysAllocStringLen [OLEAUT32.4]
136 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
137 * section, he describes the DWORD value placed before the BSTR data type.
138 * he describes it as a "DWORD count of characters". By experimenting with
139 * a windows application, this count seems to be a DWORD count of bytes in
140 * the string. Meaning that the count is double the number of wide
141 * characters in the string.
143 BSTR WINAPI
SysAllocStringLen(const OLECHAR
*in
, unsigned int len
)
150 * Find the lenth of the buffer passed-in in bytes.
152 bufferSize
= len
* sizeof (WCHAR
);
155 * Allocate a new buffer to hold the string.
156 * dont't forget to keep an empty spot at the begining of the
157 * buffer for the character count and an extra character at the
160 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
162 bufferSize
+ sizeof(WCHAR
) + sizeof(DWORD
));
165 * If the memory allocation failed, return a null pointer.
171 * Copy the length of the string in the placeholder.
173 *newBuffer
= bufferSize
;
176 * Skip the byte count.
181 * Copy the information in the buffer.
182 * Since it is valid to pass a NULL pointer here, we'll initialize the
183 * buffer to nul if it is the case.
186 memcpy(newBuffer
, in
, bufferSize
);
188 memset(newBuffer
, 0, bufferSize
);
191 * Make sure that there is a nul character at the end of the
194 stringBuffer
= (WCHAR
*)newBuffer
;
195 stringBuffer
[len
] = L
'\0';
197 return (LPWSTR
)stringBuffer
;
200 /******************************************************************************
201 * SysReAllocStringLen16 [OLE2DISP.5]
203 int WINAPI
SysReAllocStringLen16(BSTR16
*old
,const char *in
,int len
)
205 BSTR16
new=SysAllocStringLen16(in
,len
);
212 /******************************************************************************
213 * SysReAllocStringLen [OLEAUT32.5]
215 int WINAPI
SysReAllocStringLen(BSTR
* old
, const OLECHAR
* in
, unsigned int len
)
224 * Make sure we free the old string.
230 * Allocate the new string
232 *old
= SysAllocStringLen(in
, len
);
237 /******************************************************************************
238 * SysFreeString16 [OLE2DISP.6]
240 void WINAPI
SysFreeString16(BSTR16 in
)
245 /******************************************************************************
246 * SysFreeString [OLEAUT32.6]
248 void WINAPI
SysFreeString(BSTR in
)
250 DWORD
* bufferPointer
;
252 /* NULL is a valid parameter */
256 * We have to be careful when we free a BSTR pointer, it points to
257 * the beginning of the string but it skips the byte count contained
260 bufferPointer
= (DWORD
*)in
;
265 * Free the memory from it's "real" origin.
267 HeapFree(GetProcessHeap(), 0, bufferPointer
);
270 /******************************************************************************
271 * SysStringLen16 [OLE2DISP.7]
273 int WINAPI
SysStringLen16(BSTR16 str
)
275 return strlen(BSTR_GetAddr(str
));
278 /******************************************************************************
279 * SysStringLen [OLEAUT32.7]
281 * The Windows documentation states that the length returned by this function
282 * is not necessarely the same as the length returned by the _lstrlenW method.
283 * It is the same number that was passed in as the "len" parameter if the
284 * string was allocated with a SysAllocStringLen method call.
286 int WINAPI
SysStringLen(BSTR str
)
288 DWORD
* bufferPointer
;
292 * The length of the string (in bytes) is contained in a DWORD placed
293 * just before the BSTR pointer
295 bufferPointer
= (DWORD
*)str
;
299 return (int)(*bufferPointer
/sizeof(WCHAR
));
302 /******************************************************************************
303 * SysStringByteLen [OLEAUT32.149]
305 * The Windows documentation states that the length returned by this function
306 * is not necessarely the same as the length returned by the _lstrlenW method.
307 * It is the same number that was passed in as the "len" parameter if the
308 * string was allocated with a SysAllocStringLen method call.
310 int WINAPI
SysStringByteLen(BSTR str
)
312 DWORD
* bufferPointer
;
316 * The length of the string (in bytes) is contained in a DWORD placed
317 * just before the BSTR pointer
319 bufferPointer
= (DWORD
*)str
;
323 return (int)(*bufferPointer
);
326 /******************************************************************************
327 * CreateDispTypeInfo [OLE2DISP.31]
329 HRESULT WINAPI
CreateDispTypeInfo16(
330 INTERFACEDATA
*pidata
,
334 FIXME("(%p,%ld,%p),stub\n",pidata
,lcid
,pptinfo
);
338 /******************************************************************************
339 * RegisterActiveObject [OLE2DISP.35]
341 HRESULT WINAPI
RegisterActiveObject16(
342 IUnknown
*punk
, REFCLSID rclsid
, DWORD dwFlags
, unsigned long *pdwRegister
344 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk
,debugstr_guid(rclsid
),dwFlags
,pdwRegister
);
348 /******************************************************************************
349 * OleTranslateColor [OLEAUT32.421]
351 * Converts an OLE_COLOR to a COLORREF.
352 * See the documentation for conversion rules.
353 * pColorRef can be NULL. In that case the user only wants to test the
356 HRESULT WINAPI
OleTranslateColor(
362 BYTE b
= HIBYTE(HIWORD(clr
));
364 TRACE("(%08lx, %d, %p):stub\n", clr
, hpal
, pColorRef
);
367 * In case pColorRef is NULL, provide our own to simplify the code.
369 if (pColorRef
== NULL
)
370 pColorRef
= &colorref
;
377 *pColorRef
= PALETTERGB(GetRValue(clr
),
392 * Validate the palette index.
394 if (GetPaletteEntries(hpal
, LOWORD(clr
), 1, &pe
) == 0)
409 int index
= LOBYTE(LOWORD(clr
));
412 * Validate GetSysColor index.
414 if ((index
< COLOR_SCROLLBAR
) || (index
> COLOR_GRADIENTINACTIVECAPTION
))
417 *pColorRef
= GetSysColor(index
);
429 /******************************************************************************
430 * SysAllocStringByteLen [OLEAUT32.150]
433 BSTR WINAPI
SysAllocStringByteLen(char *in
, int len
)
439 * Allocate a new buffer to hold the string.
440 * dont't forget to keep an empty spot at the begining of the
441 * buffer for the character count and an extra character at the
444 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
446 len
+ sizeof(WCHAR
) + sizeof(DWORD
));
449 * If the memory allocation failed, return a null pointer.
455 * Copy the length of the string in the placeholder.
460 * Skip the byte count.
465 * Copy the information in the buffer.
466 * Since it is valid to pass a NULL pointer here, we'll initialize the
467 * buffer to nul if it is the case.
470 memcpy(newBuffer
, in
, len
);
473 * Make sure that there is a nul character at the end of the
476 stringBuffer
= (char *)newBuffer
;
477 stringBuffer
[len
] = 0;
478 stringBuffer
[len
+1] = 0;
480 return (LPWSTR
)stringBuffer
;