4 * Copyright 1995 Martin von Loewis
11 #include "wine/obj_base.h"
16 /* This implementation of the BSTR API is 16-bit only. It
17 represents BSTR as a 16:16 far pointer, and the strings
20 /******************************************************************************
21 * BSTR_AllocBytes [Internal]
23 static BSTR16
BSTR_AllocBytes(int n
)
25 void *ptr
= SEGPTR_ALLOC(n
);
26 return (BSTR16
)SEGPTR_GET(ptr
);
29 /******************************************************************************
30 * BSTR_Free [INTERNAL]
32 static void BSTR_Free(BSTR16 in
)
34 SEGPTR_FREE( PTR_SEG_TO_LIN(in
) );
37 /******************************************************************************
38 * BSTR_GetAddr [INTERNAL]
40 static void* BSTR_GetAddr(BSTR16 in
)
42 return in
? PTR_SEG_TO_LIN(in
) : 0;
45 /******************************************************************************
46 * SysAllocString16 [OLE2DISP.2]
48 BSTR16 WINAPI
SysAllocString16(LPCOLESTR16 in
)
50 BSTR16 out
=BSTR_AllocBytes(strlen(in
)+1);
52 strcpy(BSTR_GetAddr(out
),in
);
56 /******************************************************************************
57 * SysAllocString32 [OLEAUT32.2]
59 BSTR WINAPI
SysAllocString(LPCOLESTR in
)
61 /* Delegate this to the SysAllocStringLen32 method. */
62 return SysAllocStringLen(in
, lstrlenW(in
));
65 /******************************************************************************
66 * SysReAllocString16 [OLE2DISP.3]
68 INT16 WINAPI
SysReAllocString16(LPBSTR16 old
,LPCOLESTR16 in
)
70 BSTR16
new=SysAllocString16(in
);
76 /******************************************************************************
77 * SysReAllocString32 [OLEAUT32.3]
79 INT WINAPI
SysReAllocString(LPBSTR old
,LPCOLESTR in
)
88 * Make sure we free the old string.
94 * Allocate the new string
96 *old
= SysAllocString(in
);
101 /******************************************************************************
102 * SysAllocStringLen16 [OLE2DISP.4]
104 BSTR16 WINAPI
SysAllocStringLen16(const char *in
, int len
)
106 BSTR16 out
=BSTR_AllocBytes(len
+1);
108 strcpy(BSTR_GetAddr(out
),in
);
112 /******************************************************************************
113 * SysAllocStringLen32 [OLEAUT32.4]
115 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
116 * section, he describes the DWORD value placed before the BSTR data type.
117 * he describes it as a "DWORD count of characters". By experimenting with
118 * a windows application, this count seems to be a DWORD count of bytes in
119 * the string. Meaning that the count is double the number of wide
120 * characters in the string.
122 BSTR WINAPI
SysAllocStringLen(const OLECHAR
*in
, unsigned int len
)
129 * Find the lenth of the buffer passed-in in bytes.
131 bufferSize
= len
* sizeof (WCHAR
);
134 * Allocate a new buffer to hold the string.
135 * dont't forget to keep an empty spot at the begining of the
136 * buffer for the character count and an extra character at the
139 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
141 bufferSize
+ sizeof(WCHAR
) + sizeof(DWORD
));
144 * If the memory allocation failed, return a null pointer.
150 * Copy the length of the string in the placeholder.
152 *newBuffer
= bufferSize
;
155 * Skip the byte count.
160 * Copy the information in the buffer.
161 * Since it is valid to pass a NULL pointer here, we'll initialize the
162 * buffer to nul if it is the case.
165 memcpy(newBuffer
, in
, bufferSize
);
167 memset(newBuffer
, 0, bufferSize
);
170 * Make sure that there is a nul character at the end of the
173 stringBuffer
= (WCHAR
*)newBuffer
;
174 stringBuffer
[len
] = L
'\0';
176 return (LPWSTR
)stringBuffer
;
179 /******************************************************************************
180 * SysReAllocStringLen16 [OLE2DISP.5]
182 int WINAPI
SysReAllocStringLen16(BSTR16
*old
,const char *in
,int len
)
184 BSTR16
new=SysAllocStringLen16(in
,len
);
191 /******************************************************************************
192 * SysReAllocStringLen32 [OLEAUT32.5]
194 int WINAPI
SysReAllocStringLen(BSTR
* old
, const OLECHAR
* in
, unsigned int len
)
203 * Make sure we free the old string.
209 * Allocate the new string
211 *old
= SysAllocStringLen(in
, len
);
216 /******************************************************************************
217 * SysFreeString16 [OLE2DISP.6]
219 void WINAPI
SysFreeString16(BSTR16 in
)
224 /******************************************************************************
225 * SysFreeString32 [OLEAUT32.6]
227 void WINAPI
SysFreeString(BSTR in
)
229 DWORD
* bufferPointer
;
232 * We have to be careful when we free a BSTR pointer, it points to
233 * the beginning of the string but it skips the byte count contained
236 bufferPointer
= (DWORD
*)in
;
241 * Free the memory from it's "real" origin.
243 HeapFree(GetProcessHeap(), 0, bufferPointer
);
246 /******************************************************************************
247 * SysStringLen16 [OLE2DISP.7]
249 int WINAPI
SysStringLen16(BSTR16 str
)
251 return strlen(BSTR_GetAddr(str
));
254 /******************************************************************************
255 * SysStringLen32 [OLEAUT32.7]
257 * The Windows documentation states that the length returned by this function
258 * is not necessarely the same as the length returned by the _lstrlenW method.
259 * It is the same number that was passed in as the "len" parameter if the
260 * string was allocated with a SysAllocStringLen method call.
262 int WINAPI
SysStringLen(BSTR str
)
264 DWORD
* bufferPointer
;
267 * The length of the string (in bytes) is contained in a DWORD placed
268 * just before the BSTR pointer
270 bufferPointer
= (DWORD
*)str
;
274 return (int)(*bufferPointer
/sizeof(WCHAR
));
277 /******************************************************************************
278 * SysStringByteLen [OLEAUT32.149]
280 * The Windows documentation states that the length returned by this function
281 * is not necessarely the same as the length returned by the _lstrlenW method.
282 * It is the same number that was passed in as the "len" parameter if the
283 * string was allocated with a SysAllocStringLen method call.
285 int WINAPI
SysStringByteLen(BSTR str
)
287 return SysStringLen(str
)*sizeof(WCHAR
);
290 /******************************************************************************
291 * CreateDispTypeInfo [OLE2DISP.31]
293 HRESULT
CreateDispTypeInfo16(
294 INTERFACEDATA
*pidata
,
298 FIXME(ole
,"(%p,%ld,%p),stub\n",pidata
,lcid
,pptinfo
);
302 /******************************************************************************
303 * RegisterActiveObject [OLE2DISP.35]
305 HRESULT
RegisterActiveObject16(
306 IUnknown
*punk
, REFCLSID rclsid
, DWORD dwFlags
, unsigned long *pdwRegister
309 WINE_StringFromCLSID(rclsid
,buf
);
310 FIXME(ole
,"(%p,%s,0x%08lx,%p):stub\n",punk
,buf
,dwFlags
,pdwRegister
);
314 /******************************************************************************
315 * OleTranslateColor [OLEAUT32.421]
317 INT WINAPI
OleTranslateColor(
322 FIXME(ole
,"():stub\n");
329 /******************************************************************************
330 * SysAllocStringByteLen [OLEAUT32.150]
333 BSTR WINAPI
SysAllocStringByteLen(char *in
, int len
)
339 * Allocate a new buffer to hold the string.
340 * dont't forget to keep an empty spot at the begining of the
341 * buffer for the character count and an extra character at the
344 newBuffer
= (DWORD
*)HeapAlloc(GetProcessHeap(),
346 len
+ sizeof(WCHAR
) + sizeof(DWORD
));
349 * If the memory allocation failed, return a null pointer.
355 * Copy the length of the string in the placeholder.
360 * Skip the byte count.
365 * Copy the information in the buffer.
366 * Since it is valid to pass a NULL pointer here, we'll initialize the
367 * buffer to nul if it is the case.
370 memcpy(newBuffer
, in
, len
);
373 * Make sure that there is a nul character at the end of the
376 stringBuffer
= (char *)newBuffer
;
377 stringBuffer
[len
] = 0;
378 stringBuffer
[len
+1] = 0;
380 return (LPWSTR
)stringBuffer
;