oleaut.c 29.9 KB
Newer Older
1 2 3
/*
 *	OLEAUT32
 *
4 5 6 7 8 9 10 11 12 13 14 15 16 17
 * Copyright 1999, 2000 Marcus Meissner
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19
 */
20

21 22
#include "config.h"

23
#include <stdarg.h>
24
#include <string.h>
25
#include <limits.h>
26

27 28
#define COBJMACROS

29
#include "windef.h"
30
#include "winbase.h"
31
#include "wingdi.h"
32 33
#include "winuser.h"
#include "winerror.h"
34

35
#include "ole2.h"
36
#include "olectl.h"
37
#include "oleauto.h"
38
#include "initguid.h"
39
#include "typelib.h"
40
#include "oleaut32_oaidl.h"
41

42
#include "wine/debug.h"
43
#include "wine/unicode.h"
44

45
WINE_DEFAULT_DEBUG_CHANNEL(ole);
46
WINE_DECLARE_DEBUG_CHANNEL(heap);
47

48 49 50 51 52 53 54 55
/******************************************************************************
 * BSTR  {OLEAUT32}
 *
 * NOTES
 *  BSTR is a simple typedef for a wide-character string used as the principle
 *  string type in ole automation. When encapsulated in a Variant type they are
 *  automatically copied and destroyed as the variant is processed.
 *
56 57
 *  The low level BSTR API allows manipulation of these strings and is used by
 *  higher level API calls to manage the strings transparently to the caller.
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
 *
 *  Internally the BSTR type is allocated with space for a DWORD byte count before
 *  the string data begins. This is undocumented and non-system code should not
 *  access the count directly. Use SysStringLen() or SysStringByteLen()
 *  instead. Note that the byte count does not include the terminating NUL.
 *
 *  To create a new BSTR, use SysAllocString(), SysAllocStringLen() or
 *  SysAllocStringByteLen(). To change the size of an existing BSTR, use SysReAllocString()
 *  or SysReAllocStringLen(). Finally to destroy a string use SysFreeString().
 *
 *  BSTR's are cached by Ole Automation by default. To override this behaviour
 *  either set the environment variable 'OANOCACHE', or call SetOaNoCache().
 *
 * SEE ALSO
 *  'Inside OLE, second edition' by Kraig Brockshmidt.
 */

75 76 77 78 79 80 81 82 83 84 85
static BOOL bstr_cache_enabled;

static CRITICAL_SECTION cs_bstr_cache;
static CRITICAL_SECTION_DEBUG cs_bstr_cache_dbg =
{
    0, 0, &cs_bstr_cache,
    { &cs_bstr_cache_dbg.ProcessLocksList, &cs_bstr_cache_dbg.ProcessLocksList },
      0, 0, { (DWORD_PTR)(__FILE__ ": bstr_cache") }
};
static CRITICAL_SECTION cs_bstr_cache = { &cs_bstr_cache_dbg, -1, 0, 0, 0, 0 };

86
typedef struct {
87 88 89
#ifdef _WIN64
    DWORD pad;
#endif
90 91 92 93 94 95 96 97
    DWORD size;
    union {
        char ptr[1];
        WCHAR str[1];
        DWORD dwptr[1];
    } u;
} bstr_t;

98 99 100
#define BUCKET_SIZE 16
#define BUCKET_BUFFER_SIZE 6

101 102 103
typedef struct {
    unsigned short head;
    unsigned short cnt;
104
    bstr_t *buf[BUCKET_BUFFER_SIZE];
105 106
} bstr_cache_entry_t;

107 108 109 110
#define ARENA_INUSE_FILLER     0x55
#define ARENA_TAIL_FILLER      0xab
#define ARENA_FREE_FILLER      0xfeeefeee

111 112 113 114
static bstr_cache_entry_t bstr_cache[0x10000/BUCKET_SIZE];

static inline size_t bstr_alloc_size(size_t size)
{
115 116 117 118 119 120
    return (FIELD_OFFSET(bstr_t, u.ptr[size]) + sizeof(WCHAR) + BUCKET_SIZE-1) & ~(BUCKET_SIZE-1);
}

static inline bstr_t *bstr_from_str(BSTR str)
{
    return CONTAINING_RECORD(str, bstr_t, u.str);
121 122
}

123
static inline bstr_cache_entry_t *get_cache_entry_from_idx(unsigned cache_idx)
124 125 126 127 128 129
{
    return bstr_cache_enabled && cache_idx < sizeof(bstr_cache)/sizeof(*bstr_cache)
        ? bstr_cache + cache_idx
        : NULL;
}

130 131 132 133 134 135 136 137 138 139 140 141 142 143
static inline bstr_cache_entry_t *get_cache_entry(size_t size)
{
    unsigned cache_idx = FIELD_OFFSET(bstr_t, u.ptr[size+sizeof(WCHAR)-1])/BUCKET_SIZE;
    return get_cache_entry_from_idx(cache_idx);
}

static inline bstr_cache_entry_t *get_cache_entry_from_alloc_size(SIZE_T alloc_size)
{
    unsigned cache_idx;
    if (alloc_size < BUCKET_SIZE) return NULL;
    cache_idx = (alloc_size - BUCKET_SIZE) / BUCKET_SIZE;
    return get_cache_entry_from_idx(cache_idx);
}

