source: trunk/src/oleaut32/ole2disp.cpp@ 631

Last change on this file since 631 was 631, checked in by sandervl, 26 years ago

Created (WINE Port of OLEAUT32)

File size: 8.2 KB
Line 
1/*
2 * OLE2DISP library
3 *
4 *
5 * Copyright 1995 Martin von Loewis
6 * Copyright 1999 Sander van Leeuwen (WINE OS/2 Port 990815)
7 *
8 * Project Odin Software License can be found in LICENSE.TXT
9 *
10 */
11
12#include "oleaut32.h"
13#include <string.h>
14#include "winerror.h"
15#include "ole2.h"
16#include "oleauto.h"
17#include "wine/obj_base.h"
18#include "heap.h"
19#include "ldt.h"
20#include "debugtools.h"
21
22DEFAULT_DEBUG_CHANNEL(ole)
23
24/******************************************************************************
25 * SysAllocString32 [OLEAUT32.2]
26 */
27BSTR WINAPI SysAllocString(LPCOLESTR in)
28{
29 /* Delegate this to the SysAllocStringLen32 method. */
30 return SysAllocStringLen(in, lstrlenW(in));
31}
32
33/******************************************************************************
34 * SysReAllocString32 [OLEAUT32.3]
35 */
36INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
37{
38 /*
39 * Sanity check
40 */
41 if (old==NULL)
42 return 0;
43
44 /*
45 * Make sure we free the old string.
46 */
47 if (*old!=NULL)
48 SysFreeString(*old);
49
50 /*
51 * Allocate the new string
52 */
53 *old = SysAllocString(in);
54
55 return 1;
56}
57
58
59/******************************************************************************
60 * SysAllocStringLen32 [OLEAUT32.4]
61 *
62 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
63 * section, he describes the DWORD value placed before the BSTR data type.
64 * he describes it as a "DWORD count of characters". By experimenting with
65 * a windows application, this count seems to be a DWORD count of bytes in
66 * the string. Meaning that the count is double the number of wide
67 * characters in the string.
68 */
69BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
70{
71 DWORD bufferSize;
72 DWORD* newBuffer;
73 WCHAR* stringBuffer;
74
75 /*
76 * Find the lenth of the buffer passed-in in bytes.
77 */
78 bufferSize = len * sizeof (WCHAR);
79
80 /*
81 * Allocate a new buffer to hold the string.
82 * dont't forget to keep an empty spot at the begining of the
83 * buffer for the character count and an extra character at the
84 * end for the NULL.
85 */
86 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
87 0,
88 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
89
90 /*
91 * If the memory allocation failed, return a null pointer.
92 */
93 if (newBuffer==0)
94 return 0;
95
96 /*
97 * Copy the length of the string in the placeholder.
98 */
99 *newBuffer = bufferSize;
100
101 /*
102 * Skip the byte count.
103 */
104 newBuffer++;
105
106 /*
107 * Copy the information in the buffer.
108 * Since it is valid to pass a NULL pointer here, we'll initialize the
109 * buffer to nul if it is the case.
110 */
111 if (in != 0)
112 memcpy(newBuffer, in, bufferSize);
113 else
114 memset(newBuffer, 0, bufferSize);
115
116 /*
117 * Make sure that there is a nul character at the end of the
118 * string.
119 */
120 stringBuffer = (WCHAR*)newBuffer;
121 stringBuffer[len] = L'\0';
122
123 return (LPWSTR)stringBuffer;
124}
125
126
127/******************************************************************************
128 * SysReAllocStringLen32 [OLEAUT32.5]
129 */
130int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
131{
132 /*
133 * Sanity check
134 */
135 if (old==NULL)
136 return 0;
137
138 /*
139 * Make sure we free the old string.
140 */
141 if (*old!=NULL)
142 SysFreeString(*old);
143
144 /*
145 * Allocate the new string
146 */
147 *old = SysAllocStringLen(in, len);
148
149 return 1;
150}
151
152/******************************************************************************
153 * SysFreeString32 [OLEAUT32.6]
154 */
155void WINAPI SysFreeString(BSTR in)
156{
157 DWORD* bufferPointer;
158
159 /* NULL is a valid parameter */
160 if(!in) return;
161
162 /*
163 * We have to be careful when we free a BSTR pointer, it points to
164 * the beginning of the string but it skips the byte count contained
165 * before the string.
166 */
167 bufferPointer = (DWORD*)in;
168
169 bufferPointer--;
170
171 /*
172 * Free the memory from it's "real" origin.
173 */
174 HeapFree(GetProcessHeap(), 0, bufferPointer);
175}
176
177/******************************************************************************
178 * SysStringLen32 [OLEAUT32.7]
179 *
180 * The Windows documentation states that the length returned by this function
181 * is not necessarely the same as the length returned by the _lstrlenW method.
182 * It is the same number that was passed in as the "len" parameter if the
183 * string was allocated with a SysAllocStringLen method call.
184 */
185int WINAPI SysStringLen(BSTR str)
186{
187 DWORD* bufferPointer;
188
189 /*
190 * The length of the string (in bytes) is contained in a DWORD placed
191 * just before the BSTR pointer
192 */
193 bufferPointer = (DWORD*)str;
194
195 bufferPointer--;
196
197 return (int)(*bufferPointer/sizeof(WCHAR));
198}
199
200/******************************************************************************
201 * SysStringByteLen [OLEAUT32.149]
202 *
203 * The Windows documentation states that the length returned by this function
204 * is not necessarely the same as the length returned by the _lstrlenW method.
205 * It is the same number that was passed in as the "len" parameter if the
206 * string was allocated with a SysAllocStringLen method call.
207 */
208int WINAPI SysStringByteLen(BSTR str)
209{
210 return SysStringLen(str)*sizeof(WCHAR);
211}
212
213/******************************************************************************
214 * OleTranslateColor [OLEAUT32.421]
215 *
216 * Converts an OLE_COLOR to a COLORREF.
217 * See the documentation for conversion rules.
218 * pColorRef can be NULL. In that case the user only wants to test the
219 * conversion.
220 */
221INT WINAPI OleTranslateColor(
222 LONG clr,
223 HPALETTE hpal,
224 COLORREF* pColorRef)
225{
226 COLORREF colorref;
227 BYTE b = HIBYTE(HIWORD(clr));
228
229 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
230
231 /*
232 * In case pColorRef is NULL, provide our own to simplify the code.
233 */
234 if (pColorRef == NULL)
235 pColorRef = &colorref;
236
237 switch (b)
238 {
239 case 0x00:
240 {
241 if (hpal != 0)
242 *pColorRef = PALETTERGB(GetRValue(clr),
243 GetGValue(clr),
244 GetBValue(clr));
245 else
246 *pColorRef = clr;
247
248 break;
249 }
250
251 case 0x01:
252 {
253 if (hpal != 0)
254 {
255 PALETTEENTRY pe;
256 /*
257 * Validate the palette index.
258 */
259 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
260 return E_INVALIDARG;
261 }
262
263 *pColorRef = clr;
264
265 break;
266 }
267
268 case 0x02:
269 *pColorRef = clr;
270 break;
271
272 case 0x80:
273 {
274 int index = LOBYTE(LOWORD(clr));
275
276 /*
277 * Validate GetSysColor index.
278 */
279 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
280 return E_INVALIDARG;
281
282 *pColorRef = GetSysColor(index);
283
284 break;
285 }
286
287 default:
288 return E_INVALIDARG;
289 }
290
291 return S_OK;
292}
293
294/******************************************************************************
295 * SysAllocStringByteLen [OLEAUT32.150]
296 *
297 */
298BSTR WINAPI SysAllocStringByteLen(char *in, int len)
299{
300 DWORD* newBuffer;
301 char* stringBuffer;
302
303 /*
304 * Allocate a new buffer to hold the string.
305 * dont't forget to keep an empty spot at the begining of the
306 * buffer for the character count and an extra character at the
307 * end for the NULL.
308 */
309 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
310 0,
311 len + sizeof(WCHAR) + sizeof(DWORD));
312
313 /*
314 * If the memory allocation failed, return a null pointer.
315 */
316 if (newBuffer==0)
317 return 0;
318
319 /*
320 * Copy the length of the string in the placeholder.
321 */
322 *newBuffer = len;
323
324 /*
325 * Skip the byte count.
326 */
327 newBuffer++;
328
329 /*
330 * Copy the information in the buffer.
331 * Since it is valid to pass a NULL pointer here, we'll initialize the
332 * buffer to nul if it is the case.
333 */
334 if (in != 0)
335 memcpy(newBuffer, in, len);
336
337 /*
338 * Make sure that there is a nul character at the end of the
339 * string.
340 */
341 stringBuffer = (char *)newBuffer;
342 stringBuffer[len] = 0;
343 stringBuffer[len+1] = 0;
344
345 return (LPWSTR)stringBuffer;
346}
347
348
Note: See TracBrowser for help on using the repository browser.