4 * Copyright 1995 Martin von Loewis
11 #include "wine/obj_base.h"
16 DEFAULT_DEBUG_CHANNEL(ole
)
18 /* This implementation of the BSTR API is 16-bit only. It
19 represents BSTR as a 16:16 far pointer, and the strings
22 /******************************************************************************
23 * BSTR_AllocBytes [Internal]
25 static BSTR16
BSTR_AllocBytes(int n
)
27 void *ptr
= SEGPTR_ALLOC(n
);
28 return (BSTR16
)SEGPTR_GET(ptr
);
31 /******************************************************************************
32 * BSTR_Free [INTERNAL]
34 static void BSTR_Free(BSTR16 in
)
36 SEGPTR_FREE( PTR_SEG_TO_LIN(in
) );
39 /******************************************************************************
40 * BSTR_GetAddr [INTERNAL]
42 static void* BSTR_GetAddr(BSTR16 in
)
44 return in
? PTR_SEG_TO_LIN(in
) : 0;
47 /******************************************************************************
48 * SysAllocString16 [OLE2DISP.2]
50 BSTR16 WINAPI
SysAllocString16(LPCOLESTR16 in
)
52 BSTR16 out
=BSTR_AllocBytes(strlen(in
)+1);
54 strcpy(BSTR_GetAddr(out
),in
);
58 /******************************************************************************
59 * SysAllocString32 [OLEAUT32.2]
61 BSTR WINAPI
SysAllocString(LPCOLESTR in
)
63 /* Delegate this to the SysAllocStringLen32 method. */
64 return SysAllocStringLen(in
, lstrlenW(in
));
67 /******************************************************************************
68 * SysReAllocString16 [OLE2DISP.3]
70 INT16 WINAPI
SysReAllocString16(LPBSTR16 old
,LPCOLESTR16 in
)
72 BSTR16
new=SysAllocString16(in
);
78 /******************************************************************************
79 * SysReAllocString32 [OLEAUT32.3]
81 INT WINAPI
SysReAllocString(LPBSTR old
,LPCOLESTR in
)
90 * Make sure we free the old string.
96 * Allocate the new string
98 *old
= SysAllocString(in
);
103 /******************************************************************************
104 * SysAllocStringLen16 [OLE2DISP.4]
106 BSTR16 WINAPI
SysAllocStringLen16(const char *in
, int len
)
108 BSTR16 out
=BSTR_AllocBytes(len
+1);
110 strcpy(BSTR_GetAddr(out
),in
);
114 /******************************************************************************
115 * SysAllocStringLen32 [OLEAUT32.4]
117 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
118 * section, he describes the DWORD value placed before the BSTR data type.
119 * he describes it as a "DWORD count of characters". By experimenting with
120 * a windows application, this count seems to be a DWORD count of bytes in
121 * the string. Meaning that the count is double the number of wide
122 * characters in the string.
124 BSTR WINAPI
SysAllocStringLen(const OLECHAR
*in
, unsigned int len
)
131 * Find the lenth of the buffer passed-in in bytes.
133 bufferSize
= len
* sizeof (WCHAR
);
136 * Allocate a new buffer to hold the string.
137 * dont't forget to keep an empty spot at the begining of the
138 * buffer for the character count and an extra character at the
141 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
143 bufferSize
+ sizeof(WCHAR
) + sizeof(DWORD
));
146 * If the memory allocation failed, return a null pointer.
152 * Copy the length of the string in the placeholder.
154 *newBuffer
= bufferSize
;
157 * Skip the byte count.
162 * Copy the information in the buffer.
163 * Since it is valid to pass a NULL pointer here, we'll initialize the
164 * buffer to nul if it is the case.
167 memcpy(newBuffer
, in
, bufferSize
);
169 memset(newBuffer
, 0, bufferSize
);
172 * Make sure that there is a nul character at the end of the
175 stringBuffer
= (WCHAR
*)newBuffer
;
176 stringBuffer
[len
] = L
'\0';
178 return (LPWSTR
)stringBuffer
;
181 /******************************************************************************
182 * SysReAllocStringLen16 [OLE2DISP.5]
184 int WINAPI
SysReAllocStringLen16(BSTR16
*old
,const char *in
,int len
)
186 BSTR16
new=SysAllocStringLen16(in
,len
);
193 /******************************************************************************
194 * SysReAllocStringLen32 [OLEAUT32.5]
196 int WINAPI
SysReAllocStringLen(BSTR
* old
, const OLECHAR
* in
, unsigned int len
)
205 * Make sure we free the old string.
211 * Allocate the new string
213 *old
= SysAllocStringLen(in
, len
);
218 /******************************************************************************
219 * SysFreeString16 [OLE2DISP.6]
221 void WINAPI
SysFreeString16(BSTR16 in
)
226 /******************************************************************************
227 * SysFreeString32 [OLEAUT32.6]
229 void WINAPI
SysFreeString(BSTR in
)
231 DWORD
* bufferPointer
;
233 /* NULL is a valid parameter */
237 * We have to be careful when we free a BSTR pointer, it points to
238 * the beginning of the string but it skips the byte count contained
241 bufferPointer
= (DWORD
*)in
;
246 * Free the memory from it's "real" origin.
248 HeapFree(GetProcessHeap(), 0, bufferPointer
);
251 /******************************************************************************
252 * SysStringLen16 [OLE2DISP.7]
254 int WINAPI
SysStringLen16(BSTR16 str
)
256 return strlen(BSTR_GetAddr(str
));
259 /******************************************************************************
260 * SysStringLen32 [OLEAUT32.7]
262 * The Windows documentation states that the length returned by this function
263 * is not necessarely the same as the length returned by the _lstrlenW method.
264 * It is the same number that was passed in as the "len" parameter if the
265 * string was allocated with a SysAllocStringLen method call.
267 int WINAPI
SysStringLen(BSTR str
)
269 DWORD
* bufferPointer
;
272 * The length of the string (in bytes) is contained in a DWORD placed
273 * just before the BSTR pointer
275 bufferPointer
= (DWORD
*)str
;
279 return (int)(*bufferPointer
/sizeof(WCHAR
));
282 /******************************************************************************
283 * SysStringByteLen [OLEAUT32.149]
285 * The Windows documentation states that the length returned by this function
286 * is not necessarely the same as the length returned by the _lstrlenW method.
287 * It is the same number that was passed in as the "len" parameter if the
288 * string was allocated with a SysAllocStringLen method call.
290 int WINAPI
SysStringByteLen(BSTR str
)
292 return SysStringLen(str
)*sizeof(WCHAR
);
295 /******************************************************************************
296 * CreateDispTypeInfo [OLE2DISP.31]
298 HRESULT WINAPI
CreateDispTypeInfo16(
299 INTERFACEDATA
*pidata
,
303 FIXME(ole
,"(%p,%ld,%p),stub\n",pidata
,lcid
,pptinfo
);
307 /******************************************************************************
308 * RegisterActiveObject [OLE2DISP.35]
310 HRESULT WINAPI
RegisterActiveObject16(
311 IUnknown
*punk
, REFCLSID rclsid
, DWORD dwFlags
, unsigned long *pdwRegister
314 WINE_StringFromCLSID(rclsid
,buf
);
315 FIXME(ole
,"(%p,%s,0x%08lx,%p):stub\n",punk
,buf
,dwFlags
,pdwRegister
);
319 /******************************************************************************
320 * OleTranslateColor [OLEAUT32.421]
322 * Converts an OLE_COLOR to a COLORREF.
323 * See the documentation for conversion rules.
324 * pColorRef can be NULL. In that case the user only wants to test the
327 INT WINAPI
OleTranslateColor(
333 BYTE b
= HIBYTE(HIWORD(clr
));
335 TRACE(ole
,"(%08lx, %d, %p):stub\n", clr
, hpal
, pColorRef
);
338 * In case pColorRef is NULL, provide our own to simplify the code.
340 if (pColorRef
== NULL
)
341 pColorRef
= &colorref
;
348 *pColorRef
= PALETTERGB(GetRValue(clr
),
363 * Validate the palette index.
365 if (GetPaletteEntries(hpal
, LOWORD(clr
), 1, &pe
) == 0)
380 int index
= LOBYTE(LOWORD(clr
));
383 * Validate GetSysColor index.
385 if ((index
< COLOR_SCROLLBAR
) || (index
> COLOR_GRADIENTINACTIVECAPTION
))
388 *pColorRef
= GetSysColor(index
);
400 /******************************************************************************
401 * SysAllocStringByteLen [OLEAUT32.150]
404 BSTR WINAPI
SysAllocStringByteLen(char *in
, int len
)
410 * Allocate a new buffer to hold the string.
411 * dont't forget to keep an empty spot at the begining of the
412 * buffer for the character count and an extra character at the
415 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
417 len
+ sizeof(WCHAR
) + sizeof(DWORD
));
420 * If the memory allocation failed, return a null pointer.
426 * Copy the length of the string in the placeholder.
431 * Skip the byte count.
436 * Copy the information in the buffer.
437 * Since it is valid to pass a NULL pointer here, we'll initialize the
438 * buffer to nul if it is the case.
441 memcpy(newBuffer
, in
, len
);
444 * Make sure that there is a nul character at the end of the
447 stringBuffer
= (char *)newBuffer
;
448 stringBuffer
[len
] = 0;
449 stringBuffer
[len
+1] = 0;
451 return (LPWSTR
)stringBuffer
;