144
static bstr_t *alloc_bstr(size_t size)
145
{
146
    bstr_cache_entry_t *cache_entry = get_cache_entry(size);
147
    bstr_t *ret;
148 149 150 151 152

    if(cache_entry) {
        EnterCriticalSection(&cs_bstr_cache);

        if(!cache_entry->cnt) {
153
            cache_entry = get_cache_entry(size+BUCKET_SIZE);
154 155 156 157 158 159
            if(cache_entry && !cache_entry->cnt)
                cache_entry = NULL;
        }

        if(cache_entry) {
            ret = cache_entry->buf[cache_entry->head++];
160
            cache_entry->head %= BUCKET_BUFFER_SIZE;
161 162 163 164 165
            cache_entry->cnt--;
        }

        LeaveCriticalSection(&cs_bstr_cache);

166
        if(cache_entry) {
167
            if(WARN_ON(heap)) {
168 169 170
                size_t fill_size = (FIELD_OFFSET(bstr_t, u.ptr[size])+2*sizeof(WCHAR)-1) & ~(sizeof(WCHAR)-1);
                memset(ret, ARENA_INUSE_FILLER, fill_size);
                memset((char *)ret+fill_size, ARENA_TAIL_FILLER, bstr_alloc_size(size)-fill_size);
171
            }
172
            ret->size = size;
173
            return ret;
174
        }
175 176
    }

177
    ret = CoTaskMemAlloc(bstr_alloc_size(size));
178 179 180
    if(ret)
        ret->size = size;
    return ret;
181 182
}

183 184 185
/******************************************************************************
 *             SysStringLen  [OLEAUT32.7]
 *
186 187 188 189 190 191 192 193 194 195 196 197 198
 * Get the allocated length of a BSTR in wide characters.
 *
 * PARAMS
 *  str [I] BSTR to find the length of
 *
 * RETURNS
 *  The allocated length of str, or 0 if str is NULL.
 *
 * NOTES
 *  See BSTR.
 *  The returned length may be different from the length of the string as
 *  calculated by lstrlenW(), since it returns the length that was used to
 *  allocate the string by SysAllocStringLen().
199
 */
200
UINT WINAPI SysStringLen(BSTR str)
201
{
202
    return str ? bstr_from_str(str)->size/sizeof(WCHAR) : 0;
203 204 205 206 207
}

/******************************************************************************
 *             SysStringByteLen  [OLEAUT32.149]
 *
208 209 210 211 212 213 214 215 216 217
 * Get the allocated length of a BSTR in bytes.
 *
 * PARAMS
 *  str [I] BSTR to find the length of
 *
 * RETURNS
 *  The allocated length of str, or 0 if str is NULL.
 *
 * NOTES
 *  See SysStringLen(), BSTR().
218
 */
219
UINT WINAPI SysStringByteLen(BSTR str)
220
{
221
    return str ? bstr_from_str(str)->size : 0;
222 223 224 225 226
}

/******************************************************************************
 *		SysAllocString	[OLEAUT32.2]
 *
227 228 229 230 231 232 233 234 235 236 237 238 239 240
 * Create a BSTR from an OLESTR.
 *
 * PARAMS
 *  str [I] Source to create BSTR from
 *
 * RETURNS
 *  Success: A BSTR allocated with SysAllocStringLen().
 *  Failure: NULL, if oleStr is NULL.
 *
 * NOTES
 *  See BSTR.
 *  MSDN (October 2001) incorrectly states that NULL is returned if oleStr has
 *  a length of 0. Native Win32 and this implementation both return a valid
 *  empty BSTR in this case.
241
 */
242
BSTR WINAPI SysAllocString(LPCOLESTR str)
243
{
244
    if (!str) return 0;
245 246

    /* Delegate this to the SysAllocStringLen32 method. */
247
    return SysAllocStringLen(str, lstrlenW(str));
248 249
}

250 251 252 253 254 255 256 257 258 259
static inline IMalloc *get_malloc(void)
{
    static IMalloc *malloc;

    if (!malloc)
        CoGetMalloc(1, &malloc);

    return malloc;
}

260 261
/******************************************************************************
 *		SysFreeString	[OLEAUT32.6]
262 263 264 265 266 267 268 269 270 271 272 273
 *
 * Free a BSTR.
 *
 * PARAMS
 *  str [I] BSTR to free.
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
 *  See BSTR.
 *  str may be NULL, in which case this function does nothing.
274
 */
