source: trunk/src/oleaut32/ole2disp.c@ 6666

Last change on this file since 6666 was 6648, checked in by bird, 24 years ago

Added $Id:$ keyword.

File size: 12.9 KB
Line 
1/* $Id: ole2disp.c,v 1.2 2001-09-05 13:19:00 bird Exp $ */
2/*
3 * OLE2DISP library
4 *
5 * Copyright 1995 Martin von Loewis
6 */
7#ifdef __WIN32OS2__
8#define HAVE_FLOAT_H
9#define WINE_LARGE_INTEGER
10#include "oleaut32.h"
11#endif
12#include <string.h>
13
14#include "windef.h"
15#include "winbase.h"
16#include "wingdi.h"
17#include "winuser.h"
18#include "winerror.h"
19#include "wine/windef16.h"
20#include "ole2.h"
21#include "olectl.h"
22#include "oleauto.h"
23#include "heap.h"
24#include "debugtools.h"
25
26DEFAULT_DEBUG_CHANNEL(ole);
27
28#ifndef __WIN32OS2__
29/* This implementation of the BSTR API is 16-bit only. It
30 represents BSTR as a 16:16 far pointer, and the strings
31 as ISO-8859 */
32
33/******************************************************************************
34 * BSTR_AllocBytes [Internal]
35 */
36static BSTR16 BSTR_AllocBytes(int n)
37{
38 void *ptr = SEGPTR_ALLOC(n);
39 return (BSTR16)SEGPTR_GET(ptr);
40}
41
42/******************************************************************************
43 * BSTR_Free [INTERNAL]
44 */
45static void BSTR_Free(BSTR16 in)
46{
47 SEGPTR_FREE( MapSL((SEGPTR)in) );
48}
49
50/******************************************************************************
51 * BSTR_GetAddr [INTERNAL]
52 */
53static void* BSTR_GetAddr(BSTR16 in)
54{
55 return in ? MapSL((SEGPTR)in) : 0;
56}
57
58/******************************************************************************
59 * SysAllocString16 [OLE2DISP.2]
60 */
61BSTR16 WINAPI SysAllocString16(LPCOLESTR16 in)
62{
63 BSTR16 out;
64
65 if (!in) return 0;
66
67 out = BSTR_AllocBytes(strlen(in)+1);
68 if(!out)return 0;
69 strcpy(BSTR_GetAddr(out),in);
70 return out;
71}
72#endif
73
74/******************************************************************************
75 * SysAllocString [OLEAUT32.2]
76 */
77BSTR WINAPI SysAllocString(LPCOLESTR in)
78{
79 if (!in) return 0;
80
81 /* Delegate this to the SysAllocStringLen32 method. */
82 return SysAllocStringLen(in, lstrlenW(in));
83}
84
85#ifndef __WIN32OS2__
86/******************************************************************************
87 * SysReAllocString16 [OLE2DISP.3]
88 */
89INT16 WINAPI SysReAllocString16(LPBSTR16 old,LPCOLESTR16 in)
90{
91 BSTR16 new=SysAllocString16(in);
92 BSTR_Free(*old);
93 *old=new;
94 return 1;
95}
96#endif
97
98/******************************************************************************
99 * SysReAllocString [OLEAUT32.3]
100 */
101INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR in)
102{
103 /*
104 * Sanity check
105 */
106 if (old==NULL)
107 return 0;
108
109 /*
110 * Make sure we free the old string.
111 */
112 if (*old!=NULL)
113 SysFreeString(*old);
114
115 /*
116 * Allocate the new string
117 */
118 *old = SysAllocString(in);
119
120 return 1;
121}
122
123#ifndef __WIN32OS2__
124/******************************************************************************
125 * SysAllocStringLen16 [OLE2DISP.4]
126 */
127BSTR16 WINAPI SysAllocStringLen16(const char *in, int len)
128{
129 BSTR16 out=BSTR_AllocBytes(len+1);
130
131 if (!out)
132 return 0;
133
134 /*
135 * Copy the information in the buffer.
136 * Since it is valid to pass a NULL pointer here, we'll initialize the
137 * buffer to nul if it is the case.
138 */
139 if (in != 0)
140 strcpy(BSTR_GetAddr(out),in);
141 else
142 memset(BSTR_GetAddr(out), 0, len+1);
143
144 return out;
145}
146#endif
147
148/******************************************************************************
149 * SysAllocStringLen [OLEAUT32.4]
150 *
151 * In "Inside OLE, second edition" by Kraig Brockshmidt. In the Automation
152 * section, he describes the DWORD value placed *before* the BSTR data type.
153 * he describes it as a "DWORD count of characters". By experimenting with
154 * a windows application, this count seems to be a DWORD count of bytes in
155 * the string. Meaning that the count is double the number of wide
156 * characters in the string.
157 */
158BSTR WINAPI SysAllocStringLen(const OLECHAR *in, unsigned int len)
159{
160 DWORD bufferSize;
161 DWORD* newBuffer;
162 WCHAR* stringBuffer;
163
164 /*
165 * Find the length of the buffer passed-in in bytes.
166 */
167 bufferSize = len * sizeof (WCHAR);
168
169 /*
170 * Allocate a new buffer to hold the string.
171 * dont't forget to keep an empty spot at the beginning of the
172 * buffer for the character count and an extra character at the
173 * end for the NULL.
174 */
175 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
176 0,
177 bufferSize + sizeof(WCHAR) + sizeof(DWORD));
178
179 /*
180 * If the memory allocation failed, return a null pointer.
181 */
182 if (newBuffer==0)
183 return 0;
184
185 /*
186 * Copy the length of the string in the placeholder.
187 */
188 *newBuffer = bufferSize;
189
190 /*
191 * Skip the byte count.
192 */
193 newBuffer++;
194
195 /*
196 * Copy the information in the buffer.
197 * Since it is valid to pass a NULL pointer here, we'll initialize the
198 * buffer to nul if it is the case.
199 */
200 if (in != 0)
201 memcpy(newBuffer, in, bufferSize);
202 else
203 memset(newBuffer, 0, bufferSize);
204
205 /*
206 * Make sure that there is a nul character at the end of the
207 * string.
208 */
209 stringBuffer = (WCHAR*)newBuffer;
210 stringBuffer[len] = L'\0';
211
212 return (LPWSTR)stringBuffer;
213}
214
215#ifndef __WIN32OS2__
216/******************************************************************************
217 * SysReAllocStringLen16 [OLE2DISP.5]
218 */
219int WINAPI SysReAllocStringLen16(BSTR16 *old,const char *in,int len)
220{
221 BSTR16 new=SysAllocStringLen16(in,len);
222 BSTR_Free(*old);
223 *old=new;
224 return 1;
225}
226#endif
227
228
229/******************************************************************************
230 * SysReAllocStringLen [OLEAUT32.5]
231 */
232int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* in, unsigned int len)
233{
234 /*
235 * Sanity check
236 */
237 if (old==NULL)
238 return 0;
239
240 /*
241 * Make sure we free the old string.
242 */
243 if (*old!=NULL)
244 SysFreeString(*old);
245
246 /*
247 * Allocate the new string
248 */
249 *old = SysAllocStringLen(in, len);
250
251 return 1;
252}
253
254#ifndef __WIN32OS2__
255/******************************************************************************
256 * SysFreeString16 [OLE2DISP.6]
257 */
258void WINAPI SysFreeString16(BSTR16 in)
259{
260 BSTR_Free(in);
261}
262#endif
263
264/******************************************************************************
265 * SysFreeString [OLEAUT32.6]
266 */
267void WINAPI SysFreeString(BSTR in)
268{
269 DWORD* bufferPointer;
270
271 /* NULL is a valid parameter */
272 if(!in) return;
273
274 /*
275 * We have to be careful when we free a BSTR pointer, it points to
276 * the beginning of the string but it skips the byte count contained
277 * before the string.
278 */
279 bufferPointer = (DWORD*)in;
280
281 bufferPointer--;
282
283 /*
284 * Free the memory from it's "real" origin.
285 */
286 HeapFree(GetProcessHeap(), 0, bufferPointer);
287}
288
289#ifndef __WIN32OS2__
290/******************************************************************************
291 * SysStringLen16 [OLE2DISP.7]
292 */
293int WINAPI SysStringLen16(BSTR16 str)
294{
295 return strlen(BSTR_GetAddr(str));
296}
297#endif
298
299/******************************************************************************
300 * SysStringLen [OLEAUT32.7]
301 *
302 * The Windows documentation states that the length returned by this function
303 * is not necessarely the same as the length returned by the _lstrlenW method.
304 * It is the same number that was passed in as the "len" parameter if the
305 * string was allocated with a SysAllocStringLen method call.
306 */
307int WINAPI SysStringLen(BSTR str)
308{
309 DWORD* bufferPointer;
310
311 if (!str) return 0;
312 /*
313 * The length of the string (in bytes) is contained in a DWORD placed
314 * just before the BSTR pointer
315 */
316 bufferPointer = (DWORD*)str;
317
318 bufferPointer--;
319
320 return (int)(*bufferPointer/sizeof(WCHAR));
321}
322
323/******************************************************************************
324 * SysStringByteLen [OLEAUT32.149]
325 *
326 * The Windows documentation states that the length returned by this function
327 * is not necessarely the same as the length returned by the _lstrlenW method.
328 * It is the same number that was passed in as the "len" parameter if the
329 * string was allocated with a SysAllocStringLen method call.
330 */
331int WINAPI SysStringByteLen(BSTR str)
332{
333 DWORD* bufferPointer;
334
335 if (!str) return 0;
336 /*
337 * The length of the string (in bytes) is contained in a DWORD placed
338 * just before the BSTR pointer
339 */
340 bufferPointer = (DWORD*)str;
341
342 bufferPointer--;
343
344 return (int)(*bufferPointer);
345}
346
347#ifndef __WIN32OS2__
348/******************************************************************************
349 * CreateDispTypeInfo16 [OLE2DISP.31]
350 */
351HRESULT WINAPI CreateDispTypeInfo16(
352 INTERFACEDATA *pidata,
353 LCID lcid,
354 ITypeInfo **pptinfo)
355{
356 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
357 return 0;
358}
359#endif
360
361/******************************************************************************
362 * CreateDispTypeInfo [OLE2DISP.31]
363 */
364HRESULT WINAPI CreateDispTypeInfo(
365 INTERFACEDATA *pidata,
366 LCID lcid,
367 ITypeInfo **pptinfo)
368{
369 FIXME("(%p,%ld,%p),stub\n",pidata,lcid,pptinfo);
370 return 0;
371}
372
373#ifndef __WIN32OS2__
374/******************************************************************************
375 * CreateStdDispatch16 [OLE2DISP.32]
376 */
377HRESULT WINAPI CreateStdDispatch16(
378 IUnknown* punkOuter,
379 void* pvThis,
380 ITypeInfo* ptinfo,
381 IUnknown** ppunkStdDisp)
382{
383 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
384 ppunkStdDisp);
385 return 0;
386}
387#endif
388
389/******************************************************************************
390 * CreateStdDispatch [OLE2DISP.32]
391 */
392HRESULT WINAPI CreateStdDispatch(
393 IUnknown* punkOuter,
394 void* pvThis,
395 ITypeInfo* ptinfo,
396 IUnknown** ppunkStdDisp)
397{
398 FIXME("(%p,%p,%p,%p),stub\n",punkOuter, pvThis, ptinfo,
399 ppunkStdDisp);
400 return 0;
401}
402
403#ifndef __WIN32OS2__
404/******************************************************************************
405 * RegisterActiveObject [OLE2DISP.35]
406 */
407HRESULT WINAPI RegisterActiveObject16(
408 IUnknown *punk, REFCLSID rclsid, DWORD dwFlags, unsigned long *pdwRegister
409) {
410 FIXME("(%p,%s,0x%08lx,%p):stub\n",punk,debugstr_guid(rclsid),dwFlags,pdwRegister);
411 return 0;
412}
413#endif
414
415/******************************************************************************
416 * OleTranslateColor [OLEAUT32.421]
417 *
418 * Converts an OLE_COLOR to a COLORREF.
419 * See the documentation for conversion rules.
420 * pColorRef can be NULL. In that case the user only wants to test the
421 * conversion.
422 */
423HRESULT WINAPI OleTranslateColor(
424 OLE_COLOR clr,
425 HPALETTE hpal,
426 COLORREF* pColorRef)
427{
428 COLORREF colorref;
429 BYTE b = HIBYTE(HIWORD(clr));
430
431 TRACE("(%08lx, %d, %p):stub\n", clr, hpal, pColorRef);
432
433 /*
434 * In case pColorRef is NULL, provide our own to simplify the code.
435 */
436 if (pColorRef == NULL)
437 pColorRef = &colorref;
438
439 switch (b)
440 {
441 case 0x00:
442 {
443 if (hpal != 0)
444 *pColorRef = PALETTERGB(GetRValue(clr),
445 GetGValue(clr),
446 GetBValue(clr));
447 else
448 *pColorRef = clr;
449
450 break;
451 }
452
453 case 0x01:
454 {
455 if (hpal != 0)
456 {
457 PALETTEENTRY pe;
458 /*
459 * Validate the palette index.
460 */
461 if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
462 return E_INVALIDARG;
463 }
464
465 *pColorRef = clr;
466
467 break;
468 }
469
470 case 0x02:
471 *pColorRef = clr;
472 break;
473
474 case 0x80:
475 {
476 int index = LOBYTE(LOWORD(clr));
477
478 /*
479 * Validate GetSysColor index.
480 */
481 if ((index < COLOR_SCROLLBAR) || (index > COLOR_GRADIENTINACTIVECAPTION))
482 return E_INVALIDARG;
483
484 *pColorRef = GetSysColor(index);
485
486 break;
487 }
488
489 default:
490 return E_INVALIDARG;
491 }
492
493 return S_OK;
494}
495
496/******************************************************************************
497 * SysAllocStringByteLen [OLEAUT32.150]
498 *
499 */
500BSTR WINAPI SysAllocStringByteLen(LPCSTR in, UINT len)
501{
502 DWORD* newBuffer;
503 char* stringBuffer;
504
505 /*
506 * Allocate a new buffer to hold the string.
507 * dont't forget to keep an empty spot at the begining of the
508 * buffer for the character count and an extra character at the
509 * end for the NULL.
510 */
511 newBuffer = (DWORD*)HeapAlloc(GetProcessHeap(),
512 0,
513 len + sizeof(WCHAR) + sizeof(DWORD));
514
515 /*
516 * If the memory allocation failed, return a null pointer.
517 */
518 if (newBuffer==0)
519 return 0;
520
521 /*
522 * Copy the length of the string in the placeholder.
523 */
524 *newBuffer = len;
525
526 /*
527 * Skip the byte count.
528 */
529 newBuffer++;
530
531 /*
532 * Copy the information in the buffer.
533 * Since it is valid to pass a NULL pointer here, we'll initialize the
534 * buffer to nul if it is the case.
535 */
536 if (in != 0)
537 memcpy(newBuffer, in, len);
538
539 /*
540 * Make sure that there is a nul character at the end of the
541 * string.
542 */
543 stringBuffer = (char *)newBuffer;
544 stringBuffer[len] = 0;
545 stringBuffer[len+1] = 0;
546
547 return (LPWSTR)stringBuffer;
548}
549
550
Note: See TracBrowser for help on using the repository browser.