275
void WINAPI SysFreeString(BSTR str)
276
{
277
    bstr_cache_entry_t *cache_entry;
278
    bstr_t *bstr;
279 280
    IMalloc *malloc = get_malloc();
    SIZE_T alloc_size;
281

282 283
    if(!str)
        return;
284

285
    bstr = bstr_from_str(str);
286 287 288 289 290 291

    alloc_size = IMalloc_GetSize(malloc, bstr);
    if (alloc_size == ~0UL)
        return;

    cache_entry = get_cache_entry_from_alloc_size(alloc_size);
292
    if(cache_entry) {
293 294
        unsigned i;

295
        EnterCriticalSection(&cs_bstr_cache);
296

297 298 299
        /* According to tests, freeing a string that's already in cache doesn't corrupt anything.
         * For that to work we need to search the cache. */
        for(i=0; i < cache_entry->cnt; i++) {
300
            if(cache_entry->buf[(cache_entry->head+i) % BUCKET_BUFFER_SIZE] == bstr) {
301 302 303 304 305 306
                WARN_(heap)("String already is in cache!\n");
                LeaveCriticalSection(&cs_bstr_cache);
                return;
            }
        }

307
        if(cache_entry->cnt < sizeof(cache_entry->buf)/sizeof(*cache_entry->buf)) {
308
            cache_entry->buf[(cache_entry->head+cache_entry->cnt) % BUCKET_BUFFER_SIZE] = bstr;
309
            cache_entry->cnt++;
310

311
            if(WARN_ON(heap)) {
312
                unsigned n = (alloc_size-FIELD_OFFSET(bstr_t, u.ptr))/sizeof(DWORD);
313 314 315 316
                for(i=0; i<n; i++)
                    bstr->u.dwptr[i] = ARENA_FREE_FILLER;
            }

317 318 319
            LeaveCriticalSection(&cs_bstr_cache);
            return;
        }
320

321 322 323
        LeaveCriticalSection(&cs_bstr_cache);
    }

324
    CoTaskMemFree(bstr);
325 326 327 328 329
}

/******************************************************************************
 *             SysAllocStringLen     [OLEAUT32.4]
 *
330 331 332 333 334 335 336 337 338 339 340 341
 * Create a BSTR from an OLESTR of a given wide character length.
 *
 * PARAMS
 *  str [I] Source to create BSTR from
 *  len [I] Length of oleStr in wide characters
 *
 * RETURNS
 *  Success: A newly allocated BSTR from SysAllocStringByteLen()
 *  Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
 *
 * NOTES
 *  See BSTR(), SysAllocStringByteLen().
342
 */
343
BSTR WINAPI SysAllocStringLen(const OLECHAR *str, unsigned int len)
344
{
345 346
    bstr_t *bstr;
    DWORD size;
347

348 349 350
    /* Detect integer overflow. */
    if (len >= ((UINT_MAX-sizeof(WCHAR)-sizeof(DWORD))/sizeof(WCHAR)))
	return NULL;
351

352 353
    TRACE("%s\n", debugstr_wn(str, len));

354
    size = len*sizeof(WCHAR);
355 356
    bstr = alloc_bstr(size);
    if(!bstr)
357 358 359
        return NULL;

    if(str) {
360 361
        memcpy(bstr->u.str, str, size);
        bstr->u.str[len] = 0;
362
    }else {
363
        memset(bstr->u.str, 0, size+sizeof(WCHAR));
364
    }
365

366
    return bstr->u.str;
367 368 369 370
}

/******************************************************************************
 *             SysReAllocStringLen   [OLEAUT32.5]
371 372 373 374 375 376 377 378 379 380 381 382 383 384
 *
 * Change the length of a previously created BSTR.
 *
 * PARAMS
 *  old [O] BSTR to change the length of
 *  str [I] New source for pbstr
 *  len [I] Length of oleStr in wide characters
 *
 * RETURNS
 *  Success: 1. The size of pbstr is updated.
 *  Failure: 0, if len >= 0x80000000 or memory allocation fails.
 *
 * NOTES
 *  See BSTR(), SysAllocStringByteLen().
385
 *  *old may be changed by this function.
386
 */
387
int WINAPI SysReAllocStringLen(BSTR* old, const OLECHAR* str, unsigned int len)
388
{
389 390
    /* Detect integer overflow. */
    if (len >= ((UINT_MAX-sizeof(WCHAR)-sizeof(DWORD))/sizeof(WCHAR)))
391
	return FALSE;
392

393 394
    if (*old!=NULL) {
      DWORD newbytelen = len*sizeof(WCHAR);
395
      bstr_t *old_bstr = bstr_from_str(*old);
396 397 398 399
      bstr_t *bstr = CoTaskMemRealloc(old_bstr, bstr_alloc_size(newbytelen));

      if (!bstr) return FALSE;

400 401
      *old = bstr->u.str;
      bstr->size = newbytelen;
402 403 404
      /* The old string data is still there when str is NULL */
      if (str && old_bstr->u.str != str) memmove(bstr->u.str, str, newbytelen);
      bstr->u.str[len] = 0;
405
    } else {
406
      *old = SysAllocStringLen(str, len);
407
    }
408

409
    return TRUE;
410 411 412 413 414
}

/******************************************************************************
 *             SysAllocStringByteLen     [OLEAUT32.150]
 *
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
 * Create a BSTR from an OLESTR of a given byte length.
 *
 * PARAMS
 *  str [I] Source to create BSTR from
 *  len [I] Length of oleStr in bytes
 *
 * RETURNS
 *  Success: A newly allocated BSTR
 *  Failure: NULL, if len is >= 0x80000000, or memory allocation fails.
 *
 * NOTES
 *  -If len is 0 or oleStr is NULL the resulting string is empty ("").
 *  -This function always NUL terminates the resulting BSTR.
 *  -oleStr may be either an LPCSTR or LPCOLESTR, since it is copied
 *  without checking for a terminating NUL.
 *  See BSTR.
431
 */
432
BSTR WINAPI SysAllocStringByteLen(LPCSTR str, UINT len)
433
{
434
    bstr_t *bstr;
435

436 437 438 439
    /* Detect integer overflow. */
    if (len >= (UINT_MAX-sizeof(WCHAR)-sizeof(DWORD)))
	return NULL;

440 441
    bstr = alloc_bstr(len);
    if(!bstr)
442 443 444
        return NULL;

    if(str) {
445
        memcpy(bstr->u.ptr, str, len);
446
        bstr->u.ptr[len] = 0;
447
    }else {
448
        memset(bstr->u.ptr, 0, len+1);
449
    }
450
    bstr->u.str[(len+sizeof(WCHAR)-1)/sizeof(WCHAR)] = 0;
451

452
    return bstr->u.str;
453 454 455 456
}

/******************************************************************************
 *		SysReAllocString	[OLEAUT32.3]
457 458 459 460 461 462 463 464 465 466 467 468 469
 *
 * Change the length of a previously created BSTR.
 *
 * PARAMS
 *  old [I/O] BSTR to change the length of
 *  str [I]   New source for pbstr
 *
 * RETURNS
 *  Success: 1
 *  Failure: 0.
 *
 * NOTES
 *  See BSTR(), SysAllocStringStringLen().
470
 */
471
INT WINAPI SysReAllocString(LPBSTR old,LPCOLESTR str)
472 473 474 475 476 477 478 479 480 481
{
    /*
     * Sanity check
     */
    if (old==NULL)
      return 0;

    /*
     * Make sure we free the old string.
     */
482
    SysFreeString(*old);
483 484 485 486

    /*
     * Allocate the new string
     */
487
    *old = SysAllocString(str);
488 489 490 491

     return 1;
}

492 493 494 495 496 497 498 499 500 501 502 503
/******************************************************************************
 *		SetOaNoCache (OLEAUT32.327)
 *
 * Instruct Ole Automation not to cache BSTR allocations.
 *
 * PARAMS
 *  None.
 *
 * RETURNS
 *  Nothing.
 *
 * NOTES
504
 *  SetOaNoCache does not release cached strings, so it leaks by design.
505 506 507
 */
void WINAPI SetOaNoCache(void)
{
508 509
    TRACE("\n");
    bstr_cache_enabled = FALSE;
510 511
}

512
static const WCHAR	_delimiter[] = {'!',0}; /* default delimiter apparently */
513
static const WCHAR	*pdelimiter = &_delimiter[0];
514

515
/***********************************************************************
516
 *		RegisterActiveObject (OLEAUT32.33)
517 518 519 520 521 522 523 524 525 526 527 528
 *
 * Registers an object in the global item table.
 *
 * PARAMS
 *  punk        [I] Object to register.
 *  rcid        [I] CLSID of the object.
 *  dwFlags     [I] Flags.
 *  pdwRegister [O] Address to store cookie of object registration in.
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: HRESULT code.
529
 */
530
HRESULT WINAPI DECLSPEC_HOTPATCH RegisterActiveObject(
531 532
	LPUNKNOWN punk,REFCLSID rcid,DWORD dwFlags,LPDWORD pdwRegister
) {
533 534 535 536
	WCHAR 			guidbuf[80];
	HRESULT			ret;
	LPRUNNINGOBJECTTABLE	runobtable;
	LPMONIKER		moniker;
537
        DWORD                   rot_flags = ROTFLAGS_REGISTRATIONKEEPSALIVE; /* default registration is strong */
538 539 540

	StringFromGUID2(rcid,guidbuf,39);
	ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
541
	if (FAILED(ret))
542 543 544 545 546 547
		return ret;
	ret = GetRunningObjectTable(0,&runobtable);
	if (FAILED(ret)) {
		IMoniker_Release(moniker);
		return ret;
	}
548 549 550
        if(dwFlags == ACTIVEOBJECT_WEAK)
          rot_flags = 0;
	ret = IRunningObjectTable_Register(runobtable,rot_flags,punk,moniker,pdwRegister);
551 552 553
	IRunningObjectTable_Release(runobtable);
	IMoniker_Release(moniker);
	return ret;
554 555
}

556
/***********************************************************************
557
 *		RevokeActiveObject (OLEAUT32.34)
558 559 560 561 562 563 564 565 566 567
 *
 * Revokes an object from the global item table.
 *
 * PARAMS
 *  xregister [I] Registration cookie.
 *  reserved  [I] Reserved. Set to NULL.
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: HRESULT code.
568
 */
569
HRESULT WINAPI DECLSPEC_HOTPATCH RevokeActiveObject(DWORD xregister,LPVOID reserved)
570
{
571 572 573 574 575 576 577 578 579
	LPRUNNINGOBJECTTABLE	runobtable;
	HRESULT			ret;

	ret = GetRunningObjectTable(0,&runobtable);
	if (FAILED(ret)) return ret;
	ret = IRunningObjectTable_Revoke(runobtable,xregister);
	if (SUCCEEDED(ret)) ret = S_OK;
	IRunningObjectTable_Release(runobtable);
	return ret;
580 581
}

582
/***********************************************************************
583
 *		GetActiveObject (OLEAUT32.35)
584 585 586 587 588 589 590 591 592 593 594
 *
 * Gets an object from the global item table.
 *
 * PARAMS
 *  rcid        [I] CLSID of the object.
 *  preserved   [I] Reserved. Set to NULL.
 *  ppunk       [O] Address to store object into.
 *
 * RETURNS
 *  Success: S_OK.
 *  Failure: HRESULT code.
595
 */
596
HRESULT WINAPI DECLSPEC_HOTPATCH GetActiveObject(REFCLSID rcid,LPVOID preserved,LPUNKNOWN *ppunk)
597
{
598 599 600 601 602 603 604
	WCHAR 			guidbuf[80];
	HRESULT			ret;
	LPRUNNINGOBJECTTABLE	runobtable;
	LPMONIKER		moniker;

	StringFromGUID2(rcid,guidbuf,39);
	ret = CreateItemMoniker(pdelimiter,guidbuf,&moniker);
605
	if (FAILED(ret))
606 607 608 609 610 611 612 613 614 615
		return ret;
	ret = GetRunningObjectTable(0,&runobtable);
	if (FAILED(ret)) {
		IMoniker_Release(moniker);
		return ret;
	}
	ret = IRunningObjectTable_GetObject(runobtable,moniker,ppunk);
	IRunningObjectTable_Release(runobtable);
	IMoniker_Release(moniker);
	return ret;
616
}
617

618

619 620 621
/***********************************************************************
 *           OaBuildVersion           [OLEAUT32.170]
 *
Jon Griffiths's avatar
Jon Griffiths committed
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646
 * Get the Ole Automation build version.
 *
 * PARAMS
 *  None
 *
 * RETURNS
 *  The build version.
 *
 * NOTES
 *  Known oleaut32.dll versions:
 *| OLE Ver.  Comments                   Date     Build Ver.
 *| --------  -------------------------  ----     ---------
 *| OLE 2.1   NT                         1993-95  10 3023
 *| OLE 2.1                                       10 3027
 *| Win32s    Ver 1.1e                            20 4049
 *| OLE 2.20  W95/NT                     1993-96  20 4112
 *| OLE 2.20  W95/NT                     1993-96  20 4118
 *| OLE 2.20  W95/NT                     1993-96  20 4122
 *| OLE 2.30  W95/NT                     1993-98  30 4265
 *| OLE 2.40  NT??                       1993-98  40 4267
 *| OLE 2.40  W98 SE orig. file          1993-98  40 4275
 *| OLE 2.40  W2K orig. file             1993-XX  40 4514
 *
 * Currently the versions returned are 2.20 for Win3.1, 2.30 for Win95 & NT 3.51,
 * and 2.40 for all later versions. The build number is maximum, i.e. 0xffff.
647
 */
648
ULONG WINAPI OaBuildVersion(void)
649
{
650
    switch(GetVersion() & 0x8000ffff)  /* mask off build number */
651
    {
652
    case 0x80000a03:  /* WIN31 */
653
		return MAKELONG(0xffff, 20);
654
    case 0x00003303:  /* NT351 */
655 656 657 658 659 660 661 662
		return MAKELONG(0xffff, 30);
    case 0x80000004:  /* WIN95; I'd like to use the "standard" w95 minor
		         version here (30), but as we still use w95
		         as default winver (which is good IMHO), I better
		         play safe and use the latest value for w95 for now.
		         Change this as soon as default winver gets changed
		         to something more recent */
    case 0x80000a04:  /* WIN98 */
663
    case 0x00000004:  /* NT40 */
664 665
    case 0x00000005:  /* W2K */
		return MAKELONG(0xffff, 40);
666 667 668 669
    case 0x00000105:  /* WinXP */
    case 0x00000006:  /* Vista */
    case 0x00000106:  /* Win7 */
		return MAKELONG(0xffff, 50);
670
    default:
671 672
		FIXME("Version value not known yet. Please investigate it !\n");
		return MAKELONG(0xffff, 40);  /* for now return the same value as for w2k */
673 674
    }
}
675

676 677 678
/******************************************************************************
 *		OleTranslateColor	[OLEAUT32.421]
 *
Jon Griffiths's avatar
Jon Griffiths committed
679 680 681 682 683 684 685 686 687 688 689 690 691
 * Convert an OLE_COLOR to a COLORREF.
 *
 * PARAMS
 *  clr       [I] Color to convert
 *  hpal      [I] Handle to a palette for the conversion
 *  pColorRef [O] Destination for converted color, or NULL to test if the conversion is ok
 *
 * RETURNS
 *  Success: S_OK. The conversion is ok, and pColorRef contains the converted color if non-NULL.
 *  Failure: E_INVALIDARG, if any argument is invalid.
 *
 * FIXME
 *  Document the conversion rules.
692 693 694 695 696 697 698 699 700
 */
HRESULT WINAPI OleTranslateColor(
  OLE_COLOR clr,
  HPALETTE  hpal,
  COLORREF* pColorRef)
{
  COLORREF colorref;
  BYTE b = HIBYTE(HIWORD(clr));

701
  TRACE("(%08x, %p, %p)\n", clr, hpal, pColorRef);
702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750

  /*
   * In case pColorRef is NULL, provide our own to simplify the code.
   */
  if (pColorRef == NULL)
    pColorRef = &colorref;

  switch (b)
  {
    case 0x00:
    {
      if (hpal != 0)
        *pColorRef =  PALETTERGB(GetRValue(clr),
                                 GetGValue(clr),
                                 GetBValue(clr));
      else
        *pColorRef = clr;

      break;
    }

    case 0x01:
    {
      if (hpal != 0)
      {
        PALETTEENTRY pe;
        /*
         * Validate the palette index.
         */
        if (GetPaletteEntries(hpal, LOWORD(clr), 1, &pe) == 0)
          return E_INVALIDARG;
      }

      *pColorRef = clr;

      break;
    }

    case 0x02:
      *pColorRef = clr;
      break;

    case 0x80:
    {
      int index = LOBYTE(LOWORD(clr));

      /*
       * Validate GetSysColor index.
       */
751
      if ((index < COLOR_SCROLLBAR) || (index > COLOR_MENUBAR))
752 753 754 755 756 757 758 759 760 761 762 763 764 765
        return E_INVALIDARG;

      *pColorRef =  GetSysColor(index);

      break;
    }

    default:
      return E_INVALIDARG;
  }

  return S_OK;
}

766
extern HRESULT WINAPI OLEAUTPS_DllGetClassObject(REFCLSID, REFIID, LPVOID *) DECLSPEC_HIDDEN;
767
extern BOOL WINAPI OLEAUTPS_DllMain(HINSTANCE, DWORD, LPVOID) DECLSPEC_HIDDEN;
768 769
extern HRESULT WINAPI OLEAUTPS_DllRegisterServer(void) DECLSPEC_HIDDEN;
extern HRESULT WINAPI OLEAUTPS_DllUnregisterServer(void) DECLSPEC_HIDDEN;
770

771 772
extern void _get_STDFONT_CF(LPVOID *);
extern void _get_STDPIC_CF(LPVOID *);
773

774 775 776 777 778
static HRESULT WINAPI PSDispatchFacBuf_QueryInterface(IPSFactoryBuffer *iface, REFIID riid, void **ppv)
{
    if (IsEqualIID(riid, &IID_IUnknown) ||
        IsEqualIID(riid, &IID_IPSFactoryBuffer))
    {
779
        IPSFactoryBuffer_AddRef(iface);
780
        *ppv = iface;
781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
        return S_OK;
    }
    return E_NOINTERFACE;
}

static ULONG WINAPI PSDispatchFacBuf_AddRef(IPSFactoryBuffer *iface)
{
    return 2;
}

static ULONG WINAPI PSDispatchFacBuf_Release(IPSFactoryBuffer *iface)
{
    return 1;
}

static HRESULT WINAPI PSDispatchFacBuf_CreateProxy(IPSFactoryBuffer *iface, IUnknown *pUnkOuter, REFIID riid, IRpcProxyBuffer **ppProxy, void **ppv)
{
    IPSFactoryBuffer *pPSFB;
    HRESULT hr;

    if (IsEqualIID(riid, &IID_IDispatch))
802
        hr = OLEAUTPS_DllGetClassObject(&CLSID_PSFactoryBuffer, &IID_IPSFactoryBuffer, (void **)&pPSFB);
803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
    else
        hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);

    if (FAILED(hr)) return hr;

    hr = IPSFactoryBuffer_CreateProxy(pPSFB, pUnkOuter, riid, ppProxy, ppv);

    IPSFactoryBuffer_Release(pPSFB);
    return hr;
}

static HRESULT WINAPI PSDispatchFacBuf_CreateStub(IPSFactoryBuffer *iface, REFIID riid, IUnknown *pUnkOuter, IRpcStubBuffer **ppStub)
{
    IPSFactoryBuffer *pPSFB;
    HRESULT hr;

    if (IsEqualIID(riid, &IID_IDispatch))
820
        hr = OLEAUTPS_DllGetClassObject(&CLSID_PSFactoryBuffer, &IID_IPSFactoryBuffer, (void **)&pPSFB);
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843
    else
        hr = TMARSHAL_DllGetClassObject(&CLSID_PSOAInterface, &IID_IPSFactoryBuffer, (void **)&pPSFB);

    if (FAILED(hr)) return hr;

    hr = IPSFactoryBuffer_CreateStub(pPSFB, riid, pUnkOuter, ppStub);

    IPSFactoryBuffer_Release(pPSFB);
    return hr;
}

static const IPSFactoryBufferVtbl PSDispatchFacBuf_Vtbl =
{
    PSDispatchFacBuf_QueryInterface,
    PSDispatchFacBuf_AddRef,
    PSDispatchFacBuf_Release,
    PSDispatchFacBuf_CreateProxy,
    PSDispatchFacBuf_CreateStub
};

/* This is the whole PSFactoryBuffer object, just the vtableptr */
static const IPSFactoryBufferVtbl *pPSDispatchFacBuf = &PSDispatchFacBuf_Vtbl;

Patrik Stridvall's avatar
Patrik Stridvall committed
844
/***********************************************************************
845
 *		DllGetClassObject (OLEAUT32.@)
Patrik Stridvall's avatar
Patrik Stridvall committed
846
 */
847
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
848 849 850 851 852 853 854 855 856
{
    *ppv = NULL;
    if (IsEqualGUID(rclsid,&CLSID_StdFont)) {
	if (IsEqualGUID(iid,&IID_IClassFactory)) {
	    _get_STDFONT_CF(ppv);
	    IClassFactory_AddRef((IClassFactory*)*ppv);
	    return S_OK;
	}
    }
857 858 859 860 861 862 863
    if (IsEqualGUID(rclsid,&CLSID_StdPicture)) {
	if (IsEqualGUID(iid,&IID_IClassFactory)) {
	    _get_STDPIC_CF(ppv);
	    IClassFactory_AddRef((IClassFactory*)*ppv);
	    return S_OK;
	}
    }
864 865 866 867 868
    if (IsEqualCLSID(rclsid, &CLSID_PSDispatch) && IsEqualIID(iid, &IID_IPSFactoryBuffer)) {
        *ppv = &pPSDispatchFacBuf;
        IPSFactoryBuffer_AddRef((IPSFactoryBuffer *)*ppv);
        return S_OK;
    }
869
    if (IsEqualGUID(rclsid,&CLSID_PSOAInterface)) {
870
	if (S_OK==TMARSHAL_DllGetClassObject(rclsid,iid,ppv))
871 872 873
	    return S_OK;
	/*FALLTHROUGH*/
    }
874 875
    if (IsEqualCLSID(rclsid, &CLSID_PSTypeComp) ||
        IsEqualCLSID(rclsid, &CLSID_PSTypeInfo) ||
876 877 878 879 880
        IsEqualCLSID(rclsid, &CLSID_PSTypeLib) ||
        IsEqualCLSID(rclsid, &CLSID_PSDispatch) ||
        IsEqualCLSID(rclsid, &CLSID_PSEnumVariant))
        return OLEAUTPS_DllGetClassObject(&CLSID_PSFactoryBuffer, iid, ppv);

881
    return OLEAUTPS_DllGetClassObject(rclsid, iid, ppv);
882 883
}

Patrik Stridvall's avatar
Patrik Stridvall committed
884
/***********************************************************************
885
 *		DllCanUnloadNow (OLEAUT32.@)
Jon Griffiths's avatar
Jon Griffiths committed
886 887 888 889 890 891 892 893
 *
 * Determine if this dll can be unloaded from the callers address space.
 *
 * PARAMS
 *  None.
 *
 * RETURNS
 *  Always returns S_FALSE. This dll cannot be unloaded.
Patrik Stridvall's avatar
Patrik Stridvall committed
894
 */
895
HRESULT WINAPI DllCanUnloadNow(void)
Jon Griffiths's avatar
Jon Griffiths committed
896
{
897 898
    return S_FALSE;
}
899 900 901 902 903 904

/*****************************************************************************
 *              DllMain         [OLEAUT32.@]
 */
BOOL WINAPI DllMain(HINSTANCE hInstDll, DWORD fdwReason, LPVOID lpvReserved)
{
905 906
    static const WCHAR oanocacheW[] = {'o','a','n','o','c','a','c','h','e',0};

907 908
    if(fdwReason == DLL_PROCESS_ATTACH)
        bstr_cache_enabled = !GetEnvironmentVariableW(oanocacheW, NULL, 0);
909

910
    return OLEAUTPS_DllMain( hInstDll, fdwReason, lpvReserved );
911
}
912

913 914 915 916 917
/***********************************************************************
 *		DllRegisterServer (OLEAUT32.@)
 */
HRESULT WINAPI DllRegisterServer(void)
{
918
    return OLEAUTPS_DllRegisterServer();
919 920 921 922 923 924 925 926 927 928
}

/***********************************************************************
 *		DllUnregisterServer (OLEAUT32.@)
 */
HRESULT WINAPI DllUnregisterServer(void)
{
    return OLEAUTPS_DllUnregisterServer();
}

929 930 931 932 933 934
/***********************************************************************
 *              OleIconToCursor (OLEAUT32.415)
 */
HCURSOR WINAPI OleIconToCursor( HINSTANCE hinstExe, HICON hIcon)
{
    FIXME("(%p,%p), partially implemented.\n",hinstExe,hIcon);
935
    /* FIXME: make an extended conversation from HICON to HCURSOR */
936 937
    return CopyCursor(hIcon);
}
938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046

/***********************************************************************
 *              GetAltMonthNames (OLEAUT32.@)
 */
HRESULT WINAPI GetAltMonthNames(LCID lcid, LPOLESTR **str)
{
    static const WCHAR ar_month1W[] = {0x645,0x62d,0x631,0x645,0};
    static const WCHAR ar_month2W[] = {0x635,0x641,0x631,0};
    static const WCHAR ar_month3W[] = {0x631,0x628,0x64a,0x639,' ',0x627,0x644,0x627,0x648,0x644,0};
    static const WCHAR ar_month4W[] = {0x631,0x628,0x64a,0x639,' ',0x627,0x644,0x62b,0x627,0x646,0x64a,0};
    static const WCHAR ar_month5W[] = {0x62c,0x645,0x627,0x62f,0x649,' ',0x627,0x644,0x627,0x648,0x644,0x649,0};
    static const WCHAR ar_month6W[] = {0x62c,0x645,0x627,0x62f,0x649,' ',0x627,0x644,0x62b,0x627,0x646,0x64a,0x629,0};
    static const WCHAR ar_month7W[] = {0x631,0x62c,0x628,0};
    static const WCHAR ar_month8W[] = {0x634,0x639,0x628,0x627,0x646,0};
    static const WCHAR ar_month9W[] = {0x631,0x645,0x636,0x627,0x646,0};
    static const WCHAR ar_month10W[] = {0x634,0x648,0x627,0x643,0};
    static const WCHAR ar_month11W[] = {0x630,0x648,' ',0x627,0x644,0x642,0x639,0x62f,0x629,0};
    static const WCHAR ar_month12W[] = {0x630,0x648,' ',0x627,0x644,0x62d,0x62c,0x629,0};

    static const WCHAR *arabic_hijri[] =
    {
        ar_month1W,
        ar_month2W,
        ar_month3W,
        ar_month4W,
        ar_month5W,
        ar_month6W,
        ar_month7W,
        ar_month8W,
        ar_month9W,
        ar_month10W,
        ar_month11W,
        ar_month12W,
        NULL
    };

    static const WCHAR pl_month1W[] = {'s','t','y','c','z','n','i','a',0};
    static const WCHAR pl_month2W[] = {'l','u','t','e','g','o',0};
    static const WCHAR pl_month3W[] = {'m','a','r','c','a',0};
    static const WCHAR pl_month4W[] = {'k','w','i','e','t','n','i','a',0};
    static const WCHAR pl_month5W[] = {'m','a','j','a',0};
    static const WCHAR pl_month6W[] = {'c','z','e','r','w','c','a',0};
    static const WCHAR pl_month7W[] = {'l','i','p','c','a',0};
    static const WCHAR pl_month8W[] = {'s','i','e','r','p','n','i','a',0};
    static const WCHAR pl_month9W[] = {'w','r','z','e',0x15b,'n','i','a',0};
    static const WCHAR pl_month10W[] = {'p','a',0x17a,'d','z','i','e','r','n','i','k','a',0};
    static const WCHAR pl_month11W[] = {'l','i','s','t','o','p','a','d','a',0};
    static const WCHAR pl_month12W[] = {'g','r','u','d','n','i','a',0};

    static const WCHAR *polish_genitive_names[] =
    {
        pl_month1W,
        pl_month2W,
        pl_month3W,
        pl_month4W,
        pl_month5W,
        pl_month6W,
        pl_month7W,
        pl_month8W,
        pl_month9W,
        pl_month10W,
        pl_month11W,
        pl_month12W,
        NULL
    };

    static const WCHAR ru_month1W[] = {0x44f,0x43d,0x432,0x430,0x440,0x44f,0};
    static const WCHAR ru_month2W[] = {0x444,0x435,0x432,0x440,0x430,0x43b,0x44f,0};
    static const WCHAR ru_month3W[] = {0x43c,0x430,0x440,0x442,0x430,0};
    static const WCHAR ru_month4W[] = {0x430,0x43f,0x440,0x435,0x43b,0x44f,0};
    static const WCHAR ru_month5W[] = {0x43c,0x430,0x44f,0};
    static const WCHAR ru_month6W[] = {0x438,0x44e,0x43d,0x44f,0};
    static const WCHAR ru_month7W[] = {0x438,0x44e,0x43b,0x44f,0};
    static const WCHAR ru_month8W[] = {0x430,0x432,0x433,0x443,0x441,0x442,0x430,0};
    static const WCHAR ru_month9W[] = {0x441,0x435,0x43d,0x442,0x44f,0x431,0x440,0x44f,0};
    static const WCHAR ru_month10W[] = {0x43e,0x43a,0x442,0x44f,0x431,0x440,0x44f,0};
    static const WCHAR ru_month11W[] = {0x43d,0x43e,0x44f,0x431,0x440,0x44f,0};
    static const WCHAR ru_month12W[] = {0x434,0x435,0x43a,0x430,0x431,0x440,0x44f,0};

    static const WCHAR *russian_genitive_names[] =
    {
        ru_month1W,
        ru_month2W,
        ru_month3W,
        ru_month4W,
        ru_month5W,
        ru_month6W,
        ru_month7W,
        ru_month8W,
        ru_month9W,
        ru_month10W,
        ru_month11W,
        ru_month12W,
        NULL
    };

    TRACE("%#x, %p\n", lcid, str);

    if (PRIMARYLANGID(LANGIDFROMLCID(lcid)) == LANG_ARABIC)
        *str = (LPOLESTR *)arabic_hijri;
    else if (PRIMARYLANGID(LANGIDFROMLCID(lcid)) == LANG_POLISH)
        *str = (LPOLESTR *)polish_genitive_names;
    else if (PRIMARYLANGID(LANGIDFROMLCID(lcid)) == LANG_RUSSIAN)
        *str = (LPOLESTR *)russian_genitive_names;
    else
        *str = NULL;

    return S_OK;
}