ddraw: Separate IDirectDrawGammaControl reference count.
[wine.git] / dlls / oleaut32 / typelib2.c
blobe9c7613d7384333022b79099bce3b25926e3a38b
1 /*
2 * TYPELIB2
4 * Copyright 2004 Alastair Bridgewater
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * 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
20 * --------------------------------------------------------------------------------------
21 * Known problems:
23 * Badly incomplete.
25 * Only works on little-endian systems.
29 #include "config.h"
30 #include "wine/port.h"
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <ctype.h>
38 #define COBJMACROS
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
42 #include "winerror.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnls.h"
46 #include "winreg.h"
47 #include "winuser.h"
49 #include "wine/unicode.h"
50 #include "objbase.h"
51 #include "typelib.h"
52 #include "wine/debug.h"
54 WINE_DEFAULT_DEBUG_CHANNEL(typelib2);
55 /* WINE_DEFAULT_DEBUG_CHANNEL(ole); */
58 /******************************************************************************
59 * ICreateTypeLib2 {OLEAUT32}
61 * NOTES
62 * The ICreateTypeLib2 interface provides an interface whereby one may create
63 * new type library (.tlb) files.
65 * This interface inherits from ICreateTypeLib, and can be freely cast back
66 * and forth between an ICreateTypeLib and an ICreateTypeLib2 on local clients.
67 * This dispensation applies only to ICreateTypeLib objects obtained on MSFT
68 * format type libraries (those made through CreateTypeLib2).
70 * METHODS
73 /******************************************************************************
74 * ICreateTypeInfo2 {OLEAUT32}
76 * NOTES
77 * The ICreateTypeInfo2 interface provides an interface whereby one may add
78 * type information to type library (.tlb) files.
80 * This interface inherits from ICreateTypeInfo, and can be freely cast back
81 * and forth between an ICreateTypeInfo and an ICreateTypeInfo2 on local clients.
82 * This dispensation applies only to ICreateTypeInfo objects obtained on MSFT
83 * format type libraries (those made through CreateTypeLib2).
85 * METHODS
88 /******************************************************************************
89 * ITypeLib2 {OLEAUT32}
91 * NOTES
92 * The ITypeLib2 interface provides an interface whereby one may query MSFT
93 * format type library (.tlb) files.
95 * This interface inherits from ITypeLib, and can be freely cast back and
96 * forth between an ITypeLib and an ITypeLib2 on local clients. This
97 * dispensation applies only to ITypeLib objects obtained on MSFT format type
98 * libraries (those made through CreateTypeLib2).
100 * METHODS
103 /******************************************************************************
104 * ITypeInfo2 {OLEAUT32}
106 * NOTES
107 * The ITypeInfo2 interface provides an interface whereby one may query type
108 * information stored in MSFT format type library (.tlb) files.
110 * This interface inherits from ITypeInfo, and can be freely cast back and
111 * forth between an ITypeInfo and an ITypeInfo2 on local clients. This
112 * dispensation applies only to ITypeInfo objects obtained on MSFT format type
113 * libraries (those made through CreateTypeLib2).
115 * METHODS
118 /*================== Implementation Structures ===================================*/
120 /* Used for storing cyclic list. Tail address is kept */
121 typedef enum tagCyclicListElementType {
122 CyclicListSentinel,
123 CyclicListFunc,
124 CyclicListVar
125 } CyclicListElementType;
126 typedef struct tagCyclicList {
127 struct tagCyclicList *next;
128 int indice;
129 int name;
130 CyclicListElementType type;
132 union {
133 int val;
134 int *data;
136 } CyclicList;
138 enum MSFT_segment_index {
139 MSFT_SEG_TYPEINFO = 0, /* type information */
140 MSFT_SEG_IMPORTINFO, /* import information */
141 MSFT_SEG_IMPORTFILES, /* import filenames */
142 MSFT_SEG_REFERENCES, /* references (?) */
143 MSFT_SEG_GUIDHASH, /* hash table for guids? */
144 MSFT_SEG_GUID, /* guid storage */
145 MSFT_SEG_NAMEHASH, /* hash table for names */
146 MSFT_SEG_NAME, /* name storage */
147 MSFT_SEG_STRING, /* string storage */
148 MSFT_SEG_TYPEDESC, /* type descriptions */
149 MSFT_SEG_ARRAYDESC, /* array descriptions */
150 MSFT_SEG_CUSTDATA, /* custom data */
151 MSFT_SEG_CUSTDATAGUID, /* custom data guids */
152 MSFT_SEG_UNKNOWN, /* ??? */
153 MSFT_SEG_UNKNOWN2, /* ??? */
154 MSFT_SEG_MAX /* total number of segments */
157 typedef struct tagMSFT_ImpFile {
158 int guid;
159 LCID lcid;
160 int version;
161 char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
162 } MSFT_ImpFile;
164 typedef struct tagICreateTypeLib2Impl
166 const ICreateTypeLib2Vtbl *lpVtbl;
167 const ITypeLib2Vtbl *lpVtblTypeLib2;
169 LONG ref;
171 WCHAR *filename;
173 MSFT_Header typelib_header;
174 INT helpStringDll;
175 MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
176 unsigned char *typelib_segment_data[MSFT_SEG_MAX];
177 int typelib_segment_block_length[MSFT_SEG_MAX];
179 int typelib_guids; /* Number of defined typelib guids */
180 int typeinfo_guids; /* Number of defined typeinfo guids */
182 INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
184 INT *typelib_namehash_segment;
185 INT *typelib_guidhash_segment;
187 struct tagICreateTypeInfo2Impl *typeinfos;
188 struct tagICreateTypeInfo2Impl *last_typeinfo;
189 } ICreateTypeLib2Impl;
191 static inline ICreateTypeLib2Impl *impl_from_ITypeLib2( ITypeLib2 *iface )
193 return (ICreateTypeLib2Impl *)((char*)iface - FIELD_OFFSET(ICreateTypeLib2Impl, lpVtblTypeLib2));
196 typedef struct tagICreateTypeInfo2Impl
198 const ICreateTypeInfo2Vtbl *lpVtbl;
199 const ITypeInfo2Vtbl *lpVtblTypeInfo2;
201 LONG ref;
203 ICreateTypeLib2Impl *typelib;
204 MSFT_TypeInfoBase *typeinfo;
206 struct tagCyclicList *typedata; /* tail of cyclic list */
208 TYPEKIND typekind;
209 int datawidth;
211 struct tagICreateTypeInfo2Impl *next_typeinfo;
212 struct tagICreateTypeInfo2Impl *dual;
213 } ICreateTypeInfo2Impl;
215 static inline ICreateTypeInfo2Impl *impl_from_ITypeInfo2( ITypeInfo2 *iface )
217 return (ICreateTypeInfo2Impl *)((char*)iface - FIELD_OFFSET(ICreateTypeInfo2Impl, lpVtblTypeInfo2));
220 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface);
222 static CyclicList *alloc_cyclic_list_item(CyclicListElementType type)
224 CyclicList *ret = heap_alloc_zero(sizeof(CyclicList));
225 if (!ret)
226 return NULL;
227 ret->type = type;
228 return ret;
231 /*================== Internal functions ===================================*/
233 static inline UINT cti2_get_var_count(const MSFT_TypeInfoBase *typeinfo)
235 return typeinfo->cElement >> 16;
238 static inline UINT cti2_get_func_count(const MSFT_TypeInfoBase *typeinfo)
240 return typeinfo->cElement & 0xFFFF;
243 static inline INT ctl2_get_record_size(const CyclicList *iter)
245 return iter->u.data[0] & 0xFFFF;
248 static void ctl2_update_var_size(const ICreateTypeInfo2Impl *This, CyclicList *var, int size)
250 int old = ctl2_get_record_size(var), i;
252 if (old >= size) return;
254 /* initialize fields included in size but currently unused */
255 for (i = old/sizeof(int); i < (size/sizeof(int) - 1); i++)
257 /* HelpContext/HelpStringContext being 0 means it's not set */
258 var->u.data[i] = (i == 5 || i == 9) ? 0 : -1;
261 var->u.data[0] += size - old;
262 This->typedata->next->u.val += size - old;
265 /* NOTE: entry always assumed to be a function */
266 static inline INVOKEKIND ctl2_get_invokekind(const CyclicList *func)
268 /* INVOKEKIND uses bit flags up to 8 */
269 return (func->u.data[4] >> 3) & 0xF;
272 static inline SYSKIND ctl2_get_syskind(const ICreateTypeLib2Impl *This)
274 return This->typelib_header.varflags & 0xF;
277 /****************************************************************************
278 * ctl2_init_header
280 * Initializes the type library header of a new typelib.
282 static void ctl2_init_header(
283 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
285 This->typelib_header.magic1 = MSFT_SIGNATURE;
286 This->typelib_header.magic2 = 0x00010002;
287 This->typelib_header.posguid = -1;
288 This->typelib_header.lcid = This->typelib_header.lcid2 = GetUserDefaultLCID();
289 This->typelib_header.varflags = 0x40;
290 This->typelib_header.version = 0;
291 This->typelib_header.flags = 0;
292 This->typelib_header.nrtypeinfos = 0;
293 This->typelib_header.helpstring = -1;
294 This->typelib_header.helpstringcontext = 0;
295 This->typelib_header.helpcontext = 0;
296 This->typelib_header.nametablecount = 0;
297 This->typelib_header.nametablechars = 0;
298 This->typelib_header.NameOffset = -1;
299 This->typelib_header.helpfile = -1;
300 This->typelib_header.CustomDataOffset = -1;
301 This->typelib_header.res44 = 0x20;
302 This->typelib_header.res48 = 0x80;
303 This->typelib_header.dispatchpos = -1;
304 This->typelib_header.nimpinfos = 0;
305 This->helpStringDll = -1;
308 /****************************************************************************
309 * ctl2_init_segdir
311 * Initializes the segment directory of a new typelib.
313 static void ctl2_init_segdir(
314 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
316 int i;
317 MSFT_pSeg *segdir;
319 segdir = &This->typelib_segdir[MSFT_SEG_TYPEINFO];
321 for (i = 0; i < 15; i++) {
322 segdir[i].offset = -1;
323 segdir[i].length = 0;
324 segdir[i].res08 = -1;
325 segdir[i].res0c = 0x0f;
329 /****************************************************************************
330 * ctl2_hash_guid
332 * Generates a hash key from a GUID.
334 * RETURNS
336 * The hash key for the GUID.
338 static int ctl2_hash_guid(
339 REFGUID guid) /* [I] The guid to find. */
341 int hash;
342 int i;
344 hash = 0;
345 for (i = 0; i < 8; i ++) {
346 hash ^= ((const short *)guid)[i];
349 return hash & 0x1f;
352 /****************************************************************************
353 * ctl2_find_guid
355 * Locates a guid in a type library.
357 * RETURNS
359 * The offset into the GUID segment of the guid, or -1 if not found.
361 static int ctl2_find_guid(
362 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
363 int hash_key, /* [I] The hash key for the guid. */
364 REFGUID guid) /* [I] The guid to find. */
366 int offset;
367 MSFT_GuidEntry *guidentry;
369 offset = This->typelib_guidhash_segment[hash_key];
370 while (offset != -1) {
371 guidentry = (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][offset];
373 if (IsEqualGUID(guidentry, guid)) return offset;
375 offset = guidentry->next_hash;
378 return offset;
381 /****************************************************************************
382 * ctl2_find_name
384 * Locates a name in a type library.
386 * RETURNS
388 * The offset into the NAME segment of the name, or -1 if not found.
390 * NOTES
392 * The name must be encoded as with ctl2_encode_name().
394 static int ctl2_find_name(
395 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
396 const char *name) /* [I] The encoded name to find. */
398 int offset;
399 int *namestruct;
401 offset = This->typelib_namehash_segment[name[2] & 0x7f];
402 while (offset != -1) {
403 namestruct = (int *)&This->typelib_segment_data[MSFT_SEG_NAME][offset];
405 if (!((namestruct[2] ^ *((const int *)name)) & 0xffff00ff)) {
406 /* hash codes and lengths match, final test */
407 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
410 /* move to next item in hash bucket */
411 offset = namestruct[1];
414 return offset;
417 /****************************************************************************
418 * ctl2_encode_name
420 * Encodes a name string to a form suitable for storing into a type library
421 * or comparing to a name stored in a type library.
423 * RETURNS
425 * The length of the encoded name, including padding and length+hash fields.
427 * NOTES
429 * Will throw an exception if name or result are NULL. Is not multithread
430 * safe in the slightest.
432 static int ctl2_encode_name(
433 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (used for LCID only). */
434 const WCHAR *name, /* [I] The name string to encode. */
435 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
437 int length;
438 static char converted_name[0x104];
439 int offset;
440 int value;
442 length = WideCharToMultiByte(CP_ACP, 0, name, strlenW(name), converted_name+4, 0x100, NULL, NULL);
443 converted_name[0] = length & 0xff;
445 converted_name[length + 4] = 0;
447 converted_name[1] = 0x00;
449 value = LHashValOfNameSysA(ctl2_get_syskind(This), This->typelib_header.lcid, converted_name + 4);
451 converted_name[2] = value;
452 converted_name[3] = value >> 8;
454 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
456 *result = converted_name;
458 return (length + 7) & ~3;
461 /****************************************************************************
462 * ctl2_decode_name
464 * Converts string stored in typelib data to unicode.
466 static void ctl2_decode_name(
467 char *data, /* [I] String to be decoded */
468 WCHAR **string) /* [O] Decoded string */
470 int i, length;
471 static WCHAR converted_string[0x104];
473 length = data[0];
475 for(i=0; i<length; i++)
476 converted_string[i] = data[i+4];
477 converted_string[length] = '\0';
479 *string = converted_string;
482 /****************************************************************************
483 * ctl2_encode_string
485 * Encodes a string to a form suitable for storing into a type library or
486 * comparing to a string stored in a type library.
488 * RETURNS
490 * The length of the encoded string, including padding and length fields.
492 * NOTES
494 * Will throw an exception if string or result are NULL. Is not multithread
495 * safe in the slightest.
497 static int ctl2_encode_string(
498 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (not used?). */
499 const WCHAR *string, /* [I] The string to encode. */
500 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
502 int length;
503 static char converted_string[0x104];
504 int offset;
506 length = WideCharToMultiByte(CP_ACP, 0, string, strlenW(string), converted_string+2, 0x102, NULL, NULL);
507 converted_string[0] = length & 0xff;
508 converted_string[1] = (length >> 8) & 0xff;
510 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
512 *result = converted_string;
514 return (length + 5) & ~3;
517 /****************************************************************************
518 * ctl2_decode_string
520 * Converts string stored in typelib data to unicode.
522 static void ctl2_decode_string(
523 unsigned char *data,/* [I] String to be decoded */
524 WCHAR **string) /* [O] Decoded string */
526 int i, length;
527 static WCHAR converted_string[0x104];
529 length = data[0] + (data[1]<<8);
530 if((length&0x3) == 1)
531 length >>= 2;
533 for(i=0; i<length; i++)
534 converted_string[i] = data[i+2];
535 converted_string[length] = '\0';
537 *string = converted_string;
540 /****************************************************************************
541 * ctl2_alloc_segment
543 * Allocates memory from a segment in a type library.
545 * RETURNS
547 * Success: The offset within the segment of the new data area.
548 * Failure: -1 (this is invariably an out of memory condition).
550 * BUGS
552 * Does not (yet) handle the case where the allocated segment memory needs to grow.
554 static int ctl2_alloc_segment(
555 ICreateTypeLib2Impl *This, /* [I] The type library in which to allocate. */
556 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
557 int size, /* [I] The amount to allocate. */
558 int block_size) /* [I] Initial allocation block size, or 0 for default. */
560 int offset;
562 if(!This->typelib_segment_data[segment]) {
563 if (!block_size) block_size = 0x2000;
565 This->typelib_segment_block_length[segment] = block_size;
566 This->typelib_segment_data[segment] = heap_alloc(block_size);
567 if (!This->typelib_segment_data[segment]) return -1;
568 memset(This->typelib_segment_data[segment], 0x57, block_size);
571 while ((This->typelib_segdir[segment].length + size) > This->typelib_segment_block_length[segment]) {
572 unsigned char *block;
574 block_size = This->typelib_segment_block_length[segment];
575 block = heap_realloc(This->typelib_segment_data[segment], block_size << 1);
576 if (!block) return -1;
578 if (segment == MSFT_SEG_TYPEINFO) {
579 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
580 ICreateTypeInfo2Impl *typeinfo;
582 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
583 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - This->typelib_segment_data[segment]];
587 memset(block + block_size, 0x57, block_size);
588 This->typelib_segment_block_length[segment] = block_size << 1;
589 This->typelib_segment_data[segment] = block;
592 offset = This->typelib_segdir[segment].length;
593 This->typelib_segdir[segment].length += size;
595 return offset;
598 /****************************************************************************
599 * ctl2_alloc_typeinfo
601 * Allocates and initializes a typeinfo structure in a type library.
603 * RETURNS
605 * Success: The offset of the new typeinfo.
606 * Failure: -1 (this is invariably an out of memory condition).
608 static int ctl2_alloc_typeinfo(
609 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
610 int nameoffset) /* [I] The offset of the name for this typeinfo. */
612 int offset;
613 MSFT_TypeInfoBase *typeinfo;
615 offset = ctl2_alloc_segment(This, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
616 if (offset == -1) return -1;
618 This->typelib_typeinfo_offsets[This->typelib_header.nrtypeinfos++] = offset;
620 typeinfo = (void *)(This->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
622 typeinfo->typekind = (This->typelib_header.nrtypeinfos - 1) << 16;
623 typeinfo->memoffset = -1; /* should be EOF if no elements */
624 typeinfo->res2 = 0;
625 typeinfo->res3 = 0;
626 typeinfo->res4 = 3;
627 typeinfo->res5 = 0;
628 typeinfo->cElement = 0;
629 typeinfo->res7 = 0;
630 typeinfo->res8 = 0;
631 typeinfo->res9 = 0;
632 typeinfo->resA = 0;
633 typeinfo->posguid = -1;
634 typeinfo->flags = 0;
635 typeinfo->NameOffset = nameoffset;
636 typeinfo->version = 0;
637 typeinfo->docstringoffs = -1;
638 typeinfo->helpstringcontext = 0;
639 typeinfo->helpcontext = 0;
640 typeinfo->oCustData = -1;
641 typeinfo->cbSizeVft = 0;
642 typeinfo->cImplTypes = 0;
643 typeinfo->size = 0;
644 typeinfo->datatype1 = -1;
645 typeinfo->datatype2 = 0;
646 typeinfo->res18 = 0;
647 typeinfo->res19 = -1;
649 return offset;
652 /****************************************************************************
653 * ctl2_alloc_guid
655 * Allocates and initializes a GUID structure in a type library. Also updates
656 * the GUID hash table as needed.
658 * RETURNS
660 * Success: The offset of the new GUID.
661 * Failure: -1 (this is invariably an out of memory condition).
663 static int ctl2_alloc_guid(
664 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
665 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
667 int offset;
668 MSFT_GuidEntry *guid_space;
669 int hash_key;
671 hash_key = ctl2_hash_guid(&guid->guid);
673 offset = ctl2_find_guid(This, hash_key, &guid->guid);
674 if (offset != -1) return offset;
676 offset = ctl2_alloc_segment(This, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
677 if (offset == -1) return -1;
679 guid_space = (void *)(This->typelib_segment_data[MSFT_SEG_GUID] + offset);
680 *guid_space = *guid;
682 guid_space->next_hash = This->typelib_guidhash_segment[hash_key];
683 This->typelib_guidhash_segment[hash_key] = offset;
685 return offset;
688 /****************************************************************************
689 * ctl2_alloc_name
691 * Allocates and initializes a name within a type library. Also updates the
692 * name hash table as needed.
694 * RETURNS
696 * Success: The offset within the segment of the new name.
697 * Failure: -1 (this is invariably an out of memory condition).
699 static int ctl2_alloc_name(
700 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
701 const WCHAR *name) /* [I] The name to store. */
703 int length;
704 int offset;
705 MSFT_NameIntro *name_space;
706 char *encoded_name;
708 length = ctl2_encode_name(This, name, &encoded_name);
710 offset = ctl2_find_name(This, encoded_name);
711 if (offset != -1) return offset;
713 offset = ctl2_alloc_segment(This, MSFT_SEG_NAME, length + 8, 0);
714 if (offset == -1) return -1;
716 name_space = (void *)(This->typelib_segment_data[MSFT_SEG_NAME] + offset);
717 name_space->hreftype = -1;
718 name_space->next_hash = -1;
719 memcpy(&name_space->namelen, encoded_name, length);
721 if (This->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
722 name_space->next_hash = This->typelib_namehash_segment[encoded_name[2] & 0x7f];
724 This->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
726 This->typelib_header.nametablecount += 1;
727 This->typelib_header.nametablechars += *encoded_name;
729 return offset;
732 /****************************************************************************
733 * ctl2_alloc_string
735 * Allocates and initializes a string in a type library.
737 * RETURNS
739 * Success: The offset within the segment of the new string.
740 * Failure: -1 (this is invariably an out of memory condition).
742 static int ctl2_alloc_string(
743 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
744 const WCHAR *string) /* [I] The string to store. */
746 int length;
747 int offset;
748 unsigned char *string_space;
749 char *encoded_string;
751 length = ctl2_encode_string(This, string, &encoded_string);
753 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_STRING].length;
754 offset += (((This->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
755 This->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
756 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
759 offset = ctl2_alloc_segment(This, MSFT_SEG_STRING, length, 0);
760 if (offset == -1) return -1;
762 string_space = This->typelib_segment_data[MSFT_SEG_STRING] + offset;
763 memcpy(string_space, encoded_string, length);
765 return offset;
768 /****************************************************************************
769 * ctl2_alloc_importinfo
771 * Allocates and initializes an import information structure in a type library.
773 * RETURNS
775 * Success: The offset of the new importinfo.
776 * Failure: -1 (this is invariably an out of memory condition).
778 static int ctl2_alloc_importinfo(
779 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
780 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
782 int offset;
783 MSFT_ImpInfo *impinfo_space;
785 impinfo_space = (MSFT_ImpInfo*)&This->typelib_segment_data[MSFT_SEG_IMPORTINFO][0];
786 for (offset=0; offset<This->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
787 offset+=sizeof(MSFT_ImpInfo)) {
788 if(impinfo_space->oImpFile == impinfo->oImpFile
789 && impinfo_space->oGuid == impinfo->oGuid)
790 return offset;
792 impinfo_space += 1;
795 impinfo->flags |= This->typelib_header.nimpinfos++;
797 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
798 if (offset == -1) return -1;
800 impinfo_space = (void *)(This->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
801 *impinfo_space = *impinfo;
803 return offset;
806 /****************************************************************************
807 * ctl2_alloc_importfile
809 * Allocates and initializes an import file definition in a type library.
811 * RETURNS
813 * Success: The offset of the new importinfo.
814 * Failure: -1 (this is invariably an out of memory condition).
816 static int ctl2_alloc_importfile(
817 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
818 int guidoffset, /* [I] The offset to the GUID for the imported library. */
819 LCID lcid, /* [I] The LCID of imported library. */
820 int major_version, /* [I] The major version number of the imported library. */
821 int minor_version, /* [I] The minor version number of the imported library. */
822 const WCHAR *filename) /* [I] The filename of the imported library. */
824 int length;
825 int offset;
826 MSFT_ImpFile *importfile;
827 char *encoded_string;
829 length = ctl2_encode_string(This, filename, &encoded_string);
831 encoded_string[0] <<= 2;
832 encoded_string[0] |= 1;
834 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
835 offset += (((((This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
836 This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 5) & 0xfffc) + 0xc) {
837 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
840 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
841 if (offset == -1) return -1;
843 importfile = (MSFT_ImpFile *)&This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
844 importfile->guid = guidoffset;
845 importfile->lcid = lcid;
846 importfile->version = major_version | (minor_version << 16);
847 memcpy(importfile->filename, encoded_string, length);
849 return offset;
852 /****************************************************************************
853 * ctl2_encode_variant
855 * Encodes a variant, inline if possible or in custom data segment
857 * RETURNS
859 * Success: S_OK
860 * Failure: Error code from winerror.h
862 static HRESULT ctl2_encode_variant(
863 ICreateTypeLib2Impl *This, /* [I] The typelib to allocate data in */
864 int *encoded_value, /* [O] The encoded default value or data offset */
865 VARIANT *value, /* [I] Default value to be encoded */
866 VARTYPE arg_type) /* [I] Argument type */
868 VARIANT v;
869 HRESULT hres;
870 int mask = 0;
872 TRACE("%p %d %d\n", This, V_VT(value), arg_type);
874 if(arg_type == VT_INT)
875 arg_type = VT_I4;
876 if(arg_type == VT_UINT)
877 arg_type = VT_UI4;
879 v = *value;
880 if(V_VT(value) != arg_type) {
881 hres = VariantChangeType(&v, value, 0, arg_type);
882 if(FAILED(hres))
883 return hres;
886 /* Check if default value can be stored in encoded_value */
887 switch(arg_type) {
888 case VT_I4:
889 case VT_UI4:
890 mask = 0x3ffffff;
891 if(V_UI4(&v)>0x3ffffff)
892 break;
893 case VT_I1:
894 case VT_UI1:
895 case VT_BOOL:
896 if(!mask)
897 mask = 0xff;
898 case VT_I2:
899 case VT_UI2:
900 if(!mask)
901 mask = 0xffff;
902 *encoded_value = (V_UI4(&v)&mask) | ((0x80+0x4*arg_type)<<24);
903 return S_OK;
906 switch(arg_type) {
907 case VT_I4:
908 case VT_R4:
909 case VT_UI4:
910 case VT_INT:
911 case VT_UINT:
912 case VT_HRESULT:
913 case VT_PTR: {
914 /* Construct the data to be allocated */
915 int data[2];
916 data[0] = arg_type + (V_UI4(&v)<<16);
917 data[1] = (V_UI4(&v)>>16) + 0x57570000;
919 /* Check if the data was already allocated */
920 /* Currently the structures doesn't allow to do it in a nice way */
921 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-8; *encoded_value+=4)
922 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8))
923 return S_OK;
925 /* Allocate the data */
926 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
927 if(*encoded_value == -1)
928 return E_OUTOFMEMORY;
930 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8);
931 return S_OK;
933 case VT_BSTR: {
934 /* Construct the data */
935 int i, len = (6+SysStringLen(V_BSTR(&v))+3) & ~0x3;
936 char *data = heap_alloc(len);
938 if(!data)
939 return E_OUTOFMEMORY;
941 *((unsigned short*)data) = arg_type;
942 *((unsigned*)(data+2)) = SysStringLen(V_BSTR(&v));
943 for(i=0; i<SysStringLen(V_BSTR(&v)); i++) {
944 if(V_BSTR(&v)[i] <= 0x7f)
945 data[i+6] = V_BSTR(&v)[i];
946 else
947 data[i+6] = '?';
949 WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), SysStringLen(V_BSTR(&v)), &data[6], len-6, NULL, NULL);
950 for(i=6+SysStringLen(V_BSTR(&v)); i<len; i++)
951 data[i] = 0x57;
953 /* Check if the data was already allocated */
954 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-len; *encoded_value+=4)
955 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len)) {
956 heap_free(data);
957 return S_OK;
960 /* Allocate the data */
961 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, len, 0);
962 if(*encoded_value == -1) {
963 heap_free(data);
964 return E_OUTOFMEMORY;
967 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len);
968 heap_free(data);
969 return S_OK;
971 default:
972 FIXME("Argument type not yet handled\n");
973 return E_NOTIMPL;
977 static int ctl2_find_custdata(
978 ICreateTypeLib2Impl *This,
979 REFGUID guid,
980 int offset)
982 while (offset != -1) {
983 MSFT_CDGuid *cdentry =
984 (MSFT_CDGuid *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
985 MSFT_GuidEntry *guidentry =
986 (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][cdentry->GuidOffset];
988 if (IsEqualGUID(guidentry, guid))
989 return offset;
991 offset = cdentry->next;
994 return -1;
997 /****************************************************************************
998 * ctl2_decode_variant
1000 * Decodes a variant
1002 * RETURNS
1004 * Success: S_OK
1005 * Failure: Error code from winerror.h
1007 static HRESULT ctl2_decode_variant(
1008 ICreateTypeLib2Impl *This, /* [I] The typelib that contains the variant */
1009 int data_offs, /* [I] Offset within the data array, or the encoded value itself */
1010 VARIANT *value) /* [O] Decoded value */
1012 unsigned char *encoded_data;
1013 VARTYPE type;
1015 if (data_offs & 0x80000000) {
1016 /* data_offs contains the encoded value */
1017 V_VT(value) = (data_offs & ~0x80000000) >> 26;
1018 V_UI4(value) = data_offs & ~0xFF000000;
1019 return S_OK;
1022 encoded_data = &This->typelib_segment_data[MSFT_SEG_CUSTDATA][data_offs];
1023 type = *encoded_data;
1025 switch(type) {
1026 case VT_I4:
1027 case VT_R4:
1028 case VT_UI4:
1029 case VT_INT:
1030 case VT_UINT:
1031 case VT_HRESULT:
1032 case VT_PTR: {
1033 V_VT(value) = type;
1034 V_UI4(value) = *(unsigned*)(encoded_data + 2);
1035 return S_OK;
1037 case VT_BSTR: {
1038 unsigned len, i;
1040 len = *(unsigned*)(encoded_data + 2);
1042 V_VT(value) = type;
1043 V_BSTR(value) = SysAllocStringByteLen(NULL, len * sizeof(OLECHAR));
1044 for (i = 0; i < len; ++i)
1045 V_BSTR(value)[i] = *(encoded_data + 6 + i);
1047 return S_OK;
1049 default:
1050 FIXME("Don't yet have decoder for this VARTYPE: %u\n", type);
1051 return E_NOTIMPL;
1055 /****************************************************************************
1056 * ctl2_set_custdata
1058 * Adds a custom data element to an object in a type library.
1060 * RETURNS
1062 * Success: S_OK.
1063 * Failure: One of E_INVALIDARG or E_OUTOFMEMORY.
1065 static HRESULT ctl2_set_custdata(
1066 ICreateTypeLib2Impl *This, /* [I] The type library to store the custom data in. */
1067 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
1068 VARIANT *pVarVal, /* [I] The custom data itself. */
1069 int *offset) /* [I/O] The list of custom data to prepend to. */
1071 MSFT_GuidEntry guidentry;
1072 HRESULT status;
1073 int dataoffset;
1074 int guidoffset;
1075 int custoffset;
1076 int *custdata;
1077 BOOL new_segment = FALSE;
1079 switch(V_VT(pVarVal))
1081 case VT_I4:
1082 case VT_R4:
1083 case VT_UI4:
1084 case VT_INT:
1085 case VT_UINT:
1086 case VT_HRESULT:
1087 case VT_BSTR:
1088 /* empty */
1089 break;
1090 default:
1091 return DISP_E_BADVARTYPE;
1094 guidentry.guid = *guid;
1096 guidentry.hreftype = -1;
1097 guidentry.next_hash = -1;
1099 guidoffset = ctl2_alloc_guid(This, &guidentry);
1100 if (guidoffset == -1) return E_OUTOFMEMORY;
1102 status = ctl2_encode_variant(This, &dataoffset, pVarVal, V_VT(pVarVal));
1103 if (status)
1104 return status;
1106 custoffset = ctl2_find_custdata(This, guid, *offset);
1107 if (custoffset == -1) {
1108 custoffset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATAGUID, 12, 0);
1109 if (custoffset == -1)
1110 return E_OUTOFMEMORY;
1111 new_segment = TRUE;
1114 custdata = (int *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1115 custdata[0] = guidoffset;
1116 custdata[1] = dataoffset;
1117 if (new_segment) {
1118 custdata[2] = *offset;
1119 *offset = custoffset;
1122 return S_OK;
1125 /****************************************************************************
1126 * ctl2_encode_typedesc
1128 * Encodes a type description, storing information in the TYPEDESC and ARRAYDESC
1129 * segments as needed.
1131 * RETURNS
1133 * Success: 0.
1134 * Failure: -1.
1136 static int ctl2_encode_typedesc(
1137 ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the TYPEDESC. */
1138 const TYPEDESC *tdesc, /* [I] The type description to encode. */
1139 int *encoded_tdesc, /* [O] The encoded type description. */
1140 int *width, /* [O] The width of the type, or NULL. */
1141 int *alignment, /* [O] The alignment of the type, or NULL. */
1142 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1144 int default_tdesc;
1145 int scratch;
1146 int typeoffset;
1147 int arrayoffset;
1148 int *typedata;
1149 int *arraydata;
1150 int target_type;
1151 int child_size;
1153 default_tdesc = 0x80000000 | (tdesc->vt << 16) | tdesc->vt;
1154 if (!width) width = &scratch;
1155 if (!alignment) alignment = &scratch;
1156 if (!decoded_size) decoded_size = &scratch;
1158 *decoded_size = 0;
1160 switch (tdesc->vt) {
1161 case VT_UI1:
1162 case VT_I1:
1163 *encoded_tdesc = default_tdesc;
1164 *width = 1;
1165 *alignment = 1;
1166 break;
1168 case VT_INT:
1169 *encoded_tdesc = 0x80000000 | (VT_I4 << 16) | VT_INT;
1170 if (ctl2_get_syskind(This) == SYS_WIN16) {
1171 *width = 2;
1172 *alignment = 2;
1173 } else {
1174 *width = 4;
1175 *alignment = 4;
1177 break;
1179 case VT_UINT:
1180 *encoded_tdesc = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
1181 if (ctl2_get_syskind(This) == SYS_WIN16) {
1182 *width = 2;
1183 *alignment = 2;
1184 } else {
1185 *width = 4;
1186 *alignment = 4;
1188 break;
1190 case VT_UI2:
1191 case VT_I2:
1192 case VT_BOOL:
1193 *encoded_tdesc = default_tdesc;
1194 *width = 2;
1195 *alignment = 2;
1196 break;
1198 case VT_I4:
1199 case VT_UI4:
1200 case VT_R4:
1201 case VT_ERROR:
1202 case VT_BSTR:
1203 case VT_HRESULT:
1204 *encoded_tdesc = default_tdesc;
1205 *width = 4;
1206 *alignment = 4;
1207 break;
1209 case VT_CY:
1210 *encoded_tdesc = default_tdesc;
1211 *width = 8;
1212 *alignment = 4; /* guess? */
1213 break;
1215 case VT_VOID:
1216 *encoded_tdesc = 0x80000000 | (VT_EMPTY << 16) | tdesc->vt;
1217 *width = 0;
1218 *alignment = 1;
1219 break;
1221 case VT_PTR:
1222 case VT_SAFEARRAY:
1223 /* FIXME: Make with the error checking. */
1224 FIXME("PTR or SAFEARRAY vartype, may not work correctly.\n");
1226 ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1228 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1229 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1230 if (((typedata[0] & 0xffff) == tdesc->vt) && (typedata[1] == target_type)) break;
1233 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1234 int mix_field;
1236 if (target_type & 0x80000000) {
1237 mix_field = (target_type >> 16) & VT_TYPEMASK;
1238 } else {
1239 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1240 switch((typedata[0]>>16) & ~VT_ARRAY)
1242 case VT_UI1:
1243 case VT_I1:
1244 case VT_UI2:
1245 case VT_I2:
1246 case VT_I4:
1247 case VT_UI4:
1248 mix_field = typedata[0]>>16;
1249 break;
1250 default:
1251 mix_field = ((typedata[0] >> 16) == 0x7fff) ? 0x7fff : 0x7ffe;
1252 break;
1256 if (tdesc->vt == VT_PTR)
1257 mix_field |= VT_BYREF;
1258 else if (tdesc->vt == VT_SAFEARRAY)
1259 mix_field |= VT_ARRAY;
1261 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1262 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1264 typedata[0] = (mix_field << 16) | tdesc->vt;
1265 typedata[1] = target_type;
1268 *encoded_tdesc = typeoffset;
1270 *width = 4;
1271 *alignment = 4;
1272 *decoded_size = sizeof(TYPEDESC) + child_size;
1273 break;
1275 case VT_CARRAY:
1277 /* FIXME: Make with the error checking. */
1278 int num_dims = tdesc->u.lpadesc->cDims, elements = 1, dim;
1280 ctl2_encode_typedesc(This, &tdesc->u.lpadesc->tdescElem, &target_type, width, alignment, NULL);
1281 arrayoffset = ctl2_alloc_segment(This, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1282 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1284 arraydata[0] = target_type;
1285 arraydata[1] = num_dims;
1286 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1287 arraydata += 2;
1289 for(dim = 0; dim < num_dims; dim++) {
1290 arraydata[0] = tdesc->u.lpadesc->rgbounds[dim].cElements;
1291 arraydata[1] = tdesc->u.lpadesc->rgbounds[dim].lLbound;
1292 elements *= tdesc->u.lpadesc->rgbounds[dim].cElements;
1293 arraydata += 2;
1295 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1296 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1298 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1299 typedata[1] = arrayoffset;
1301 *encoded_tdesc = typeoffset;
1302 *width = *width * elements;
1303 *decoded_size = sizeof(ARRAYDESC) + (num_dims - 1) * sizeof(SAFEARRAYBOUND);
1305 break;
1307 case VT_USERDEFINED:
1309 const MSFT_TypeInfoBase *basetype;
1310 INT basevt = 0x7fff;
1312 TRACE("USERDEFINED.\n");
1313 if (tdesc->u.hreftype % sizeof(*basetype) == 0 && tdesc->u.hreftype < This->typelib_segdir[MSFT_SEG_TYPEINFO].length)
1315 basetype = (MSFT_TypeInfoBase*)&(This->typelib_segment_data[MSFT_SEG_TYPEINFO][tdesc->u.hreftype]);
1316 switch(basetype->typekind & 0xf)
1318 case TKIND_ENUM:
1319 basevt = VT_I4;
1320 break;
1321 default:
1322 FIXME("USERDEFINED basetype %d not handled\n", basetype->typekind & 0xf);
1323 break;
1326 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1327 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1328 if ((typedata[0] == ((basevt << 16) | VT_USERDEFINED)) && (typedata[1] == tdesc->u.hreftype)) break;
1331 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1332 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1333 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1335 typedata[0] = (basevt << 16) | VT_USERDEFINED;
1336 typedata[1] = tdesc->u.hreftype;
1339 *encoded_tdesc = typeoffset;
1340 *width = 0;
1341 *alignment = 1;
1342 break;
1345 default:
1346 FIXME("Unrecognized type %d.\n", tdesc->vt);
1347 *encoded_tdesc = default_tdesc;
1348 *width = 0;
1349 *alignment = 1;
1350 break;
1353 return 0;
1356 /****************************************************************************
1357 * ctl2_decode_typedesc
1359 * Decodes a type description from an ICreateTypeLib2Impl.
1361 * RETURNS
1363 * Success: S_OK.
1364 * Failure: HRESULT error code.
1366 static HRESULT ctl2_decode_typedesc(
1367 ICreateTypeLib2Impl *This, /* [I] The type library from which to decode the TYPEDESC. */
1368 int encoded_tdesc, /* [I] The encoded type description. */
1369 TYPEDESC *tdesc) /* [O] The decoded type description. */
1371 int *typedata, i;
1372 HRESULT hres;
1374 if (encoded_tdesc & 0x80000000) {
1375 tdesc->vt = encoded_tdesc & VT_TYPEMASK;
1376 tdesc->u.lptdesc = NULL;
1377 return S_OK;
1380 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][encoded_tdesc];
1382 tdesc->vt = typedata[0] & 0xFFFF;
1384 switch(tdesc->vt) {
1385 case VT_PTR:
1386 case VT_SAFEARRAY:
1387 tdesc->u.lptdesc = heap_alloc_zero(sizeof(TYPEDESC));
1388 if (!tdesc->u.lptdesc)
1389 return E_OUTOFMEMORY;
1391 hres = ctl2_decode_typedesc(This, typedata[1], tdesc->u.lptdesc);
1392 if (FAILED(hres)) {
1393 heap_free(tdesc->u.lptdesc);
1394 return hres;
1397 return S_OK;
1399 case VT_CARRAY: {
1400 int arrayoffset, *arraydata, num_dims;
1402 arrayoffset = typedata[1];
1403 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1404 num_dims = arraydata[1] & 0xFFFF;
1406 tdesc->u.lpadesc = heap_alloc_zero(sizeof(ARRAYDESC) + sizeof(SAFEARRAYBOUND) * (num_dims - 1));
1407 if (!tdesc->u.lpadesc)
1408 return E_OUTOFMEMORY;
1410 hres = ctl2_decode_typedesc(This, arraydata[0], &tdesc->u.lpadesc->tdescElem);
1411 if (FAILED(hres)) {
1412 heap_free(tdesc->u.lpadesc);
1413 return E_OUTOFMEMORY;
1416 for (i = 0; i < num_dims; ++i) {
1417 tdesc->u.lpadesc->rgbounds[i].cElements = arraydata[2 + i * 2];
1418 tdesc->u.lpadesc->rgbounds[i].lLbound = arraydata[3 + i * 2];
1421 return S_OK;
1423 case VT_USERDEFINED:
1424 tdesc->u.hreftype = typedata[1];
1425 return S_OK;
1426 default:
1427 FIXME("unable to decode typedesc (%08x): unknown VT: %d\n", encoded_tdesc, tdesc->vt);
1428 return E_NOTIMPL;
1432 /****************************************************************************
1433 * ctl2_find_nth_reference
1435 * Finds a reference by index into the linked list of reference records.
1437 * RETURNS
1439 * Success: Offset of the desired reference record.
1440 * Failure: -1.
1442 static int ctl2_find_nth_reference(
1443 ICreateTypeLib2Impl *This, /* [I] The type library in which to search. */
1444 int offset, /* [I] The starting offset of the reference list. */
1445 int index) /* [I] The index of the reference to find. */
1447 MSFT_RefRecord *ref;
1449 for (; index && (offset != -1); index--) {
1450 ref = (MSFT_RefRecord *)&This->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1451 offset = ref->onext;
1454 return offset;
1457 /****************************************************************************
1458 * ctl2_find_typeinfo_from_offset
1460 * Finds an ITypeInfo given an offset into the TYPEINFO segment.
1462 * RETURNS
1464 * Success: S_OK.
1465 * Failure: TYPE_E_ELEMENTNOTFOUND.
1467 static HRESULT ctl2_find_typeinfo_from_offset(
1468 ICreateTypeLib2Impl *This, /* [I] The typelib to find the typeinfo in. */
1469 int offset, /* [I] The offset of the desired typeinfo. */
1470 ITypeInfo **ppTinfo) /* [I] The typeinfo found. */
1472 void *typeinfodata;
1473 ICreateTypeInfo2Impl *typeinfo;
1475 typeinfodata = &This->typelib_segment_data[MSFT_SEG_TYPEINFO][offset];
1477 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
1478 if (typeinfo->typeinfo == typeinfodata) {
1479 *ppTinfo = (ITypeInfo *)&typeinfo->lpVtblTypeInfo2;
1480 ITypeInfo2_AddRef(*ppTinfo);
1481 return S_OK;
1485 ERR("Failed to find typeinfo, invariant varied.\n");
1487 return TYPE_E_ELEMENTNOTFOUND;
1490 /****************************************************************************
1491 * funcrecord_reallochdr
1493 * Ensure FuncRecord data block contains header of required size
1495 * PARAMS
1497 * typedata [IO] - reference to pointer to data block
1498 * need [I] - required size of block in bytes
1500 * RETURNS
1502 * Number of additionally allocated bytes
1504 static INT funcrecord_reallochdr(INT **typedata, int need)
1506 int tail = (*typedata)[5]*((*typedata)[4]&0x1000?16:12);
1507 int hdr = (*typedata)[0] - tail;
1508 int i;
1510 if (hdr >= need)
1511 return 0;
1513 *typedata = heap_realloc(*typedata, need + tail);
1514 if (!*typedata)
1515 return -1;
1517 if (tail)
1518 memmove((char*)*typedata + need, (const char*)*typedata + hdr, tail);
1519 (*typedata)[0] = need + tail;
1521 /* fill in default values */
1522 for(i = (hdr+3)/4; (i+1)*4 <= need; i++)
1524 switch(i)
1526 case 2:
1527 (*typedata)[i] = 0;
1528 break;
1529 case 7:
1530 (*typedata)[i] = -1;
1531 break;
1532 case 8:
1533 (*typedata)[i] = -1;
1534 break;
1535 case 9:
1536 (*typedata)[i] = -1;
1537 break;
1538 case 10:
1539 (*typedata)[i] = -1;
1540 break;
1541 case 11:
1542 (*typedata)[i] = 0;
1543 break;
1544 case 12:
1545 (*typedata)[i] = -1;
1546 break;
1550 return need - hdr;
1553 /*================== ICreateTypeInfo2 Implementation ===================================*/
1555 /******************************************************************************
1556 * ICreateTypeInfo2_QueryInterface {OLEAUT32}
1559 static HRESULT WINAPI ICreateTypeInfo2_fnQueryInterface(
1560 ICreateTypeInfo2 * iface,
1561 REFIID riid,
1562 VOID **ppvObject)
1564 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1566 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
1568 *ppvObject=NULL;
1569 if(IsEqualIID(riid, &IID_IUnknown) ||
1570 IsEqualIID(riid,&IID_ICreateTypeInfo)||
1571 IsEqualIID(riid,&IID_ICreateTypeInfo2))
1573 *ppvObject = This;
1574 } else if (IsEqualIID(riid, &IID_ITypeInfo) ||
1575 IsEqualIID(riid, &IID_ITypeInfo2)) {
1576 *ppvObject = &This->lpVtblTypeInfo2;
1579 if(*ppvObject)
1581 ICreateTypeInfo2_AddRef(iface);
1582 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
1583 return S_OK;
1585 TRACE("-- Interface: E_NOINTERFACE\n");
1586 return E_NOINTERFACE;
1589 /******************************************************************************
1590 * ICreateTypeInfo2_AddRef {OLEAUT32}
1592 static ULONG WINAPI ICreateTypeInfo2_fnAddRef(ICreateTypeInfo2 *iface)
1594 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1595 ULONG ref = InterlockedIncrement(&This->ref);
1597 TRACE("(%p)->ref was %u\n",This, ref - 1);
1599 if(ref==1 && This->typelib)
1600 ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This->typelib);
1602 return ref;
1605 /******************************************************************************
1606 * ICreateTypeInfo2_Release {OLEAUT32}
1608 static ULONG WINAPI ICreateTypeInfo2_fnRelease(ICreateTypeInfo2 *iface)
1610 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1611 ULONG ref = InterlockedDecrement(&This->ref);
1613 TRACE("(%p)->(%u)\n",This, ref);
1615 if (!ref) {
1616 if (This->typelib) {
1617 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)This->typelib);
1618 /* Keep This->typelib reference to make stored ICreateTypeInfo structure valid */
1619 /* This->typelib = NULL; */
1622 /* ICreateTypeLib2 frees all ICreateTypeInfos when it releases. */
1623 /* HeapFree(GetProcessHeap(),0,This); */
1624 return 0;
1627 return ref;
1631 /******************************************************************************
1632 * ICreateTypeInfo2_SetGuid {OLEAUT32}
1634 static HRESULT WINAPI ICreateTypeInfo2_fnSetGuid(ICreateTypeInfo2 *iface, REFGUID guid)
1636 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1638 MSFT_GuidEntry guidentry;
1639 int offset;
1641 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
1643 guidentry.guid = *guid;
1644 guidentry.hreftype = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1645 guidentry.next_hash = -1;
1647 offset = ctl2_alloc_guid(This->typelib, &guidentry);
1649 if (offset == -1) return E_OUTOFMEMORY;
1651 This->typeinfo->posguid = offset;
1653 if (IsEqualIID(guid, &IID_IDispatch)) {
1654 This->typelib->typelib_header.dispatchpos = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1657 return S_OK;
1660 /******************************************************************************
1661 * ICreateTypeInfo2_SetTypeFlags {OLEAUT32}
1663 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeFlags(ICreateTypeInfo2 *iface, UINT uTypeFlags)
1665 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1667 TRACE("(%p,0x%x)\n", iface, uTypeFlags);
1669 if(uTypeFlags & TYPEFLAG_FDUAL) {
1670 This->typeinfo->typekind |= 0x10;
1671 This->typeinfo->typekind &= ~0x0f;
1672 This->typeinfo->typekind |= TKIND_DISPATCH;
1674 if(!This->dual) {
1675 This->dual = heap_alloc(sizeof(ICreateTypeInfo2Impl));
1676 if(!This->dual)
1677 return E_OUTOFMEMORY;
1679 memcpy(This->dual, This, sizeof(ICreateTypeInfo2Impl));
1680 This->dual->ref = 0;
1681 This->dual->typekind = This->typekind==TKIND_DISPATCH ?
1682 TKIND_INTERFACE : TKIND_DISPATCH;
1683 This->dual->dual = This;
1686 /* Make sure dispatch is in typeinfos queue */
1687 if(This->typekind != TKIND_DISPATCH) {
1688 if(This->typelib->last_typeinfo == This)
1689 This->typelib->last_typeinfo = This->dual;
1691 if(This->typelib->typeinfos == This)
1692 This->typelib->typeinfos = This->dual;
1693 else {
1694 ICreateTypeInfo2Impl *iter;
1696 for(iter=This->typelib->typeinfos; iter->next_typeinfo!=This; iter=iter->next_typeinfo);
1697 iter->next_typeinfo = This->dual;
1699 } else
1700 iface = (ICreateTypeInfo2*)&This->dual->lpVtbl;
1703 if (uTypeFlags & (TYPEFLAG_FDISPATCHABLE|TYPEFLAG_FDUAL)) {
1704 static const WCHAR stdole2tlb[] = { 's','t','d','o','l','e','2','.','t','l','b',0 };
1705 ITypeLib *stdole;
1706 ITypeInfo *dispatch;
1707 HREFTYPE hreftype;
1708 HRESULT hres;
1710 hres = LoadTypeLib(stdole2tlb, &stdole);
1711 if(FAILED(hres))
1712 return hres;
1714 hres = ITypeLib_GetTypeInfoOfGuid(stdole, &IID_IDispatch, &dispatch);
1715 ITypeLib_Release(stdole);
1716 if(FAILED(hres))
1717 return hres;
1719 hres = ICreateTypeInfo2_AddRefTypeInfo(iface, dispatch, &hreftype);
1720 ITypeInfo_Release(dispatch);
1721 if(FAILED(hres))
1722 return hres;
1725 This->typeinfo->flags = uTypeFlags;
1726 return S_OK;
1729 /******************************************************************************
1730 * ICreateTypeInfo2_SetDocString {OLEAUT32}
1732 static HRESULT WINAPI ICreateTypeInfo2_fnSetDocString(
1733 ICreateTypeInfo2* iface,
1734 LPOLESTR pStrDoc)
1736 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1738 int offset;
1740 TRACE("(%p,%s)\n", iface, debugstr_w(pStrDoc));
1741 if (!pStrDoc)
1742 return E_INVALIDARG;
1744 offset = ctl2_alloc_string(This->typelib, pStrDoc);
1745 if (offset == -1) return E_OUTOFMEMORY;
1746 This->typeinfo->docstringoffs = offset;
1747 return S_OK;
1750 /******************************************************************************
1751 * ICreateTypeInfo2_SetHelpContext {OLEAUT32}
1753 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpContext(
1754 ICreateTypeInfo2* iface,
1755 DWORD dwHelpContext)
1757 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1759 TRACE("(%p,%d)\n", iface, dwHelpContext);
1761 This->typeinfo->helpcontext = dwHelpContext;
1763 return S_OK;
1766 /******************************************************************************
1767 * ICreateTypeInfo2_SetVersion {OLEAUT32}
1769 static HRESULT WINAPI ICreateTypeInfo2_fnSetVersion(
1770 ICreateTypeInfo2* iface,
1771 WORD wMajorVerNum,
1772 WORD wMinorVerNum)
1774 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1776 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
1778 This->typeinfo->version = wMajorVerNum | (wMinorVerNum << 16);
1779 return S_OK;
1782 /******************************************************************************
1783 * ICreateTypeInfo2_AddRefTypeInfo {OLEAUT32}
1785 static HRESULT WINAPI ICreateTypeInfo2_fnAddRefTypeInfo(
1786 ICreateTypeInfo2* iface,
1787 ITypeInfo* pTInfo,
1788 HREFTYPE* phRefType)
1790 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1792 ITypeLib *container;
1793 UINT index;
1794 HRESULT res;
1796 TRACE("(%p,%p,%p)\n", iface, pTInfo, phRefType);
1798 if(!pTInfo || !phRefType)
1799 return E_INVALIDARG;
1802 * Unfortunately, we can't rely on the passed-in TypeInfo even having the
1803 * same internal structure as one of ours. It could be from another
1804 * implementation of ITypeInfo. So we need to do the following...
1806 res = ITypeInfo_GetContainingTypeLib(pTInfo, &container, &index);
1807 if (FAILED(res)) {
1808 TRACE("failed to find containing typelib.\n");
1809 return res;
1812 if (container == (ITypeLib *)&This->typelib->lpVtblTypeLib2) {
1813 /* Process locally defined TypeInfo */
1814 *phRefType = This->typelib->typelib_typeinfo_offsets[index];
1815 } else {
1816 BSTR name;
1817 TLIBATTR *tlibattr;
1818 TYPEATTR *typeattr;
1819 TYPEKIND typekind;
1820 MSFT_GuidEntry guid, *check_guid;
1821 MSFT_ImpInfo impinfo;
1822 int guid_offset, import_offset;
1823 HRESULT hres;
1825 /* Allocate container GUID */
1826 hres = ITypeLib_GetLibAttr(container, &tlibattr);
1827 if(FAILED(hres)) {
1828 ITypeLib_Release(container);
1829 return hres;
1832 guid.guid = tlibattr->guid;
1833 guid.hreftype = This->typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length+2;
1834 guid.next_hash = -1;
1836 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1837 if(guid_offset == -1) {
1838 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1839 ITypeLib_Release(container);
1840 return E_OUTOFMEMORY;
1843 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1844 if(check_guid->hreftype == guid.hreftype)
1845 This->typelib->typelib_guids++;
1847 /* Get import file name */
1848 hres = QueryPathOfRegTypeLib(&guid.guid, tlibattr->wMajorVerNum,
1849 tlibattr->wMinorVerNum, tlibattr->lcid, &name);
1850 if(FAILED(hres)) {
1851 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1852 ITypeLib_Release(container);
1853 return hres;
1856 /* Import file */
1857 import_offset = ctl2_alloc_importfile(This->typelib, guid_offset, tlibattr->lcid,
1858 tlibattr->wMajorVerNum, tlibattr->wMinorVerNum, strrchrW(name, '\\')+1);
1859 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1860 SysFreeString(name);
1862 if(import_offset == -1) {
1863 ITypeLib_Release(container);
1864 return E_OUTOFMEMORY;
1867 /* Allocate referenced guid */
1868 hres = ITypeInfo_GetTypeAttr(pTInfo, &typeattr);
1869 if(FAILED(hres)) {
1870 ITypeLib_Release(container);
1871 return hres;
1874 guid.guid = typeattr->guid;
1875 guid.hreftype = This->typelib->typeinfo_guids*12+1;
1876 guid.next_hash = -1;
1877 typekind = typeattr->typekind;
1878 ITypeInfo_ReleaseTypeAttr(pTInfo, typeattr);
1880 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1881 if(guid_offset == -1) {
1882 ITypeLib_Release(container);
1883 return E_OUTOFMEMORY;
1886 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1887 if(check_guid->hreftype == guid.hreftype)
1888 This->typelib->typeinfo_guids++;
1890 /* Allocate importinfo */
1891 impinfo.flags = (typekind<<24) | MSFT_IMPINFO_OFFSET_IS_GUID;
1892 impinfo.oImpFile = import_offset;
1893 impinfo.oGuid = guid_offset;
1894 *phRefType = ctl2_alloc_importinfo(This->typelib, &impinfo)+1;
1896 if(IsEqualGUID(&guid.guid, &IID_IDispatch))
1897 This->typelib->typelib_header.dispatchpos = *phRefType;
1900 ITypeLib_Release(container);
1901 return S_OK;
1904 /******************************************************************************
1905 * ICreateTypeInfo2_AddFuncDesc {OLEAUT32}
1907 static HRESULT WINAPI ICreateTypeInfo2_fnAddFuncDesc(
1908 ICreateTypeInfo2* iface,
1909 UINT index,
1910 FUNCDESC* pFuncDesc)
1912 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1914 CyclicList *iter, *insert;
1915 int *typedata;
1916 int i, num_defaults = 0, num_retval = 0;
1917 int decoded_size;
1918 HRESULT hres;
1920 TRACE("(%p,%d,%p)\n", iface, index, pFuncDesc);
1922 if(!pFuncDesc || pFuncDesc->oVft&3)
1923 return E_INVALIDARG;
1925 TRACE("{%d,%p,%p,%d,%d,%d,%d,%d,%d,%d,{%d},%d}\n", pFuncDesc->memid,
1926 pFuncDesc->lprgscode, pFuncDesc->lprgelemdescParam, pFuncDesc->funckind,
1927 pFuncDesc->invkind, pFuncDesc->callconv, pFuncDesc->cParams,
1928 pFuncDesc->cParamsOpt, pFuncDesc->oVft, pFuncDesc->cScodes,
1929 pFuncDesc->elemdescFunc.tdesc.vt, pFuncDesc->wFuncFlags);
1931 if(pFuncDesc->cParamsOpt || pFuncDesc->cScodes)
1932 FIXME("Unimplemented parameter - created typelib will be incorrect\n");
1934 switch(This->typekind) {
1935 case TKIND_MODULE:
1936 if(pFuncDesc->funckind != FUNC_STATIC)
1937 return TYPE_E_BADMODULEKIND;
1938 break;
1939 case TKIND_DISPATCH:
1940 if(pFuncDesc->funckind != FUNC_DISPATCH)
1941 return TYPE_E_BADMODULEKIND;
1942 break;
1943 default:
1944 if(pFuncDesc->funckind != FUNC_PUREVIRTUAL)
1945 return TYPE_E_BADMODULEKIND;
1948 if(cti2_get_func_count(This->typeinfo) < index)
1949 return TYPE_E_ELEMENTNOTFOUND;
1951 if((pFuncDesc->invkind&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) &&
1952 !pFuncDesc->cParams)
1953 return TYPE_E_INCONSISTENTPROPFUNCS;
1955 /* get number of arguments with default values specified */
1956 for (i = 0; i < pFuncDesc->cParams; i++) {
1957 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT)
1958 num_defaults++;
1959 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FRETVAL)
1960 num_retval++;
1963 if (!This->typedata) {
1964 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
1965 if(!This->typedata)
1966 return E_OUTOFMEMORY;
1968 This->typedata->next = This->typedata;
1970 if(This->dual)
1971 This->dual->typedata = This->typedata;
1974 /* allocate type data space for us */
1975 insert = alloc_cyclic_list_item(CyclicListFunc);
1976 if(!insert)
1977 return E_OUTOFMEMORY;
1978 insert->u.data = heap_alloc(FIELD_OFFSET(MSFT_FuncRecord, HelpContext) +
1979 sizeof(int[(num_defaults?4:3)])*pFuncDesc->cParams);
1980 if(!insert->u.data) {
1981 heap_free(insert);
1982 return E_OUTOFMEMORY;
1985 /* fill out the basic type information */
1986 typedata = insert->u.data;
1987 typedata[0] = FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
1988 ctl2_encode_typedesc(This->typelib, &pFuncDesc->elemdescFunc.tdesc, &typedata[1], NULL, NULL, &decoded_size);
1989 typedata[2] = pFuncDesc->wFuncFlags;
1990 typedata[3] = ((sizeof(FUNCDESC) + decoded_size) << 16) | (unsigned short)(pFuncDesc->oVft?pFuncDesc->oVft+1:0);
1991 typedata[4] = (pFuncDesc->callconv << 8) | (pFuncDesc->invkind << 3) | pFuncDesc->funckind;
1992 if(num_defaults) typedata[4] |= 0x1000;
1993 if (num_retval) typedata[4] |= 0x4000;
1994 typedata[5] = pFuncDesc->cParams;
1996 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1997 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1998 typedata[3] += (sizeof(ELEMDESC) * pFuncDesc->cParams) << 16;
1999 typedata[3] += (sizeof(PARAMDESCEX) * num_defaults) << 16;
2001 /* add default values */
2002 if(num_defaults) {
2003 for (i = 0; i < pFuncDesc->cParams; i++)
2004 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
2005 hres = ctl2_encode_variant(This->typelib, typedata+6+i,
2006 &pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue,
2007 pFuncDesc->lprgelemdescParam[i].tdesc.vt);
2009 if(FAILED(hres)) {
2010 heap_free(insert->u.data);
2011 heap_free(insert);
2012 return hres;
2014 } else
2015 typedata[6+i] = 0xffffffff;
2017 num_defaults = pFuncDesc->cParams;
2020 /* add arguments */
2021 for (i = 0; i < pFuncDesc->cParams; i++) {
2022 ctl2_encode_typedesc(This->typelib, &pFuncDesc->lprgelemdescParam[i].tdesc,
2023 &typedata[6+num_defaults+(i*3)], NULL, NULL, &decoded_size);
2024 typedata[7+num_defaults+(i*3)] = -1;
2025 typedata[8+num_defaults+(i*3)] = pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags;
2026 typedata[3] += decoded_size << 16;
2029 /* update the index data */
2030 insert->indice = pFuncDesc->memid;
2031 insert->name = -1;
2033 /* insert type data to list */
2034 if(index == cti2_get_func_count(This->typeinfo)) {
2035 insert->next = This->typedata->next;
2036 This->typedata->next = insert;
2037 This->typedata = insert;
2039 if(This->dual)
2040 This->dual->typedata = This->typedata;
2041 } else {
2042 iter = This->typedata->next;
2043 for(i=0; i<index; i++)
2044 iter = iter->next;
2046 insert->next = iter->next;
2047 iter->next = insert;
2050 /* update type data size */
2051 This->typedata->next->u.val += FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
2053 /* Increment the number of function elements */
2054 This->typeinfo->cElement += 1;
2056 return S_OK;
2059 /******************************************************************************
2060 * ICreateTypeInfo2_AddImplType {OLEAUT32}
2062 static HRESULT WINAPI ICreateTypeInfo2_fnAddImplType(
2063 ICreateTypeInfo2* iface,
2064 UINT index,
2065 HREFTYPE hRefType)
2067 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2069 TRACE("(%p,%d,%d)\n", iface, index, hRefType);
2071 if (This->typekind == TKIND_COCLASS) {
2072 int offset;
2073 MSFT_RefRecord *ref;
2075 if (index == 0) {
2076 if (This->typeinfo->datatype1 != -1) return TYPE_E_ELEMENTNOTFOUND;
2078 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2079 if (offset == -1) return E_OUTOFMEMORY;
2081 This->typeinfo->datatype1 = offset;
2082 } else {
2083 int lastoffset;
2085 lastoffset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index - 1);
2086 if (lastoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
2088 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][lastoffset];
2089 if (ref->onext != -1) return TYPE_E_ELEMENTNOTFOUND;
2091 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2092 if (offset == -1) return E_OUTOFMEMORY;
2094 ref->onext = offset;
2097 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2099 ref->reftype = hRefType;
2100 ref->flags = 0;
2101 ref->oCustData = -1;
2102 ref->onext = -1;
2103 This->typeinfo->cImplTypes++;
2104 } else if (This->typekind == TKIND_INTERFACE) {
2105 if (This->typeinfo->cImplTypes && index==1)
2106 return TYPE_E_BADMODULEKIND;
2108 if( index != 0) return TYPE_E_ELEMENTNOTFOUND;
2110 This->typeinfo->datatype1 = hRefType;
2111 This->typeinfo->cImplTypes = 1;
2112 } else if (This->typekind == TKIND_DISPATCH) {
2113 if(index != 0) return TYPE_E_ELEMENTNOTFOUND;
2115 /* FIXME: Check if referenced typeinfo is IDispatch */
2116 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2117 This->typeinfo->cImplTypes = 1;
2118 } else {
2119 FIXME("AddImplType unsupported on typekind %d\n", This->typekind);
2120 return E_OUTOFMEMORY;
2123 return S_OK;
2126 /******************************************************************************
2127 * ICreateTypeInfo2_SetImplTypeFlags {OLEAUT32}
2129 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeFlags(
2130 ICreateTypeInfo2* iface,
2131 UINT index,
2132 INT implTypeFlags)
2134 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2135 int offset;
2136 MSFT_RefRecord *ref;
2138 TRACE("(%p,%d,0x%x)\n", iface, index, implTypeFlags);
2140 if (This->typekind != TKIND_COCLASS) {
2141 return TYPE_E_BADMODULEKIND;
2144 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
2145 if (offset == -1) return TYPE_E_ELEMENTNOTFOUND;
2147 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2148 ref->flags = implTypeFlags;
2150 return S_OK;
2153 /******************************************************************************
2154 * ICreateTypeInfo2_SetAlignment {OLEAUT32}
2156 static HRESULT WINAPI ICreateTypeInfo2_fnSetAlignment(
2157 ICreateTypeInfo2* iface,
2158 WORD cbAlignment)
2160 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2162 TRACE("(%p,%d)\n", iface, cbAlignment);
2164 if (!cbAlignment) return E_INVALIDARG;
2165 if (cbAlignment > 16) return E_INVALIDARG;
2167 This->typeinfo->typekind &= ~0xffc0;
2168 This->typeinfo->typekind |= cbAlignment << 6;
2170 /* FIXME: There's probably some way to simplify this. */
2171 switch (This->typekind) {
2172 case TKIND_ALIAS:
2173 default:
2174 break;
2176 case TKIND_ENUM:
2177 case TKIND_INTERFACE:
2178 case TKIND_DISPATCH:
2179 case TKIND_COCLASS:
2180 if (cbAlignment > 4) cbAlignment = 4;
2181 break;
2183 case TKIND_RECORD:
2184 case TKIND_MODULE:
2185 case TKIND_UNION:
2186 cbAlignment = 1;
2187 break;
2190 This->typeinfo->typekind |= cbAlignment << 11;
2192 return S_OK;
2195 /******************************************************************************
2196 * ICreateTypeInfo2_SetSchema {OLEAUT32}
2198 static HRESULT WINAPI ICreateTypeInfo2_fnSetSchema(
2199 ICreateTypeInfo2* iface,
2200 LPOLESTR pStrSchema)
2202 FIXME("(%p,%s), stub!\n", iface, debugstr_w(pStrSchema));
2203 return E_OUTOFMEMORY;
2206 /******************************************************************************
2207 * ICreateTypeInfo2_AddVarDesc {OLEAUT32}
2209 static HRESULT WINAPI ICreateTypeInfo2_fnAddVarDesc(
2210 ICreateTypeInfo2* iface,
2211 UINT index,
2212 VARDESC* pVarDesc)
2214 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2216 HRESULT status = S_OK;
2217 CyclicList *insert;
2218 INT *typedata;
2219 int var_datawidth;
2220 int var_alignment;
2221 int var_type_size;
2222 int alignment;
2224 TRACE("(%p,%d,%p)\n", iface, index, pVarDesc);
2225 TRACE("%d, %p, %d, {{%x, %d}, {%p, %x}}, 0x%x, %d\n", pVarDesc->memid, pVarDesc->lpstrSchema, pVarDesc->u.oInst,
2226 pVarDesc->elemdescVar.tdesc.u.hreftype, pVarDesc->elemdescVar.tdesc.vt,
2227 pVarDesc->elemdescVar.u.paramdesc.pparamdescex, pVarDesc->elemdescVar.u.paramdesc.wParamFlags,
2228 pVarDesc->wVarFlags, pVarDesc->varkind);
2230 if (cti2_get_var_count(This->typeinfo) != index)
2231 return TYPE_E_ELEMENTNOTFOUND;
2233 if (!This->typedata) {
2234 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
2235 if(!This->typedata)
2236 return E_OUTOFMEMORY;
2238 This->typedata->next = This->typedata;
2240 if(This->dual)
2241 This->dual->typedata = This->typedata;
2244 insert = alloc_cyclic_list_item(CyclicListVar);
2245 if(!insert)
2246 return E_OUTOFMEMORY;
2248 /* allocate whole structure, it's fixed size always */
2249 insert->u.data = heap_alloc(sizeof(MSFT_VarRecord));
2250 if(!insert->u.data) {
2251 heap_free(insert);
2252 return E_OUTOFMEMORY;
2255 insert->next = This->typedata->next;
2256 This->typedata->next = insert;
2257 This->typedata = insert;
2259 if(This->dual)
2260 This->dual->typedata = This->typedata;
2262 This->typedata->next->u.val += FIELD_OFFSET(MSFT_VarRecord, HelpContext);
2263 typedata = This->typedata->u.data;
2265 /* fill out the basic type information */
2267 /* no optional fields initially */
2268 typedata[0] = FIELD_OFFSET(MSFT_VarRecord, HelpContext) | (index << 16);
2269 typedata[2] = pVarDesc->wVarFlags;
2270 typedata[3] = (sizeof(VARDESC) << 16) | pVarDesc->varkind;
2272 /* update the index data */
2273 insert->indice = 0x40000000 + index;
2274 insert->name = -1;
2276 /* figure out type widths and whatnot */
2277 ctl2_encode_typedesc(This->typelib, &pVarDesc->elemdescVar.tdesc,
2278 &typedata[1], &var_datawidth, &var_alignment,
2279 &var_type_size);
2281 if (pVarDesc->varkind != VAR_CONST)
2283 /* pad out starting position to data width */
2284 This->datawidth += var_alignment - 1;
2285 This->datawidth &= ~(var_alignment - 1);
2286 typedata[4] = This->datawidth;
2288 /* add the new variable to the total data width */
2289 This->datawidth += var_datawidth;
2290 if(This->dual)
2291 This->dual->datawidth = This->datawidth;
2293 /* add type description size to total required allocation */
2294 typedata[3] += var_type_size << 16;
2296 /* fix type alignment */
2297 alignment = (This->typeinfo->typekind >> 11) & 0x1f;
2298 if (alignment < var_alignment) {
2299 alignment = var_alignment;
2300 This->typeinfo->typekind &= ~0xf800;
2301 This->typeinfo->typekind |= alignment << 11;
2304 /* ??? */
2305 if (!This->typeinfo->res2) This->typeinfo->res2 = 0x1a;
2306 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
2307 This->typeinfo->res2 <<= 1;
2310 /* ??? */
2311 if (This->typeinfo->res3 == -1) This->typeinfo->res3 = 0;
2312 This->typeinfo->res3 += 0x2c;
2314 /* pad data width to alignment */
2315 This->typeinfo->size = (This->datawidth + (alignment - 1)) & ~(alignment - 1);
2316 } else {
2317 VARIANT *value = pVarDesc->DUMMYUNIONNAME.lpvarValue;
2318 status = ctl2_encode_variant(This->typelib, typedata+4, value, V_VT(value));
2319 /* ??? native sets size 0x34 */
2320 typedata[3] += 0x10 << 16;
2323 /* increment the number of variable elements */
2324 This->typeinfo->cElement += 0x10000;
2326 return status;
2329 /******************************************************************************
2330 * ICreateTypeInfo2_SetFuncAndParamNames {OLEAUT32}
2332 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncAndParamNames(
2333 ICreateTypeInfo2* iface,
2334 UINT index,
2335 LPOLESTR* names,
2336 UINT cNames)
2338 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2339 CyclicList *iter, *iter2;
2340 int offset, len, i;
2341 unsigned char *namedata;
2343 TRACE("(%p %d %p %d)\n", This, index, names, cNames);
2345 if(!names)
2346 return E_INVALIDARG;
2348 if(index >= cti2_get_func_count(This->typeinfo) || cNames == 0)
2349 return TYPE_E_ELEMENTNOTFOUND;
2351 for(iter=This->typedata->next->next, i=0; /* empty */; iter=iter->next)
2352 if (iter->type == CyclicListFunc)
2353 if (i++ >= index)
2354 break;
2356 /* cNames == cParams for put or putref accessor, cParams+1 otherwise */
2357 if(cNames != iter->u.data[5] + (ctl2_get_invokekind(iter) & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF) ? 0 : 1))
2358 return TYPE_E_ELEMENTNOTFOUND;
2360 TRACE("function name %s\n", debugstr_w(names[0]));
2361 len = ctl2_encode_name(This->typelib, names[0], (char**)&namedata);
2362 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2364 int cmp = memcmp(namedata, This->typelib->typelib_segment_data[MSFT_SEG_NAME]+iter2->name+8, len);
2365 if (iter2->name != -1 && cmp == 0) {
2366 if (iter2->type == CyclicListFunc) {
2367 INVOKEKIND inv1 = ctl2_get_invokekind(iter);
2368 INVOKEKIND inv2 = ctl2_get_invokekind(iter2);
2370 /* it's allowed to have PUT, PUTREF and GET methods with the same name */
2371 if ((inv1 != inv2) &&
2372 (inv1 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)) &&
2373 (inv2 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)))
2374 continue;
2377 return TYPE_E_AMBIGUOUSNAME;
2381 offset = ctl2_alloc_name(This->typelib, names[0]);
2382 if(offset == -1)
2383 return E_OUTOFMEMORY;
2385 iter->name = offset;
2387 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2388 if (*((INT*)namedata) == -1)
2389 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2391 len = ctl2_get_record_size(iter)/4 - iter->u.data[5]*3;
2393 for (i = 1; i < cNames; i++) {
2394 offset = ctl2_alloc_name(This->typelib, names[i]);
2395 iter->u.data[len + ((i-1)*3) + 1] = offset;
2398 return S_OK;
2401 /******************************************************************************
2402 * ICreateTypeInfo2_SetVarName {OLEAUT32}
2404 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarName(
2405 ICreateTypeInfo2* iface,
2406 UINT index,
2407 LPOLESTR szName)
2409 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2410 CyclicList *iter;
2411 int offset, i;
2412 unsigned char *namedata;
2414 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(szName));
2416 if (cti2_get_var_count(This->typeinfo) <= index)
2417 return TYPE_E_ELEMENTNOTFOUND;
2419 offset = ctl2_alloc_name(This->typelib, szName);
2420 if (offset == -1) return E_OUTOFMEMORY;
2422 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2423 if (*((INT *)namedata) == -1) {
2424 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2425 namedata[9] |= 0x10;
2427 if (This->typekind == TKIND_ENUM) {
2428 namedata[9] |= 0x20;
2431 for(iter = This->typedata->next->next, i = 0; /* empty */; iter = iter->next)
2432 if (iter->type == CyclicListVar)
2433 if (i++ >= index)
2434 break;
2436 iter->name = offset;
2437 return S_OK;
2440 /******************************************************************************
2441 * ICreateTypeInfo2_SetTypeDescAlias {OLEAUT32}
2443 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeDescAlias(
2444 ICreateTypeInfo2* iface,
2445 TYPEDESC* pTDescAlias)
2447 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2449 int encoded_typedesc;
2450 int width;
2452 if (This->typekind != TKIND_ALIAS) {
2453 return TYPE_E_WRONGTYPEKIND;
2456 FIXME("(%p,%p), hack!\n", iface, pTDescAlias);
2458 if (ctl2_encode_typedesc(This->typelib, pTDescAlias, &encoded_typedesc, &width, NULL, NULL) == -1) {
2459 return E_OUTOFMEMORY;
2462 This->typeinfo->size = width;
2463 This->typeinfo->datatype1 = encoded_typedesc;
2465 return S_OK;
2468 /******************************************************************************
2469 * ICreateTypeInfo2_DefineFuncAsDllEntry {OLEAUT32}
2471 static HRESULT WINAPI ICreateTypeInfo2_fnDefineFuncAsDllEntry(
2472 ICreateTypeInfo2* iface,
2473 UINT index,
2474 LPOLESTR szDllName,
2475 LPOLESTR szProcName)
2477 FIXME("(%p,%d,%s,%s), stub!\n", iface, index, debugstr_w(szDllName), debugstr_w(szProcName));
2478 return E_OUTOFMEMORY;
2481 /******************************************************************************
2482 * ICreateTypeInfo2_SetFuncDocString {OLEAUT32}
2484 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncDocString(
2485 ICreateTypeInfo2* iface,
2486 UINT index,
2487 LPOLESTR szDocString)
2489 FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2490 return E_OUTOFMEMORY;
2493 /******************************************************************************
2494 * ICreateTypeInfo2_SetVarDocString {OLEAUT32}
2496 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarDocString(
2497 ICreateTypeInfo2* iface,
2498 UINT index,
2499 LPOLESTR docstring)
2501 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2502 CyclicList *iter;
2504 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(docstring));
2506 if (!docstring) return E_INVALIDARG;
2508 if (cti2_get_var_count(This->typeinfo) <= index)
2509 return TYPE_E_ELEMENTNOTFOUND;
2511 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2512 if (iter->type == CyclicListVar)
2514 if (index-- == 0)
2516 int offset = ctl2_alloc_string(This->typelib, docstring);
2518 if (offset == -1) return E_OUTOFMEMORY;
2519 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, res9));
2520 iter->u.data[6] = offset;
2521 return S_OK;
2525 return TYPE_E_ELEMENTNOTFOUND;
2528 /******************************************************************************
2529 * ICreateTypeInfo2_SetFuncHelpContext {OLEAUT32}
2531 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpContext(
2532 ICreateTypeInfo2* iface,
2533 UINT index,
2534 DWORD dwHelpContext)
2536 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2537 CyclicList *func;
2539 TRACE("(%p,%d,%d)\n", iface, index, dwHelpContext);
2541 if(cti2_get_func_count(This->typeinfo) < index)
2542 return TYPE_E_ELEMENTNOTFOUND;
2544 if(cti2_get_func_count(This->typeinfo) == index && This->typedata->type == CyclicListFunc)
2545 func = This->typedata;
2546 else
2547 for(func=This->typedata->next->next; func!=This->typedata; func=func->next)
2548 if (func->type == CyclicListFunc)
2549 if(index-- == 0)
2550 break;
2552 This->typedata->next->u.val += funcrecord_reallochdr(&func->u.data, 7*sizeof(int));
2553 if(!func->u.data)
2554 return E_OUTOFMEMORY;
2556 func->u.data[6] = dwHelpContext;
2557 return S_OK;
2560 /******************************************************************************
2561 * ICreateTypeInfo2_SetVarHelpContext {OLEAUT32}
2563 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpContext(
2564 ICreateTypeInfo2* iface,
2565 UINT index,
2566 DWORD context)
2568 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2569 CyclicList *iter;
2571 TRACE("(%p,%d,%d)\n", This, index, context);
2573 if (cti2_get_var_count(This->typeinfo) <= index)
2574 return TYPE_E_ELEMENTNOTFOUND;
2576 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2577 if (iter->type == CyclicListVar)
2579 if (index-- == 0)
2581 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, HelpString));
2582 iter->u.data[5] = context;
2583 return S_OK;
2587 return TYPE_E_ELEMENTNOTFOUND;
2590 /******************************************************************************
2591 * ICreateTypeInfo2_SetMops {OLEAUT32}
2593 static HRESULT WINAPI ICreateTypeInfo2_fnSetMops(
2594 ICreateTypeInfo2* iface,
2595 UINT index,
2596 BSTR bstrMops)
2598 FIXME("(%p,%d,%p), stub!\n", iface, index, bstrMops);
2599 return E_OUTOFMEMORY;
2602 /******************************************************************************
2603 * ICreateTypeInfo2_SetTypeIdldesc {OLEAUT32}
2605 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeIdldesc(
2606 ICreateTypeInfo2* iface,
2607 IDLDESC* pIdlDesc)
2609 FIXME("(%p,%p), stub!\n", iface, pIdlDesc);
2610 return E_OUTOFMEMORY;
2613 /******************************************************************************
2614 * ICreateTypeInfo2_LayOut {OLEAUT32}
2616 static HRESULT WINAPI ICreateTypeInfo2_fnLayOut(
2617 ICreateTypeInfo2* iface)
2619 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2620 CyclicList *iter, *iter2, *last = NULL, **typedata;
2621 HREFTYPE hreftype;
2622 HRESULT hres;
2623 unsigned user_vft = 0;
2624 int i;
2626 TRACE("(%p)\n", This);
2628 /* FIXME: LayOut should be run on all ImplTypes */
2629 if(This->typekind == TKIND_COCLASS || This->typekind == TKIND_ALIAS)
2630 return S_OK;
2632 /* Validate inheritance */
2633 This->typeinfo->datatype2 = 0;
2634 hreftype = This->typeinfo->datatype1;
2636 /* Process internally defined interfaces */
2637 for(i=0; i<This->typelib->typelib_header.nrtypeinfos; i++) {
2638 MSFT_TypeInfoBase *header;
2640 if(hreftype&1)
2641 break;
2643 header = (MSFT_TypeInfoBase*)&(This->typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][hreftype]);
2644 This->typeinfo->datatype2 += (header->cElement<<16) + 1;
2645 hreftype = header->datatype1;
2647 if(i == This->typelib->typelib_header.nrtypeinfos)
2648 return TYPE_E_CIRCULARTYPE;
2650 /* Process externally defined interfaces */
2651 if(hreftype != -1) {
2652 ITypeInfo *cur, *next;
2653 TYPEATTR *typeattr;
2655 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&next);
2656 if(FAILED(hres))
2657 return hres;
2659 hres = ITypeInfo_GetRefTypeInfo(next, hreftype, &cur);
2660 ITypeInfo_Release(next);
2661 if(FAILED(hres))
2662 return hres;
2665 while(1) {
2666 hres = ITypeInfo_GetTypeAttr(cur, &typeattr);
2667 if(FAILED(hres)) {
2668 ITypeInfo_Release(cur);
2669 return hres;
2672 if(IsEqualGUID(&typeattr->guid, &IID_IDispatch))
2673 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2675 This->typeinfo->datatype2 += (typeattr->cFuncs<<16) + 1;
2676 ITypeInfo_ReleaseTypeAttr(cur, typeattr);
2678 hres = ITypeInfo_GetRefTypeOfImplType(cur, 0, &hreftype);
2679 if(hres == TYPE_E_ELEMENTNOTFOUND)
2680 break;
2681 if(FAILED(hres)) {
2682 ITypeInfo_Release(cur);
2683 return hres;
2686 hres = ITypeInfo_GetRefTypeInfo(cur, hreftype, &next);
2687 if(FAILED(hres)) {
2688 ITypeInfo_Release(cur);
2689 return hres;
2692 ITypeInfo_Release(cur);
2693 cur = next;
2695 ITypeInfo_Release(cur);
2698 /* Get cbSizeVft of inherited interface */
2699 /* Makes LayOut running recursively */
2700 if(This->typeinfo->datatype1 != -1) {
2701 ITypeInfo *cur, *inherited;
2702 TYPEATTR *typeattr;
2704 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&cur);
2705 if(FAILED(hres))
2706 return hres;
2708 hres = ITypeInfo_GetRefTypeInfo(cur, This->typeinfo->datatype1, &inherited);
2709 ITypeInfo_Release(cur);
2710 if(FAILED(hres))
2711 return hres;
2713 hres = ITypeInfo_GetTypeAttr(inherited, &typeattr);
2714 if(FAILED(hres)) {
2715 ITypeInfo_Release(inherited);
2716 return hres;
2719 This->typeinfo->cbSizeVft = typeattr->cbSizeVft * 4 / sizeof(void *);
2721 ITypeInfo_ReleaseTypeAttr(inherited, typeattr);
2722 ITypeInfo_Release(inherited);
2723 } else
2724 This->typeinfo->cbSizeVft = 0;
2726 if(!This->typedata)
2727 return S_OK;
2729 typedata = heap_alloc(sizeof(CyclicList*)*cti2_get_func_count(This->typeinfo));
2730 if(!typedata)
2731 return E_OUTOFMEMORY;
2733 /* Assign IDs and VTBL entries */
2734 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next)
2735 if (iter->type == CyclicListFunc)
2736 last = iter;
2738 if(last && last->u.data[3]&1)
2739 user_vft = last->u.data[3]&0xffff;
2741 i = 0;
2742 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
2743 /* Assign MEMBERID if MEMBERID_NIL was specified */
2744 if(iter->indice == MEMBERID_NIL) {
2745 iter->indice = 0x60000000 + i + (This->typeinfo->datatype2<<16);
2747 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2748 if(iter == iter2) continue;
2749 if(iter2->indice == iter->indice) {
2750 iter->indice = 0x60000000 + This->typeinfo->cElement + (This->typeinfo->datatype2<<16);
2752 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2753 if(iter == iter2) continue;
2754 if(iter2->indice == iter->indice) {
2755 ++iter->indice;
2756 iter2 = This->typedata->next;
2760 break;
2765 if (iter->type != CyclicListFunc)
2766 continue;
2768 typedata[i] = iter;
2770 iter->u.data[0] = ctl2_get_record_size(iter) | (i<<16);
2772 if((iter->u.data[3]&1) != (user_vft&1)) {
2773 heap_free(typedata);
2774 return TYPE_E_INVALIDID;
2777 if(user_vft&1) {
2778 if(user_vft < (iter->u.data[3]&0xffff))
2779 user_vft = (iter->u.data[3]&0xffff);
2781 if((iter->u.data[3]&0xffff) < This->typeinfo->cbSizeVft) {
2782 heap_free(typedata);
2783 return TYPE_E_INVALIDID;
2785 } else if(This->typekind != TKIND_MODULE) {
2786 iter->u.data[3] = (iter->u.data[3]&0xffff0000) | This->typeinfo->cbSizeVft;
2787 This->typeinfo->cbSizeVft += 4;
2790 /* Construct a list of elements with the same memberid */
2791 iter->u.data[4] = (iter->u.data[4]&0xffff) | (i<<16);
2792 for(iter2=This->typedata->next->next; iter2!=iter; iter2=iter2->next) {
2793 if(iter->indice == iter2->indice) {
2794 int v1, v2;
2796 v1 = iter->u.data[4] >> 16;
2797 v2 = iter2->u.data[4] >> 16;
2799 iter->u.data[4] = (iter->u.data[4]&0xffff) | (v2<<16);
2800 iter2->u.data[4] = (iter2->u.data[4]&0xffff) | (v1<<16);
2801 break;
2805 i++;
2808 if(user_vft)
2809 This->typeinfo->cbSizeVft = user_vft+3;
2811 for(i=0; i< cti2_get_func_count(This->typeinfo); i++) {
2812 if(typedata[i]->u.data[4]>>16 > i) {
2813 INVOKEKIND inv = ctl2_get_invokekind(typedata[i]);
2815 i = typedata[i]->u.data[4] >> 16;
2817 while(i > typedata[i]->u.data[4]>>16) {
2818 INVOKEKIND invkind = ctl2_get_invokekind(typedata[i]);
2820 if(inv & invkind) {
2821 heap_free(typedata);
2822 return TYPE_E_DUPLICATEID;
2825 i = typedata[i]->u.data[4] >> 16;
2826 inv |= invkind;
2829 if(inv & INVOKE_FUNC) {
2830 heap_free(typedata);
2831 return TYPE_E_INCONSISTENTPROPFUNCS;
2836 heap_free(typedata);
2837 return S_OK;
2840 /******************************************************************************
2841 * ICreateTypeInfo2_DeleteFuncDesc {OLEAUT32}
2843 * Delete a function description from a type.
2845 * RETURNS
2847 * Success: S_OK.
2848 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2850 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDesc(
2851 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2852 UINT index) /* [I] The index of the function to delete. */
2854 FIXME("(%p,%d), stub!\n", iface, index);
2855 return E_OUTOFMEMORY;
2858 /******************************************************************************
2859 * ICreateTypeInfo2_DeleteFuncDescByMemId {OLEAUT32}
2861 * Delete a function description from a type.
2863 * RETURNS
2865 * Success: S_OK.
2866 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2868 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDescByMemId(
2869 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2870 MEMBERID memid, /* [I] The member id of the function to delete. */
2871 INVOKEKIND invKind) /* [I] The invocation type of the function to delete. (?) */
2873 FIXME("(%p,%d,%d), stub!\n", iface, memid, invKind);
2874 return E_OUTOFMEMORY;
2877 /******************************************************************************
2878 * ICreateTypeInfo2_DeleteVarDesc {OLEAUT32}
2880 * Delete a variable description from a type.
2882 * RETURNS
2884 * Success: S_OK.
2885 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2886 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2888 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDesc(
2889 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2890 UINT index) /* [I] The index of the variable description to delete. */
2892 FIXME("(%p,%d), stub!\n", iface, index);
2893 return E_OUTOFMEMORY;
2896 /******************************************************************************
2897 * ICreateTypeInfo2_DeleteVarDescByMemId {OLEAUT32}
2899 * Delete a variable description from a type.
2901 * RETURNS
2903 * Success: S_OK.
2904 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2905 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2907 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDescByMemId(
2908 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2909 MEMBERID memid) /* [I] The member id of the variable description to delete. */
2911 FIXME("(%p,%d), stub!\n", iface, memid);
2912 return E_OUTOFMEMORY;
2915 /******************************************************************************
2916 * ICreateTypeInfo2_DeleteImplType {OLEAUT32}
2918 * Delete an interface implementation from a type. (?)
2920 * RETURNS
2922 * Success: S_OK.
2923 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2925 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteImplType(
2926 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete. */
2927 UINT index) /* [I] The index of the interface to delete. */
2929 FIXME("(%p,%d), stub!\n", iface, index);
2930 return E_OUTOFMEMORY;
2933 /******************************************************************************
2934 * ICreateTypeInfo2_SetCustData {OLEAUT32}
2936 * Set the custom data for a type.
2938 * RETURNS
2940 * Success: S_OK.
2941 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2943 static HRESULT WINAPI ICreateTypeInfo2_fnSetCustData(
2944 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2945 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2946 VARIANT* pVarVal) /* [I] The custom data. */
2948 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2950 TRACE("(%p,%s,%p)!\n", iface, debugstr_guid(guid), pVarVal);
2952 if (!pVarVal)
2953 return E_INVALIDARG;
2955 return ctl2_set_custdata(This->typelib, guid, pVarVal, &This->typeinfo->oCustData);
2958 /******************************************************************************
2959 * ICreateTypeInfo2_SetFuncCustData {OLEAUT32}
2961 * Set the custom data for a function.
2963 * RETURNS
2965 * Success: S_OK.
2966 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2968 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncCustData(
2969 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2970 UINT index, /* [I] The index of the function for which to set the custom data. */
2971 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2972 VARIANT* pVarVal) /* [I] The custom data. */
2974 ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2975 CyclicList *iter;
2977 TRACE("(%p,%d,%s,%p)\n", iface, index, debugstr_guid(guid), pVarVal);
2979 if(index >= cti2_get_func_count(This->typeinfo))
2980 return TYPE_E_ELEMENTNOTFOUND;
2982 for(iter=This->typedata->next->next; /* empty */; iter=iter->next)
2983 if (iter->type == CyclicListFunc)
2984 if (index-- == 0)
2985 break;
2987 This->typedata->next->u.val += funcrecord_reallochdr(&iter->u.data, 13*sizeof(int));
2988 if(!iter->u.data)
2989 return E_OUTOFMEMORY;
2991 iter->u.data[4] |= 0x80;
2992 return ctl2_set_custdata(This->typelib, guid, pVarVal, &iter->u.data[12]);
2995 /******************************************************************************
2996 * ICreateTypeInfo2_SetParamCustData {OLEAUT32}
2998 * Set the custom data for a function parameter.
3000 * RETURNS
3002 * Success: S_OK.
3003 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3005 static HRESULT WINAPI ICreateTypeInfo2_fnSetParamCustData(
3006 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3007 UINT indexFunc, /* [I] The index of the function on which the parameter resides. */
3008 UINT indexParam, /* [I] The index of the parameter on which to set the custom data. */
3009 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3010 VARIANT* pVarVal) /* [I] The custom data. */
3012 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3013 return E_OUTOFMEMORY;
3016 /******************************************************************************
3017 * ICreateTypeInfo2_SetVarCustData {OLEAUT32}
3019 * Set the custom data for a variable.
3021 * RETURNS
3023 * Success: S_OK.
3024 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3026 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarCustData(
3027 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3028 UINT index, /* [I] The index of the variable on which to set the custom data. */
3029 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3030 VARIANT* pVarVal) /* [I] The custom data. */
3032 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3033 return E_OUTOFMEMORY;
3036 /******************************************************************************
3037 * ICreateTypeInfo2_SetImplTypeCustData {OLEAUT32}
3039 * Set the custom data for an implemented interface.
3041 * RETURNS
3043 * Success: S_OK.
3044 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3046 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeCustData(
3047 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the custom data. */
3048 UINT index, /* [I] The index of the implemented interface on which to set the custom data. */
3049 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3050 VARIANT* pVarVal) /* [I] The custom data. */
3052 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3053 return E_OUTOFMEMORY;
3056 /******************************************************************************
3057 * ICreateTypeInfo2_SetHelpStringContext {OLEAUT32}
3059 * Set the help string context for the typeinfo.
3061 * RETURNS
3063 * Success: S_OK.
3064 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3066 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpStringContext(
3067 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3068 ULONG dwHelpStringContext) /* [I] The help string context. */
3070 FIXME("(%p,%d), stub!\n", iface, dwHelpStringContext);
3071 return E_OUTOFMEMORY;
3074 /******************************************************************************
3075 * ICreateTypeInfo2_SetFuncHelpStringContext {OLEAUT32}
3077 * Set the help string context for a function.
3079 * RETURNS
3081 * Success: S_OK.
3082 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3084 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpStringContext(
3085 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3086 UINT index, /* [I] The index for the function on which to set the help string context. */
3087 ULONG dwHelpStringContext) /* [I] The help string context. */
3089 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3090 return E_OUTOFMEMORY;
3093 /******************************************************************************
3094 * ICreateTypeInfo2_SetVarHelpStringContext {OLEAUT32}
3096 * Set the help string context for a variable.
3098 * RETURNS
3100 * Success: S_OK.
3101 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3103 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpStringContext(
3104 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3105 UINT index, /* [I] The index of the variable on which to set the help string context. */
3106 ULONG dwHelpStringContext) /* [I] The help string context */
3108 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3109 return E_OUTOFMEMORY;
3112 /******************************************************************************
3113 * ICreateTypeInfo2_Invalidate {OLEAUT32}
3115 * Undocumented function. (!)
3117 static HRESULT WINAPI ICreateTypeInfo2_fnInvalidate(
3118 ICreateTypeInfo2* iface)
3120 FIXME("(%p), stub!\n", iface);
3121 return E_OUTOFMEMORY;
3124 /******************************************************************************
3125 * ICreateTypeInfo2_SetName {OLEAUT32}
3127 * Set the name for a typeinfo.
3129 * RETURNS
3131 * Success: S_OK.
3132 * Failure: One of STG_E_INSUFFICIENTMEMORY, E_OUTOFMEMORY, E_INVALIDARG or TYPE_E_INVALIDSTATE.
3134 static HRESULT WINAPI ICreateTypeInfo2_fnSetName(
3135 ICreateTypeInfo2* iface,
3136 LPOLESTR szName)
3138 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
3139 return E_OUTOFMEMORY;
3142 /*================== ITypeInfo2 Implementation ===================================*/
3144 /******************************************************************************
3145 * ITypeInfo2_QueryInterface {OLEAUT32}
3147 static HRESULT WINAPI ITypeInfo2_fnQueryInterface(ITypeInfo2 * iface, REFIID riid, LPVOID * ppv)
3149 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3151 return ICreateTypeInfo2_QueryInterface((ICreateTypeInfo2 *)This, riid, ppv);
3154 /******************************************************************************
3155 * ITypeInfo2_AddRef {OLEAUT32}
3157 static ULONG WINAPI ITypeInfo2_fnAddRef(ITypeInfo2 * iface)
3159 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3161 return ICreateTypeInfo2_AddRef((ICreateTypeInfo2 *)This);
3164 /******************************************************************************
3165 * ITypeInfo2_Release {OLEAUT32}
3167 static ULONG WINAPI ITypeInfo2_fnRelease(ITypeInfo2 * iface)
3169 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3171 return ICreateTypeInfo2_Release((ICreateTypeInfo2 *)This);
3174 /******************************************************************************
3175 * ITypeInfo2_GetTypeAttr {OLEAUT32}
3177 static HRESULT WINAPI ITypeInfo2_fnGetTypeAttr(
3178 ITypeInfo2* iface,
3179 TYPEATTR** ppTypeAttr)
3181 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3182 HRESULT hres;
3184 TRACE("(%p,%p)\n", iface, ppTypeAttr);
3186 if(!ppTypeAttr)
3187 return E_INVALIDARG;
3189 hres = ICreateTypeInfo_LayOut((ICreateTypeInfo*)This);
3190 if(FAILED(hres))
3191 return hres;
3193 *ppTypeAttr = heap_alloc_zero(sizeof(TYPEATTR));
3194 if(!*ppTypeAttr)
3195 return E_OUTOFMEMORY;
3197 if(This->typeinfo->posguid != -1) {
3198 MSFT_GuidEntry *guid;
3200 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][This->typeinfo->posguid];
3201 (*ppTypeAttr)->guid = guid->guid;
3204 (*ppTypeAttr)->lcid = This->typelib->typelib_header.lcid;
3205 (*ppTypeAttr)->cbSizeInstance = This->typeinfo->size;
3206 (*ppTypeAttr)->typekind = This->typekind;
3207 (*ppTypeAttr)->cFuncs = cti2_get_func_count(This->typeinfo);
3208 if(This->typeinfo->flags&TYPEFLAG_FDUAL && This->typekind==TKIND_DISPATCH)
3209 (*ppTypeAttr)->cFuncs += sizeof(IDispatchVtbl)/sizeof(void*);
3210 (*ppTypeAttr)->cVars = cti2_get_var_count(This->typeinfo);
3211 (*ppTypeAttr)->cImplTypes = This->typeinfo->cImplTypes;
3212 (*ppTypeAttr)->cbSizeVft = This->typekind == TKIND_DISPATCH ? sizeof(IDispatchVtbl) : This->typeinfo->cbSizeVft;
3213 (*ppTypeAttr)->cbAlignment = (This->typeinfo->typekind>>11) & 0x1f;
3214 (*ppTypeAttr)->wTypeFlags = This->typeinfo->flags;
3215 (*ppTypeAttr)->wMajorVerNum = LOWORD(This->typeinfo->version);
3216 (*ppTypeAttr)->wMinorVerNum = HIWORD(This->typeinfo->version);
3218 if((*ppTypeAttr)->typekind == TKIND_ALIAS)
3219 FIXME("TKIND_ALIAS handling not implemented\n");
3221 return S_OK;
3224 /******************************************************************************
3225 * ITypeInfo2_GetTypeComp {OLEAUT32}
3227 static HRESULT WINAPI ITypeInfo2_fnGetTypeComp(
3228 ITypeInfo2* iface,
3229 ITypeComp** ppTComp)
3231 FIXME("(%p,%p), stub!\n", iface, ppTComp);
3232 return E_OUTOFMEMORY;
3235 /******************************************************************************
3236 * ITypeInfo2_GetFuncDesc {OLEAUT32}
3238 static HRESULT WINAPI ITypeInfo2_fnGetFuncDesc(
3239 ITypeInfo2* iface,
3240 UINT index,
3241 FUNCDESC** ppFuncDesc)
3243 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3244 int i, *typedata, num_defaults = 0, hdr_len, tail, has_defaults;
3245 CyclicList *desc;
3246 HRESULT hres;
3248 TRACE("(%p,%d,%p), semi-stub\n", iface, index, ppFuncDesc);
3250 if (!ppFuncDesc)
3251 return E_INVALIDARG;
3253 if (index >= cti2_get_func_count(This->typeinfo))
3254 return TYPE_E_ELEMENTNOTFOUND;
3256 hres = ICreateTypeInfo2_LayOut((ICreateTypeInfo2*)This);
3257 if (FAILED(hres))
3258 return hres;
3260 desc = This->typedata->next;
3261 for (i = index; i >= 0; ) {
3262 desc = desc->next;
3263 if (desc->type == CyclicListFunc)
3264 --i;
3267 typedata = desc->u.data;
3269 *ppFuncDesc = heap_alloc_zero(sizeof(FUNCDESC));
3270 if (!*ppFuncDesc)
3271 return E_OUTOFMEMORY;
3273 (*ppFuncDesc)->memid = desc->indice;
3274 (*ppFuncDesc)->lprgscode = NULL; /* FIXME: Unimplemented */
3275 (*ppFuncDesc)->funckind = typedata[4] & 0x7;
3276 (*ppFuncDesc)->invkind = (typedata[4] >> 3) & 0xF;
3277 (*ppFuncDesc)->callconv = (typedata[4] >> 8) & 0xF;
3278 (*ppFuncDesc)->cParams = typedata[5];
3279 (*ppFuncDesc)->cParamsOpt = 0; /* FIXME: Unimplemented*/
3280 (*ppFuncDesc)->oVft = typedata[3] & 0xFFFF;
3281 if ((*ppFuncDesc)->oVft)
3282 --(*ppFuncDesc)->oVft;
3283 (*ppFuncDesc)->cScodes = 0; /* FIXME: Unimplemented*/
3284 hres = ctl2_decode_typedesc(This->typelib, typedata[1],
3285 &(*ppFuncDesc)->elemdescFunc.tdesc);
3286 if (FAILED(hres)) {
3287 heap_free(*ppFuncDesc);
3288 return hres;
3290 (*ppFuncDesc)->wFuncFlags = typedata[2];
3292 has_defaults = typedata[4] & 0x1000;
3293 tail = typedata[5] * (has_defaults ? 16 : 12);
3294 hdr_len = (ctl2_get_record_size(desc) - tail) / sizeof(int);
3296 if ((*ppFuncDesc)->cParams > 0) {
3297 (*ppFuncDesc)->lprgelemdescParam = heap_alloc_zero((*ppFuncDesc)->cParams * sizeof(ELEMDESC));
3298 if (!(*ppFuncDesc)->lprgelemdescParam) {
3299 heap_free(*ppFuncDesc);
3300 return E_OUTOFMEMORY;
3302 if (has_defaults) {
3303 num_defaults = (*ppFuncDesc)->cParams;
3305 for (i = 0; i < num_defaults; ++i) {
3306 if (typedata[hdr_len + i] != 0xFFFFFFFF) {
3307 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags |= PARAMFLAG_FHASDEFAULT;
3309 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex = heap_alloc(sizeof(PARAMDESCEX));
3310 if (!(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex) {
3311 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3312 return E_OUTOFMEMORY;
3315 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->cBytes = sizeof(PARAMDESCEX);
3316 VariantInit(&(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3317 hres = ctl2_decode_variant(This->typelib, typedata[hdr_len + i],
3318 &(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3319 if (FAILED(hres)) {
3320 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3321 return hres;
3327 for (i = 0; i < (*ppFuncDesc)->cParams; ++i) {
3328 hres = ctl2_decode_typedesc(This->typelib, typedata[hdr_len + num_defaults + (i * 3)],
3329 &((*ppFuncDesc)->lprgelemdescParam + i)->tdesc);
3330 if (FAILED(hres)) {
3331 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3332 return hres;
3334 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags = typedata[hdr_len + num_defaults + (i * 3) + 2];
3338 return S_OK;
3341 /******************************************************************************
3342 * ITypeInfo2_GetVarDesc {OLEAUT32}
3344 static HRESULT WINAPI ITypeInfo2_fnGetVarDesc(
3345 ITypeInfo2* iface,
3346 UINT index,
3347 VARDESC** ppVarDesc)
3349 FIXME("(%p,%d,%p), stub!\n", iface, index, ppVarDesc);
3350 return E_OUTOFMEMORY;
3353 /******************************************************************************
3354 * ITypeInfo2_GetNames {OLEAUT32}
3356 static HRESULT WINAPI ITypeInfo2_fnGetNames(
3357 ITypeInfo2* iface,
3358 MEMBERID memid,
3359 BSTR* rgBstrNames,
3360 UINT cMaxNames,
3361 UINT* pcNames)
3363 FIXME("(%p,%d,%p,%d,%p), stub!\n", iface, memid, rgBstrNames, cMaxNames, pcNames);
3364 return E_OUTOFMEMORY;
3367 /******************************************************************************
3368 * ITypeInfo2_GetRefTypeOfImplType {OLEAUT32}
3370 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeOfImplType(
3371 ITypeInfo2* iface,
3372 UINT index,
3373 HREFTYPE* pRefType)
3375 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3376 MSFT_RefRecord *ref;
3377 int offset;
3379 TRACE("(%p,%d,%p)\n", iface, index, pRefType);
3381 if(!pRefType)
3382 return E_INVALIDARG;
3384 if(This->typeinfo->flags&TYPEFLAG_FDUAL) {
3385 if(index == -1) {
3386 *pRefType = -2;
3387 return S_OK;
3390 if(This->typekind == TKIND_DISPATCH)
3391 return ITypeInfo2_GetRefTypeOfImplType((ITypeInfo2*)&This->dual->lpVtblTypeInfo2,
3392 index, pRefType);
3395 if(index>=This->typeinfo->cImplTypes)
3396 return TYPE_E_ELEMENTNOTFOUND;
3398 if(This->typekind == TKIND_INTERFACE) {
3399 *pRefType = This->typeinfo->datatype1 + 2;
3400 return S_OK;
3403 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3404 if(offset == -1)
3405 return TYPE_E_ELEMENTNOTFOUND;
3407 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3408 *pRefType = ref->reftype;
3409 return S_OK;
3412 /******************************************************************************
3413 * ITypeInfo2_GetImplTypeFlags {OLEAUT32}
3415 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeFlags(
3416 ITypeInfo2* iface,
3417 UINT index,
3418 INT* pImplTypeFlags)
3420 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3421 int offset;
3422 MSFT_RefRecord *ref;
3424 TRACE("(%p,%d,%p)\n", iface, index, pImplTypeFlags);
3426 if(!pImplTypeFlags)
3427 return E_INVALIDARG;
3429 if(index >= This->typeinfo->cImplTypes)
3430 return TYPE_E_ELEMENTNOTFOUND;
3432 if(This->typekind != TKIND_COCLASS) {
3433 *pImplTypeFlags = 0;
3434 return S_OK;
3437 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3438 if(offset == -1)
3439 return TYPE_E_ELEMENTNOTFOUND;
3441 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3442 *pImplTypeFlags = ref->flags;
3443 return S_OK;
3446 /******************************************************************************
3447 * ITypeInfo2_GetIDsOfNames {OLEAUT32}
3449 static HRESULT WINAPI ITypeInfo2_fnGetIDsOfNames(
3450 ITypeInfo2* iface,
3451 LPOLESTR* rgszNames,
3452 UINT cNames,
3453 MEMBERID* pMemId)
3455 FIXME("(%p,%p,%d,%p), stub!\n", iface, rgszNames, cNames, pMemId);
3456 return E_OUTOFMEMORY;
3459 /******************************************************************************
3460 * ITypeInfo2_Invoke {OLEAUT32}
3462 static HRESULT WINAPI ITypeInfo2_fnInvoke(
3463 ITypeInfo2* iface,
3464 PVOID pvInstance,
3465 MEMBERID memid,
3466 WORD wFlags,
3467 DISPPARAMS* pDispParams,
3468 VARIANT* pVarResult,
3469 EXCEPINFO* pExcepInfo,
3470 UINT* puArgErr)
3472 FIXME("(%p,%p,%d,%x,%p,%p,%p,%p), stub!\n", iface, pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
3473 return E_OUTOFMEMORY;
3476 /******************************************************************************
3477 * ITypeInfo2_GetDocumentation {OLEAUT32}
3479 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation(
3480 ITypeInfo2* iface,
3481 MEMBERID memid,
3482 BSTR* pBstrName,
3483 BSTR* pBstrDocString,
3484 DWORD* pdwHelpContext,
3485 BSTR* pBstrHelpFile)
3487 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3488 HRESULT status = TYPE_E_ELEMENTNOTFOUND;
3489 INT nameoffset, docstringoffset, helpcontext;
3491 TRACE("(%p,%d,%p,%p,%p,%p)\n", iface, memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
3493 if (memid == -1)
3495 nameoffset = This->typeinfo->NameOffset;
3496 docstringoffset = This->typeinfo->docstringoffs;
3497 helpcontext = This->typeinfo->helpcontext;
3498 status = S_OK;
3499 } else {
3500 CyclicList *iter;
3501 if (This->typedata) {
3502 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
3503 if (iter->indice == memid) {
3504 if (iter->type == CyclicListFunc) {
3505 const int *typedata = iter->u.data;
3506 int size = ctl2_get_record_size(iter) - typedata[5]*(typedata[4]&0x1000?16:12);
3508 nameoffset = iter->name;
3509 /* FIXME implement this once SetFuncDocString is implemented */
3510 docstringoffset = -1;
3511 helpcontext = (size < 7*sizeof(int)) ? 0 : typedata[6];
3513 status = S_OK;
3514 } else {
3515 FIXME("Not implemented for variable members\n");
3518 break;
3524 if (!status) {
3525 WCHAR *string;
3526 if (pBstrName) {
3527 if (nameoffset == -1)
3528 *pBstrName = NULL;
3529 else {
3530 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3531 typelib_segment_data[MSFT_SEG_NAME][nameoffset];
3532 ctl2_decode_name((char*)&name->namelen, &string);
3533 *pBstrName = SysAllocString(string);
3534 if(!*pBstrName)
3535 return E_OUTOFMEMORY;
3539 if (pBstrDocString) {
3540 if (docstringoffset == -1)
3541 *pBstrDocString = NULL;
3542 else {
3543 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3544 typelib_segment_data[MSFT_SEG_NAME][docstringoffset];
3545 ctl2_decode_name((char*)&name->namelen, &string);
3546 *pBstrDocString = SysAllocString(string);
3547 if(!*pBstrDocString) {
3548 if (pBstrName) SysFreeString(*pBstrName);
3549 return E_OUTOFMEMORY;
3554 if (pdwHelpContext) {
3555 *pdwHelpContext = helpcontext;
3558 if (pBstrHelpFile) {
3559 status = ITypeLib_GetDocumentation((ITypeLib*)&This->typelib->lpVtblTypeLib2,
3560 -1, NULL, NULL, NULL, pBstrHelpFile);
3561 if (status) {
3562 if (pBstrName) SysFreeString(*pBstrName);
3563 if (pBstrDocString) SysFreeString(*pBstrDocString);
3568 return status;
3571 /******************************************************************************
3572 * ITypeInfo2_GetDllEntry {OLEAUT32}
3574 static HRESULT WINAPI ITypeInfo2_fnGetDllEntry(
3575 ITypeInfo2* iface,
3576 MEMBERID memid,
3577 INVOKEKIND invKind,
3578 BSTR* pBstrDllName,
3579 BSTR* pBstrName,
3580 WORD* pwOrdinal)
3582 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
3583 return E_OUTOFMEMORY;
3586 /******************************************************************************
3587 * ITypeInfo2_GetRefTypeInfo {OLEAUT32}
3589 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeInfo(
3590 ITypeInfo2* iface,
3591 HREFTYPE hRefType,
3592 ITypeInfo** ppTInfo)
3594 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3596 TRACE("(%p,%d,%p)\n", iface, hRefType, ppTInfo);
3598 if(!ppTInfo)
3599 return E_INVALIDARG;
3601 if(hRefType==-2 && This->dual) {
3602 *ppTInfo = (ITypeInfo*)&This->dual->lpVtblTypeInfo2;
3603 ITypeInfo_AddRef(*ppTInfo);
3604 return S_OK;
3607 if(hRefType&1) {
3608 ITypeLib *tl;
3609 MSFT_ImpInfo *impinfo;
3610 MSFT_ImpFile *impfile;
3611 MSFT_GuidEntry *guid;
3612 WCHAR *filename;
3613 HRESULT hres;
3615 if((hRefType&(~0x3)) >= This->typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length)
3616 return E_FAIL;
3618 impinfo = (MSFT_ImpInfo*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][hRefType&(~0x3)];
3619 impfile = (MSFT_ImpFile*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][impinfo->oImpFile];
3620 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo->oGuid];
3622 ctl2_decode_string((unsigned char*)impfile->filename, &filename);
3624 hres = LoadTypeLib(filename, &tl);
3625 if(FAILED(hres))
3626 return hres;
3628 hres = ITypeLib_GetTypeInfoOfGuid(tl, &guid->guid, ppTInfo);
3630 ITypeLib_Release(tl);
3631 return hres;
3632 } else {
3633 ICreateTypeInfo2Impl *iter;
3634 int i = 0;
3636 for(iter=This->typelib->typeinfos; iter; iter=iter->next_typeinfo) {
3637 if(This->typelib->typelib_typeinfo_offsets[i] == (hRefType&(~0x3))) {
3638 *ppTInfo = (ITypeInfo*)&iter->lpVtblTypeInfo2;
3640 ITypeLib_AddRef(*ppTInfo);
3641 return S_OK;
3643 i++;
3647 return E_FAIL;
3650 /******************************************************************************
3651 * ITypeInfo2_AddressOfMember {OLEAUT32}
3653 static HRESULT WINAPI ITypeInfo2_fnAddressOfMember(
3654 ITypeInfo2* iface,
3655 MEMBERID memid,
3656 INVOKEKIND invKind,
3657 PVOID* ppv)
3659 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, ppv);
3660 return E_OUTOFMEMORY;
3663 /******************************************************************************
3664 * ITypeInfo2_CreateInstance {OLEAUT32}
3666 static HRESULT WINAPI ITypeInfo2_fnCreateInstance(
3667 ITypeInfo2* iface,
3668 IUnknown* pUnkOuter,
3669 REFIID riid,
3670 PVOID* ppvObj)
3672 FIXME("(%p,%p,%s,%p), stub!\n", iface, pUnkOuter, debugstr_guid(riid), ppvObj);
3673 return E_OUTOFMEMORY;
3676 /******************************************************************************
3677 * ITypeInfo2_GetMops {OLEAUT32}
3679 static HRESULT WINAPI ITypeInfo2_fnGetMops(
3680 ITypeInfo2* iface,
3681 MEMBERID memid,
3682 BSTR* pBstrMops)
3684 FIXME("(%p,%d,%p), stub!\n", iface, memid, pBstrMops);
3685 return E_OUTOFMEMORY;
3688 /******************************************************************************
3689 * ITypeInfo2_GetContainingTypeLib {OLEAUT32}
3691 static HRESULT WINAPI ITypeInfo2_fnGetContainingTypeLib(
3692 ITypeInfo2* iface,
3693 ITypeLib** ppTLib,
3694 UINT* pIndex)
3696 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3698 TRACE("(%p,%p,%p)\n", iface, ppTLib, pIndex);
3700 *ppTLib = (ITypeLib *)&This->typelib->lpVtblTypeLib2;
3701 ICreateTypeLib_AddRef((ICreateTypeLib*)This->typelib);
3702 *pIndex = This->typeinfo->typekind >> 16;
3704 return S_OK;
3707 static void release_typedesc(TYPEDESC *tdesc)
3709 while (tdesc) {
3710 TYPEDESC *next;
3711 if (tdesc->vt == VT_USERDEFINED)
3712 next = NULL;
3713 else
3714 next = tdesc->u.lptdesc;
3715 heap_free(tdesc);
3716 tdesc = next;
3720 /******************************************************************************
3721 * ITypeInfo2_ReleaseTypeAttr {OLEAUT32}
3723 static void WINAPI ITypeInfo2_fnReleaseTypeAttr(
3724 ITypeInfo2* iface,
3725 TYPEATTR* pTypeAttr)
3727 TRACE("(%p,%p)\n", iface, pTypeAttr);
3729 if (pTypeAttr->tdescAlias.vt != VT_USERDEFINED)
3730 release_typedesc(pTypeAttr->tdescAlias.u.lptdesc);
3732 heap_free(pTypeAttr);
3735 /******************************************************************************
3736 * ITypeInfo2_ReleaseFuncDesc {OLEAUT32}
3738 static void WINAPI ITypeInfo2_fnReleaseFuncDesc(
3739 ITypeInfo2* iface,
3740 FUNCDESC* pFuncDesc)
3742 int i;
3744 TRACE("(%p,%p)\n", iface, pFuncDesc);
3746 heap_free(pFuncDesc->lprgscode);
3748 if (pFuncDesc->lprgelemdescParam) {
3749 for (i = 0; i < pFuncDesc->cParams; ++i) {
3750 if (pFuncDesc->lprgelemdescParam[i].tdesc.vt != VT_USERDEFINED)
3751 release_typedesc(pFuncDesc->lprgelemdescParam[i].tdesc.u.lptdesc);
3753 if (pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex)
3755 VariantClear(&pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3756 heap_free(pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex);
3759 heap_free(pFuncDesc->lprgelemdescParam);
3762 heap_free(pFuncDesc->elemdescFunc.u.paramdesc.pparamdescex);
3764 if (pFuncDesc->elemdescFunc.tdesc.vt != VT_USERDEFINED)
3765 release_typedesc(pFuncDesc->elemdescFunc.tdesc.u.lptdesc);
3767 heap_free(pFuncDesc);
3770 /******************************************************************************
3771 * ITypeInfo2_ReleaseVarDesc {OLEAUT32}
3773 static void WINAPI ITypeInfo2_fnReleaseVarDesc(
3774 ITypeInfo2* iface,
3775 VARDESC* pVarDesc)
3777 FIXME("(%p,%p), stub!\n", iface, pVarDesc);
3780 /******************************************************************************
3781 * ITypeInfo2_GetTypeKind {OLEAUT32}
3783 * Get the TYPEKIND value for a TypeInfo.
3785 * RETURNS
3787 * Success: S_OK.
3788 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3790 static HRESULT WINAPI ITypeInfo2_fnGetTypeKind(
3791 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typekind for. */
3792 TYPEKIND* pTypeKind) /* [O] The typekind for this TypeInfo. */
3794 FIXME("(%p,%p), stub!\n", iface, pTypeKind);
3795 return E_OUTOFMEMORY;
3798 /******************************************************************************
3799 * ITypeInfo2_GetTypeFlags {OLEAUT32}
3801 * Get the Type Flags for a TypeInfo.
3803 * RETURNS
3805 * Success: S_OK.
3806 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3808 static HRESULT WINAPI ITypeInfo2_fnGetTypeFlags(
3809 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typeflags for. */
3810 ULONG* pTypeFlags) /* [O] The type flags for this TypeInfo. */
3812 FIXME("(%p,%p), stub!\n", iface, pTypeFlags);
3813 return E_OUTOFMEMORY;
3816 /******************************************************************************
3817 * ITypeInfo2_GetFuncIndexOfMemId {OLEAUT32}
3819 * Gets the index of a function given its member id.
3821 * RETURNS
3823 * Success: S_OK.
3824 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3826 static HRESULT WINAPI ITypeInfo2_fnGetFuncIndexOfMemId(
3827 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the function. */
3828 MEMBERID memid, /* [I] The member id for the function. */
3829 INVOKEKIND invKind, /* [I] The invocation kind for the function. */
3830 UINT* pFuncIndex) /* [O] The index of the function. */
3832 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, pFuncIndex);
3833 return E_OUTOFMEMORY;
3836 /******************************************************************************
3837 * ITypeInfo2_GetVarIndexOfMemId {OLEAUT32}
3839 * Gets the index of a variable given its member id.
3841 * RETURNS
3843 * Success: S_OK.
3844 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3846 static HRESULT WINAPI ITypeInfo2_fnGetVarIndexOfMemId(
3847 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the variable. */
3848 MEMBERID memid, /* [I] The member id for the variable. */
3849 UINT* pVarIndex) /* [O] The index of the variable. */
3851 FIXME("(%p,%d,%p), stub!\n", iface, memid, pVarIndex);
3852 return E_OUTOFMEMORY;
3855 /******************************************************************************
3856 * ITypeInfo2_GetCustData {OLEAUT32}
3858 * Gets a custom data element from a TypeInfo.
3860 * RETURNS
3862 * Success: S_OK.
3863 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3865 static HRESULT WINAPI ITypeInfo2_fnGetCustData(
3866 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3867 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3868 VARIANT* pVarVal) /* [O] The custom data. */
3870 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3871 MSFT_CDGuid *cdentry;
3872 int offset;
3874 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
3876 if (!guid || !pVarVal)
3877 return E_INVALIDARG;
3879 VariantClear(pVarVal);
3881 offset = ctl2_find_custdata(This->typelib, guid, This->typeinfo->oCustData);
3882 if (offset == -1)
3883 return S_OK;
3885 cdentry = (MSFT_CDGuid *)&This->typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
3886 return ctl2_decode_variant(This->typelib, cdentry->DataOffset, pVarVal);
3889 /******************************************************************************
3890 * ITypeInfo2_GetFuncCustData {OLEAUT32}
3892 * Gets a custom data element from a function.
3894 * RETURNS
3896 * Success: S_OK.
3897 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3899 static HRESULT WINAPI ITypeInfo2_fnGetFuncCustData(
3900 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3901 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
3902 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3903 VARIANT* pVarVal) /* [O] The custom data. */
3905 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3906 return E_OUTOFMEMORY;
3909 /******************************************************************************
3910 * ITypeInfo2_GetParamCustData {OLEAUT32}
3912 * Gets a custom data element from a parameter.
3914 * RETURNS
3916 * Success: S_OK.
3917 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3919 static HRESULT WINAPI ITypeInfo2_fnGetParamCustData(
3920 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3921 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
3922 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
3923 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3924 VARIANT* pVarVal) /* [O] The custom data. */
3926 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3927 return E_OUTOFMEMORY;
3930 /******************************************************************************
3931 * ITypeInfo2_GetVarCustData {OLEAUT32}
3933 * Gets a custom data element from a variable.
3935 * RETURNS
3937 * Success: S_OK.
3938 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3940 static HRESULT WINAPI ITypeInfo2_fnGetVarCustData(
3941 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3942 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
3943 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3944 VARIANT* pVarVal) /* [O] The custom data. */
3946 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3947 return E_OUTOFMEMORY;
3950 /******************************************************************************
3951 * ITypeInfo2_GetImplTypeCustData {OLEAUT32}
3953 * Gets a custom data element from an implemented type of a TypeInfo.
3955 * RETURNS
3957 * Success: S_OK.
3958 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3960 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeCustData(
3961 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3962 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
3963 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3964 VARIANT* pVarVal) /* [O] The custom data. */
3966 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3967 return E_OUTOFMEMORY;
3970 /******************************************************************************
3971 * ITypeInfo2_GetDocumentation2 {OLEAUT32}
3973 * Gets some documentation from a TypeInfo in a locale-aware fashion.
3975 * RETURNS
3977 * Success: S_OK.
3978 * Failure: One of STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
3980 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation2(
3981 ITypeInfo2* iface, /* [I] The TypeInfo to retrieve the documentation from. */
3982 MEMBERID memid, /* [I] The member id (why?). */
3983 LCID lcid, /* [I] The locale (why?). */
3984 BSTR* pbstrHelpString, /* [O] The help string. */
3985 DWORD* pdwHelpStringContext, /* [O] The help string context. */
3986 BSTR* pbstrHelpStringDll) /* [O] The help file name. */
3988 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
3989 return E_OUTOFMEMORY;
3992 /******************************************************************************
3993 * ITypeInfo2_GetAllCustData {OLEAUT32}
3995 * Gets all of the custom data associated with a TypeInfo.
3997 * RETURNS
3999 * Success: S_OK.
4000 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4002 static HRESULT WINAPI ITypeInfo2_fnGetAllCustData(
4003 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4004 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4006 FIXME("(%p,%p), stub!\n", iface, pCustData);
4007 return E_OUTOFMEMORY;
4010 /******************************************************************************
4011 * ITypeInfo2_GetAllFuncCustData {OLEAUT32}
4013 * Gets all of the custom data associated with a function.
4015 * RETURNS
4017 * Success: S_OK.
4018 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4020 static HRESULT WINAPI ITypeInfo2_fnGetAllFuncCustData(
4021 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4022 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
4023 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4025 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4026 return E_OUTOFMEMORY;
4029 /******************************************************************************
4030 * ITypeInfo2_GetAllParamCustData {OLEAUT32}
4032 * Gets all of the custom data associated with a parameter.
4034 * RETURNS
4036 * Success: S_OK.
4037 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4039 static HRESULT WINAPI ITypeInfo2_fnGetAllParamCustData(
4040 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4041 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
4042 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
4043 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4045 FIXME("(%p,%d,%d,%p), stub!\n", iface, indexFunc, indexParam, pCustData);
4046 return E_OUTOFMEMORY;
4049 /******************************************************************************
4050 * ITypeInfo2_GetAllVarCustData {OLEAUT32}
4052 * Gets all of the custom data associated with a variable.
4054 * RETURNS
4056 * Success: S_OK.
4057 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4059 static HRESULT WINAPI ITypeInfo2_fnGetAllVarCustData(
4060 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4061 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
4062 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4064 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4065 return E_OUTOFMEMORY;
4068 /******************************************************************************
4069 * ITypeInfo2_GetAllImplTypeCustData {OLEAUT32}
4071 * Gets all of the custom data associated with an implemented type.
4073 * RETURNS
4075 * Success: S_OK.
4076 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4078 static HRESULT WINAPI ITypeInfo2_fnGetAllImplTypeCustData(
4079 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4080 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
4081 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4083 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4084 return E_OUTOFMEMORY;
4088 /*================== ICreateTypeInfo2 & ITypeInfo2 VTABLEs And Creation ===================================*/
4090 static const ICreateTypeInfo2Vtbl ctypeinfo2vt =
4093 ICreateTypeInfo2_fnQueryInterface,
4094 ICreateTypeInfo2_fnAddRef,
4095 ICreateTypeInfo2_fnRelease,
4097 ICreateTypeInfo2_fnSetGuid,
4098 ICreateTypeInfo2_fnSetTypeFlags,
4099 ICreateTypeInfo2_fnSetDocString,
4100 ICreateTypeInfo2_fnSetHelpContext,
4101 ICreateTypeInfo2_fnSetVersion,
4102 ICreateTypeInfo2_fnAddRefTypeInfo,
4103 ICreateTypeInfo2_fnAddFuncDesc,
4104 ICreateTypeInfo2_fnAddImplType,
4105 ICreateTypeInfo2_fnSetImplTypeFlags,
4106 ICreateTypeInfo2_fnSetAlignment,
4107 ICreateTypeInfo2_fnSetSchema,
4108 ICreateTypeInfo2_fnAddVarDesc,
4109 ICreateTypeInfo2_fnSetFuncAndParamNames,
4110 ICreateTypeInfo2_fnSetVarName,
4111 ICreateTypeInfo2_fnSetTypeDescAlias,
4112 ICreateTypeInfo2_fnDefineFuncAsDllEntry,
4113 ICreateTypeInfo2_fnSetFuncDocString,
4114 ICreateTypeInfo2_fnSetVarDocString,
4115 ICreateTypeInfo2_fnSetFuncHelpContext,
4116 ICreateTypeInfo2_fnSetVarHelpContext,
4117 ICreateTypeInfo2_fnSetMops,
4118 ICreateTypeInfo2_fnSetTypeIdldesc,
4119 ICreateTypeInfo2_fnLayOut,
4121 ICreateTypeInfo2_fnDeleteFuncDesc,
4122 ICreateTypeInfo2_fnDeleteFuncDescByMemId,
4123 ICreateTypeInfo2_fnDeleteVarDesc,
4124 ICreateTypeInfo2_fnDeleteVarDescByMemId,
4125 ICreateTypeInfo2_fnDeleteImplType,
4126 ICreateTypeInfo2_fnSetCustData,
4127 ICreateTypeInfo2_fnSetFuncCustData,
4128 ICreateTypeInfo2_fnSetParamCustData,
4129 ICreateTypeInfo2_fnSetVarCustData,
4130 ICreateTypeInfo2_fnSetImplTypeCustData,
4131 ICreateTypeInfo2_fnSetHelpStringContext,
4132 ICreateTypeInfo2_fnSetFuncHelpStringContext,
4133 ICreateTypeInfo2_fnSetVarHelpStringContext,
4134 ICreateTypeInfo2_fnInvalidate,
4135 ICreateTypeInfo2_fnSetName
4138 static const ITypeInfo2Vtbl typeinfo2vt =
4141 ITypeInfo2_fnQueryInterface,
4142 ITypeInfo2_fnAddRef,
4143 ITypeInfo2_fnRelease,
4145 ITypeInfo2_fnGetTypeAttr,
4146 ITypeInfo2_fnGetTypeComp,
4147 ITypeInfo2_fnGetFuncDesc,
4148 ITypeInfo2_fnGetVarDesc,
4149 ITypeInfo2_fnGetNames,
4150 ITypeInfo2_fnGetRefTypeOfImplType,
4151 ITypeInfo2_fnGetImplTypeFlags,
4152 ITypeInfo2_fnGetIDsOfNames,
4153 ITypeInfo2_fnInvoke,
4154 ITypeInfo2_fnGetDocumentation,
4155 ITypeInfo2_fnGetDllEntry,
4156 ITypeInfo2_fnGetRefTypeInfo,
4157 ITypeInfo2_fnAddressOfMember,
4158 ITypeInfo2_fnCreateInstance,
4159 ITypeInfo2_fnGetMops,
4160 ITypeInfo2_fnGetContainingTypeLib,
4161 ITypeInfo2_fnReleaseTypeAttr,
4162 ITypeInfo2_fnReleaseFuncDesc,
4163 ITypeInfo2_fnReleaseVarDesc,
4165 ITypeInfo2_fnGetTypeKind,
4166 ITypeInfo2_fnGetTypeFlags,
4167 ITypeInfo2_fnGetFuncIndexOfMemId,
4168 ITypeInfo2_fnGetVarIndexOfMemId,
4169 ITypeInfo2_fnGetCustData,
4170 ITypeInfo2_fnGetFuncCustData,
4171 ITypeInfo2_fnGetParamCustData,
4172 ITypeInfo2_fnGetVarCustData,
4173 ITypeInfo2_fnGetImplTypeCustData,
4174 ITypeInfo2_fnGetDocumentation2,
4175 ITypeInfo2_fnGetAllCustData,
4176 ITypeInfo2_fnGetAllFuncCustData,
4177 ITypeInfo2_fnGetAllParamCustData,
4178 ITypeInfo2_fnGetAllVarCustData,
4179 ITypeInfo2_fnGetAllImplTypeCustData,
4182 static ICreateTypeInfo2 *ICreateTypeInfo2_Constructor(ICreateTypeLib2Impl *typelib, WCHAR *szName, TYPEKIND tkind)
4184 ICreateTypeInfo2Impl *pCreateTypeInfo2Impl;
4186 int nameoffset;
4187 int typeinfo_offset;
4188 MSFT_TypeInfoBase *typeinfo;
4190 TRACE("Constructing ICreateTypeInfo2 for %s with tkind %d\n", debugstr_w(szName), tkind);
4192 pCreateTypeInfo2Impl = heap_alloc_zero(sizeof(ICreateTypeInfo2Impl));
4193 if (!pCreateTypeInfo2Impl) return NULL;
4195 pCreateTypeInfo2Impl->lpVtbl = &ctypeinfo2vt;
4196 pCreateTypeInfo2Impl->lpVtblTypeInfo2 = &typeinfo2vt;
4197 pCreateTypeInfo2Impl->ref = 1;
4199 pCreateTypeInfo2Impl->typelib = typelib;
4200 ICreateTypeLib_AddRef((ICreateTypeLib*)typelib);
4202 nameoffset = ctl2_alloc_name(typelib, szName);
4203 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
4204 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
4206 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
4207 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
4209 pCreateTypeInfo2Impl->typeinfo = typeinfo;
4211 pCreateTypeInfo2Impl->typekind = tkind;
4212 typeinfo->typekind |= tkind | 0x20;
4213 ICreateTypeInfo2_SetAlignment((ICreateTypeInfo2 *)pCreateTypeInfo2Impl, 4);
4215 switch (tkind) {
4216 case TKIND_ENUM:
4217 case TKIND_INTERFACE:
4218 case TKIND_DISPATCH:
4219 case TKIND_COCLASS:
4220 typeinfo->size = 4;
4221 break;
4223 case TKIND_RECORD:
4224 case TKIND_UNION:
4225 typeinfo->size = 0;
4226 break;
4228 case TKIND_MODULE:
4229 typeinfo->size = 2;
4230 break;
4232 case TKIND_ALIAS:
4233 typeinfo->size = -0x75;
4234 break;
4236 default:
4237 FIXME("(%s,%d), unrecognized typekind %d\n", debugstr_w(szName), tkind, tkind);
4238 typeinfo->size = 0xdeadbeef;
4239 break;
4242 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = pCreateTypeInfo2Impl;
4243 typelib->last_typeinfo = pCreateTypeInfo2Impl;
4244 if (!typelib->typeinfos) typelib->typeinfos = pCreateTypeInfo2Impl;
4246 TRACE(" -- %p\n", pCreateTypeInfo2Impl);
4248 return (ICreateTypeInfo2 *)pCreateTypeInfo2Impl;
4252 /*================== ICreateTypeLib2 Implementation ===================================*/
4254 /******************************************************************************
4255 * ICreateTypeLib2_QueryInterface {OLEAUT32}
4257 static HRESULT WINAPI ICreateTypeLib2_fnQueryInterface(
4258 ICreateTypeLib2 * iface,
4259 REFIID riid,
4260 VOID **ppvObject)
4262 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4264 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
4266 *ppvObject=NULL;
4267 if(IsEqualIID(riid, &IID_IUnknown) ||
4268 IsEqualIID(riid,&IID_ICreateTypeLib)||
4269 IsEqualIID(riid,&IID_ICreateTypeLib2))
4271 *ppvObject = This;
4272 } else if (IsEqualIID(riid, &IID_ITypeLib) ||
4273 IsEqualIID(riid, &IID_ITypeLib2)) {
4274 *ppvObject = &This->lpVtblTypeLib2;
4277 if(*ppvObject)
4279 ICreateTypeLib2_AddRef(iface);
4280 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
4281 return S_OK;
4283 TRACE("-- Interface: E_NOINTERFACE\n");
4284 return E_NOINTERFACE;
4287 /******************************************************************************
4288 * ICreateTypeLib2_AddRef {OLEAUT32}
4290 static ULONG WINAPI ICreateTypeLib2_fnAddRef(ICreateTypeLib2 *iface)
4292 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4293 ULONG ref = InterlockedIncrement(&This->ref);
4295 TRACE("(%p)->(%u)\n", This, ref);
4297 return ref;
4300 /******************************************************************************
4301 * ICreateTypeLib2_Release {OLEAUT32}
4303 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface)
4305 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4306 ULONG ref = InterlockedDecrement(&This->ref);
4308 TRACE("(%p)->(%u)\n", This, ref);
4310 if (!ref) {
4311 int i;
4313 for (i = 0; i < MSFT_SEG_MAX; i++) {
4314 heap_free(This->typelib_segment_data[i]);
4315 This->typelib_segment_data[i] = NULL;
4318 heap_free(This->filename);
4319 This->filename = NULL;
4321 while (This->typeinfos) {
4322 ICreateTypeInfo2Impl *typeinfo = This->typeinfos;
4323 This->typeinfos = typeinfo->next_typeinfo;
4324 if(typeinfo->typedata) {
4325 CyclicList *iter, *rem;
4327 rem = typeinfo->typedata->next;
4328 typeinfo->typedata->next = NULL;
4329 iter = rem->next;
4330 heap_free(rem);
4332 while(iter) {
4333 rem = iter;
4334 iter = iter->next;
4335 heap_free(rem->u.data);
4336 heap_free(rem);
4340 heap_free(typeinfo->dual);
4341 heap_free(typeinfo);
4344 heap_free(This);
4347 return ref;
4351 /******************************************************************************
4352 * ICreateTypeLib2_CreateTypeInfo {OLEAUT32}
4354 static HRESULT WINAPI ICreateTypeLib2_fnCreateTypeInfo(
4355 ICreateTypeLib2 * iface,
4356 LPOLESTR szName,
4357 TYPEKIND tkind,
4358 ICreateTypeInfo **tinfo)
4360 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4361 char *name;
4363 TRACE("(%p,%s,%d,%p)\n", iface, debugstr_w(szName), tkind, tinfo);
4365 if (!szName || !tinfo) return E_INVALIDARG;
4367 ctl2_encode_name(This, szName, &name);
4368 if(ctl2_find_name(This, name) != -1)
4369 return TYPE_E_NAMECONFLICT;
4371 *tinfo = (ICreateTypeInfo *)ICreateTypeInfo2_Constructor(This, szName, tkind);
4372 if (!*tinfo) return E_OUTOFMEMORY;
4374 return S_OK;
4377 /******************************************************************************
4378 * ICreateTypeLib2_SetName {OLEAUT32}
4380 static HRESULT WINAPI ICreateTypeLib2_fnSetName(
4381 ICreateTypeLib2 * iface,
4382 LPOLESTR szName)
4384 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4386 int offset;
4388 TRACE("(%p,%s)\n", iface, debugstr_w(szName));
4390 offset = ctl2_alloc_name(This, szName);
4391 if (offset == -1) return E_OUTOFMEMORY;
4392 This->typelib_header.NameOffset = offset;
4393 return S_OK;
4396 /******************************************************************************
4397 * ICreateTypeLib2_SetVersion {OLEAUT32}
4399 static HRESULT WINAPI ICreateTypeLib2_fnSetVersion(ICreateTypeLib2 * iface, WORD wMajorVerNum, WORD wMinorVerNum)
4401 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4403 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
4405 This->typelib_header.version = wMajorVerNum | (wMinorVerNum << 16);
4406 return S_OK;
4409 /******************************************************************************
4410 * ICreateTypeLib2_SetGuid {OLEAUT32}
4412 static HRESULT WINAPI ICreateTypeLib2_fnSetGuid(ICreateTypeLib2 * iface, REFGUID guid)
4414 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4416 MSFT_GuidEntry guidentry;
4417 int offset;
4419 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
4421 guidentry.guid = *guid;
4422 guidentry.hreftype = -2;
4423 guidentry.next_hash = -1;
4425 offset = ctl2_alloc_guid(This, &guidentry);
4427 if (offset == -1) return E_OUTOFMEMORY;
4429 This->typelib_header.posguid = offset;
4431 return S_OK;
4434 /******************************************************************************
4435 * ICreateTypeLib2_SetDocString {OLEAUT32}
4437 static HRESULT WINAPI ICreateTypeLib2_fnSetDocString(ICreateTypeLib2 * iface, LPOLESTR szDoc)
4439 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4441 int offset;
4443 TRACE("(%p,%s)\n", iface, debugstr_w(szDoc));
4444 if (!szDoc)
4445 return E_INVALIDARG;
4447 offset = ctl2_alloc_string(This, szDoc);
4448 if (offset == -1) return E_OUTOFMEMORY;
4449 This->typelib_header.helpstring = offset;
4450 return S_OK;
4453 /******************************************************************************
4454 * ICreateTypeLib2_SetHelpFileName {OLEAUT32}
4456 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpFileName(ICreateTypeLib2 * iface, LPOLESTR szHelpFileName)
4458 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4460 int offset;
4462 TRACE("(%p,%s)\n", iface, debugstr_w(szHelpFileName));
4464 offset = ctl2_alloc_string(This, szHelpFileName);
4465 if (offset == -1) return E_OUTOFMEMORY;
4466 This->typelib_header.helpfile = offset;
4467 This->typelib_header.varflags |= 0x10;
4468 return S_OK;
4471 /******************************************************************************
4472 * ICreateTypeLib2_SetHelpContext {OLEAUT32}
4474 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpContext(ICreateTypeLib2 * iface, DWORD dwHelpContext)
4476 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4478 TRACE("(%p,%d)\n", iface, dwHelpContext);
4479 This->typelib_header.helpcontext = dwHelpContext;
4480 return S_OK;
4483 /******************************************************************************
4484 * ICreateTypeLib2_SetLcid {OLEAUT32}
4486 * Sets both the lcid and lcid2 members in the header to lcid.
4488 * As a special case if lcid == LOCALE_NEUTRAL (0), then the first header lcid
4489 * is set to US English while the second one is set to 0.
4491 static HRESULT WINAPI ICreateTypeLib2_fnSetLcid(ICreateTypeLib2 * iface, LCID lcid)
4493 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4495 TRACE("(%p,%d)\n", iface, lcid);
4497 This->typelib_header.lcid = This->typelib_header.lcid2 = lcid;
4499 if(lcid == LOCALE_NEUTRAL) This->typelib_header.lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
4501 return S_OK;
4504 /******************************************************************************
4505 * ICreateTypeLib2_SetLibFlags {OLEAUT32}
4507 static HRESULT WINAPI ICreateTypeLib2_fnSetLibFlags(ICreateTypeLib2 * iface, UINT uLibFlags)
4509 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4511 TRACE("(%p,0x%x)\n", iface, uLibFlags);
4513 This->typelib_header.flags = uLibFlags;
4515 return S_OK;
4518 static int ctl2_write_chunk(HANDLE hFile, const void *segment, int length)
4520 DWORD dwWritten;
4521 if (!WriteFile(hFile, segment, length, &dwWritten, 0)) {
4522 CloseHandle(hFile);
4523 return 0;
4525 return -1;
4528 static int ctl2_write_segment(ICreateTypeLib2Impl *This, HANDLE hFile, int segment)
4530 DWORD dwWritten;
4531 if (!WriteFile(hFile, This->typelib_segment_data[segment],
4532 This->typelib_segdir[segment].length, &dwWritten, 0)) {
4533 CloseHandle(hFile);
4534 return 0;
4537 return -1;
4540 static HRESULT ctl2_finalize_typeinfos(ICreateTypeLib2Impl *This, int filesize)
4542 ICreateTypeInfo2Impl *typeinfo;
4543 HRESULT hres;
4545 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4546 typeinfo->typeinfo->memoffset = filesize;
4548 hres = ICreateTypeInfo2_fnLayOut((ICreateTypeInfo2 *)typeinfo);
4549 if(FAILED(hres))
4550 return hres;
4552 if (typeinfo->typedata)
4553 filesize += typeinfo->typedata->next->u.val
4554 + cti2_get_var_count(typeinfo->typeinfo) * 12
4555 + cti2_get_func_count(typeinfo->typeinfo) * 12 + 4;
4558 return S_OK;
4561 static int ctl2_finalize_segment(ICreateTypeLib2Impl *This, int filepos, int segment)
4563 if (This->typelib_segdir[segment].length) {
4564 This->typelib_segdir[segment].offset = filepos;
4565 } else {
4566 This->typelib_segdir[segment].offset = -1;
4569 return This->typelib_segdir[segment].length;
4572 static void ctl2_write_typeinfos(ICreateTypeLib2Impl *This, HANDLE hFile)
4574 ICreateTypeInfo2Impl *typeinfo;
4576 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4577 CyclicList *iter;
4578 int offset = 0;
4580 if (!typeinfo->typedata) continue;
4582 iter = typeinfo->typedata->next;
4583 ctl2_write_chunk(hFile, &iter->u.val, sizeof(int));
4584 for(iter=iter->next; iter!=typeinfo->typedata->next; iter=iter->next)
4585 ctl2_write_chunk(hFile, iter->u.data, ctl2_get_record_size(iter));
4587 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4588 ctl2_write_chunk(hFile, &iter->indice, sizeof(int));
4590 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4591 ctl2_write_chunk(hFile, &iter->name, sizeof(int));
4593 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next) {
4594 ctl2_write_chunk(hFile, &offset, sizeof(int));
4595 offset += ctl2_get_record_size(iter);
4600 /******************************************************************************
4601 * ICreateTypeLib2_SaveAllChanges {OLEAUT32}
4603 static HRESULT WINAPI ICreateTypeLib2_fnSaveAllChanges(ICreateTypeLib2 * iface)
4605 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4607 int retval;
4608 int filepos;
4609 HANDLE hFile;
4610 HRESULT hres;
4612 TRACE("(%p)\n", iface);
4614 retval = TYPE_E_IOERROR;
4616 hFile = CreateFileW(This->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4617 if (hFile == INVALID_HANDLE_VALUE) return retval;
4619 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
4620 filepos += This->typelib_header.nrtypeinfos * 4;
4622 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEINFO);
4623 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUIDHASH);
4624 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUID);
4625 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_REFERENCES);
4626 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTINFO);
4627 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTFILES);
4628 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAMEHASH);
4629 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAME);
4630 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_STRING);
4631 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEDESC);
4632 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_ARRAYDESC);
4633 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATA);
4634 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATAGUID);
4636 hres = ctl2_finalize_typeinfos(This, filepos);
4637 if(FAILED(hres)) {
4638 CloseHandle(hFile);
4639 return hres;
4642 if (!ctl2_write_chunk(hFile, &This->typelib_header, sizeof(This->typelib_header))) return retval;
4643 if (This->typelib_header.varflags & HELPDLLFLAG)
4644 if (!ctl2_write_chunk(hFile, &This->helpStringDll, sizeof(This->helpStringDll))) return retval;
4645 if (!ctl2_write_chunk(hFile, This->typelib_typeinfo_offsets, This->typelib_header.nrtypeinfos * 4)) return retval;
4646 if (!ctl2_write_chunk(hFile, This->typelib_segdir, sizeof(This->typelib_segdir))) return retval;
4647 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEINFO )) return retval;
4648 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUIDHASH )) return retval;
4649 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUID )) return retval;
4650 if (!ctl2_write_segment(This, hFile, MSFT_SEG_REFERENCES )) return retval;
4651 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTINFO )) return retval;
4652 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTFILES )) return retval;
4653 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAMEHASH )) return retval;
4654 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAME )) return retval;
4655 if (!ctl2_write_segment(This, hFile, MSFT_SEG_STRING )) return retval;
4656 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEDESC )) return retval;
4657 if (!ctl2_write_segment(This, hFile, MSFT_SEG_ARRAYDESC )) return retval;
4658 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATA )) return retval;
4659 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATAGUID)) return retval;
4661 ctl2_write_typeinfos(This, hFile);
4663 if (!CloseHandle(hFile)) return retval;
4665 return S_OK;
4669 /******************************************************************************
4670 * ICreateTypeLib2_DeleteTypeInfo {OLEAUT32}
4672 * Deletes a named TypeInfo from a type library.
4674 * RETURNS
4676 * Success: S_OK
4677 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4679 static HRESULT WINAPI ICreateTypeLib2_fnDeleteTypeInfo(
4680 ICreateTypeLib2 * iface, /* [I] The type library to delete from. */
4681 LPOLESTR szName) /* [I] The name of the typeinfo to delete. */
4683 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
4684 return E_OUTOFMEMORY;
4687 /******************************************************************************
4688 * ICreateTypeLib2_SetCustData {OLEAUT32}
4690 * Sets custom data for a type library.
4692 * RETURNS
4694 * Success: S_OK
4695 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4697 static HRESULT WINAPI ICreateTypeLib2_fnSetCustData(
4698 ICreateTypeLib2 * iface, /* [I] The type library to store the custom data in. */
4699 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
4700 VARIANT *pVarVal) /* [I] The custom data itself. */
4702 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4704 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
4706 return ctl2_set_custdata(This, guid, pVarVal, &This->typelib_header.CustomDataOffset);
4709 /******************************************************************************
4710 * ICreateTypeLib2_SetHelpStringContext {OLEAUT32}
4712 * Sets a context number for the library help string.
4714 * PARAMS
4715 * iface [I] The type library to set the help string context for.
4716 * dwContext [I] The help string context.
4718 * RETURNS
4719 * Success: S_OK
4720 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4722 static
4723 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringContext(ICreateTypeLib2 * iface,
4724 ULONG dwContext)
4726 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4728 TRACE("(%p,%d)\n", iface, dwContext);
4730 This->typelib_header.helpstringcontext = dwContext;
4731 return S_OK;
4734 /******************************************************************************
4735 * ICreateTypeLib2_SetHelpStringDll {OLEAUT32}
4737 * Set the DLL used to look up localized help strings.
4739 * PARAMS
4740 * iface [I] The type library to set the help DLL for.
4741 * szDllName [I] The name of the help DLL.
4743 * RETURNS
4744 * Success: S_OK
4745 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4747 static
4748 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringDll(ICreateTypeLib2 * iface,
4749 LPOLESTR szDllName)
4751 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4752 int offset;
4754 TRACE("(%p,%s)\n", iface, debugstr_w(szDllName));
4755 if (!szDllName)
4756 return E_INVALIDARG;
4758 offset = ctl2_alloc_string(This, szDllName);
4759 if (offset == -1)
4760 return E_OUTOFMEMORY;
4761 This->typelib_header.varflags |= HELPDLLFLAG;
4762 This->helpStringDll = offset;
4763 return S_OK;
4766 /*================== ITypeLib2 Implementation ===================================*/
4768 /******************************************************************************
4769 * ITypeLib2_QueryInterface {OLEAUT32}
4771 static HRESULT WINAPI ITypeLib2_fnQueryInterface(ITypeLib2 * iface, REFIID riid, LPVOID * ppv)
4773 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4775 return ICreateTypeLib2_QueryInterface((ICreateTypeLib2 *)This, riid, ppv);
4778 /******************************************************************************
4779 * ITypeLib2_AddRef {OLEAUT32}
4781 static ULONG WINAPI ITypeLib2_fnAddRef(ITypeLib2 * iface)
4783 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4785 return ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This);
4788 /******************************************************************************
4789 * ITypeLib2_Release {OLEAUT32}
4791 static ULONG WINAPI ITypeLib2_fnRelease(ITypeLib2 * iface)
4793 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4795 return ICreateTypeLib2_Release((ICreateTypeLib2 *)This);
4798 /******************************************************************************
4799 * ITypeLib2_GetTypeInfoCount {OLEAUT32}
4801 static UINT WINAPI ITypeLib2_fnGetTypeInfoCount(
4802 ITypeLib2 * iface)
4804 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4806 TRACE("(%p)\n", iface);
4808 return This->typelib_header.nrtypeinfos;
4811 /******************************************************************************
4812 * ITypeLib2_GetTypeInfo {OLEAUT32}
4814 static HRESULT WINAPI ITypeLib2_fnGetTypeInfo(
4815 ITypeLib2 * iface,
4816 UINT index,
4817 ITypeInfo** ppTInfo)
4819 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4821 TRACE("(%p,%d,%p)\n", iface, index, ppTInfo);
4823 if (!ppTInfo) return E_INVALIDARG;
4825 if (index >= This->typelib_header.nrtypeinfos) {
4826 return TYPE_E_ELEMENTNOTFOUND;
4829 return ctl2_find_typeinfo_from_offset(This, This->typelib_typeinfo_offsets[index], ppTInfo);
4832 /******************************************************************************
4833 * ITypeLib2_GetTypeInfoType {OLEAUT32}
4835 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoType(
4836 ITypeLib2 * iface,
4837 UINT index,
4838 TYPEKIND* kind)
4840 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4842 TRACE("(%p,%d,%p)\n", iface, index, kind);
4844 if (!kind) return E_INVALIDARG;
4846 if (index >= This->typelib_header.nrtypeinfos) {
4847 return TYPE_E_ELEMENTNOTFOUND;
4850 *kind = (This->typelib_segment_data[MSFT_SEG_TYPEINFO][This->typelib_typeinfo_offsets[index]]) & 0xF;
4852 return S_OK;
4855 /******************************************************************************
4856 * ITypeLib2_GetTypeInfoOfGuid {OLEAUT32}
4858 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoOfGuid(
4859 ITypeLib2 * iface,
4860 REFGUID guid,
4861 ITypeInfo** ppTinfo)
4863 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4865 int guidoffset;
4866 int typeinfo;
4868 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), ppTinfo);
4870 guidoffset = ctl2_find_guid(This, ctl2_hash_guid(guid), guid);
4871 if (guidoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
4873 typeinfo = ((MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][guidoffset])->hreftype;
4874 if (typeinfo < 0) return TYPE_E_ELEMENTNOTFOUND;
4876 return ctl2_find_typeinfo_from_offset(This, typeinfo, ppTinfo);
4879 /******************************************************************************
4880 * ITypeLib2_GetLibAttr {OLEAUT32}
4882 static HRESULT WINAPI ITypeLib2_fnGetLibAttr(
4883 ITypeLib2 * iface,
4884 TLIBATTR** ppTLibAttr)
4886 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4888 TRACE("(%p,%p)\n", This, ppTLibAttr);
4890 if(!ppTLibAttr)
4891 return E_INVALIDARG;
4893 *ppTLibAttr = heap_alloc_zero(sizeof(TLIBATTR));
4894 if(!*ppTLibAttr)
4895 return E_OUTOFMEMORY;
4897 if(This->typelib_header.posguid != -1) {
4898 MSFT_GuidEntry *guid;
4900 guid = (MSFT_GuidEntry*)&This->typelib_segment_data[MSFT_SEG_GUID][This->typelib_header.posguid];
4901 (*ppTLibAttr)->guid = guid->guid;
4904 (*ppTLibAttr)->lcid = This->typelib_header.lcid;
4905 (*ppTLibAttr)->syskind = ctl2_get_syskind(This);
4906 (*ppTLibAttr)->wMajorVerNum = LOWORD(This->typelib_header.version);
4907 (*ppTLibAttr)->wMinorVerNum = HIWORD(This->typelib_header.version);
4908 (*ppTLibAttr)->wLibFlags = This->typelib_header.flags;
4909 return S_OK;
4912 /******************************************************************************
4913 * ITypeLib2_GetTypeComp {OLEAUT32}
4915 static HRESULT WINAPI ITypeLib2_fnGetTypeComp(
4916 ITypeLib2 * iface,
4917 ITypeComp** ppTComp)
4919 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4921 FIXME("(%p,%p), stub!\n", This, ppTComp);
4923 return E_OUTOFMEMORY;
4926 /******************************************************************************
4927 * ITypeLib2_GetDocumentation {OLEAUT32}
4929 static HRESULT WINAPI ITypeLib2_fnGetDocumentation(
4930 ITypeLib2 * iface,
4931 INT index,
4932 BSTR* pBstrName,
4933 BSTR* pBstrDocString,
4934 DWORD* pdwHelpContext,
4935 BSTR* pBstrHelpFile)
4937 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4938 WCHAR *string;
4940 TRACE("(%p,%d,%p,%p,%p,%p)\n", This, index, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4942 if(index != -1) {
4943 ICreateTypeInfo2Impl *iter;
4945 for(iter=This->typeinfos; iter!=NULL && index!=0; iter=iter->next_typeinfo)
4946 index--;
4948 if(!iter)
4949 return TYPE_E_ELEMENTNOTFOUND;
4951 return ITypeInfo_GetDocumentation((ITypeInfo*)&iter->lpVtblTypeInfo2,
4952 -1, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4955 if(pBstrName) {
4956 if(This->typelib_header.NameOffset == -1)
4957 *pBstrName = NULL;
4958 else {
4959 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->
4960 typelib_segment_data[MSFT_SEG_NAME][This->typelib_header.NameOffset];
4962 ctl2_decode_name((char*)&name->namelen, &string);
4964 *pBstrName = SysAllocString(string);
4965 if(!*pBstrName)
4966 return E_OUTOFMEMORY;
4970 if(pBstrDocString) {
4971 if(This->typelib_header.helpstring == -1)
4972 *pBstrDocString = NULL;
4973 else {
4974 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpstring], &string);
4976 *pBstrDocString = SysAllocString(string);
4977 if(!*pBstrDocString) {
4978 if(pBstrName) SysFreeString(*pBstrName);
4979 return E_OUTOFMEMORY;
4984 if(pdwHelpContext)
4985 *pdwHelpContext = This->typelib_header.helpcontext;
4987 if(pBstrHelpFile) {
4988 if(This->typelib_header.helpfile == -1)
4989 *pBstrHelpFile = NULL;
4990 else {
4991 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpfile], &string);
4993 *pBstrHelpFile = SysAllocString(string);
4994 if(!*pBstrHelpFile) {
4995 if(pBstrName) SysFreeString(*pBstrName);
4996 if(pBstrDocString) SysFreeString(*pBstrDocString);
4997 return E_OUTOFMEMORY;
5002 return S_OK;
5005 /******************************************************************************
5006 * ITypeLib2_IsName {OLEAUT32}
5008 static HRESULT WINAPI ITypeLib2_fnIsName(
5009 ITypeLib2 * iface,
5010 LPOLESTR szNameBuf,
5011 ULONG lHashVal,
5012 BOOL* pfName)
5014 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5016 char *encoded_name;
5017 int nameoffset;
5018 MSFT_NameIntro *nameintro;
5020 TRACE("(%p,%s,%x,%p)\n", iface, debugstr_w(szNameBuf), lHashVal, pfName);
5022 ctl2_encode_name(This, szNameBuf, &encoded_name);
5023 nameoffset = ctl2_find_name(This, encoded_name);
5025 *pfName = 0;
5027 if (nameoffset == -1) return S_OK;
5029 nameintro = (MSFT_NameIntro *)(&This->typelib_segment_data[MSFT_SEG_NAME][nameoffset]);
5030 if (nameintro->hreftype == -1) return S_OK;
5032 *pfName = 1;
5034 FIXME("Should be decoding our copy of the name over szNameBuf.\n");
5036 return S_OK;
5039 /******************************************************************************
5040 * ITypeLib2_FindName {OLEAUT32}
5042 static HRESULT WINAPI ITypeLib2_fnFindName(
5043 ITypeLib2 * iface,
5044 LPOLESTR szNameBuf,
5045 ULONG lHashVal,
5046 ITypeInfo** ppTInfo,
5047 MEMBERID* rgMemId,
5048 USHORT* pcFound)
5050 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5052 FIXME("(%p,%s,%x,%p,%p,%p), stub!\n", This, debugstr_w(szNameBuf), lHashVal, ppTInfo, rgMemId, pcFound);
5054 return E_OUTOFMEMORY;
5057 /******************************************************************************
5058 * ITypeLib2_ReleaseTLibAttr {OLEAUT32}
5060 static void WINAPI ITypeLib2_fnReleaseTLibAttr(
5061 ITypeLib2 * iface,
5062 TLIBATTR* attr)
5064 TRACE("(%p,%p)\n", iface, attr);
5065 heap_free(attr);
5068 /******************************************************************************
5069 * ICreateTypeLib2_GetCustData {OLEAUT32}
5071 * Retrieves a custom data value stored on a type library.
5073 * RETURNS
5075 * Success: S_OK
5076 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5078 static HRESULT WINAPI ITypeLib2_fnGetCustData(
5079 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5080 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
5081 VARIANT* pVarVal) /* [O] The custom data. */
5083 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5085 FIXME("(%p,%s,%p), stub!\n", This, debugstr_guid(guid), pVarVal);
5087 return E_OUTOFMEMORY;
5090 /******************************************************************************
5091 * ICreateTypeLib2_GetLibStatistics {OLEAUT32}
5093 * Retrieves some statistics about names in a type library, supposedly for
5094 * hash table optimization purposes.
5096 * RETURNS
5098 * Success: S_OK
5099 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5101 static HRESULT WINAPI ITypeLib2_fnGetLibStatistics(
5102 ITypeLib2 * iface, /* [I] The type library to get statistics about. */
5103 ULONG* pcUniqueNames, /* [O] The number of unique names in the type library. */
5104 ULONG* pcchUniqueNames) /* [O] The number of changed (?) characters in names in the type library. */
5106 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5108 FIXME("(%p,%p,%p), stub!\n", This, pcUniqueNames, pcchUniqueNames);
5110 return E_OUTOFMEMORY;
5113 /******************************************************************************
5114 * ICreateTypeLib2_GetDocumentation2 {OLEAUT32}
5116 * Obtain locale-aware help string information.
5118 * RETURNS
5120 * Success: S_OK
5121 * Failure: STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
5123 static HRESULT WINAPI ITypeLib2_fnGetDocumentation2(
5124 ITypeLib2 * iface,
5125 INT index,
5126 LCID lcid,
5127 BSTR* pbstrHelpString,
5128 DWORD* pdwHelpStringContext,
5129 BSTR* pbstrHelpStringDll)
5131 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5133 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", This, index, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
5135 return E_OUTOFMEMORY;
5138 /******************************************************************************
5139 * ICreateTypeLib2_GetAllCustData {OLEAUT32}
5141 * Retrieve all of the custom data for a type library.
5143 * RETURNS
5145 * Success: S_OK
5146 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5148 static HRESULT WINAPI ITypeLib2_fnGetAllCustData(
5149 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5150 CUSTDATA* pCustData) /* [O] The structure in which to place the custom data. */
5152 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5154 FIXME("(%p,%p), stub!\n", This, pCustData);
5156 return E_OUTOFMEMORY;
5160 /*================== ICreateTypeLib2 & ITypeLib2 VTABLEs And Creation ===================================*/
5162 static const ICreateTypeLib2Vtbl ctypelib2vt =
5165 ICreateTypeLib2_fnQueryInterface,
5166 ICreateTypeLib2_fnAddRef,
5167 ICreateTypeLib2_fnRelease,
5169 ICreateTypeLib2_fnCreateTypeInfo,
5170 ICreateTypeLib2_fnSetName,
5171 ICreateTypeLib2_fnSetVersion,
5172 ICreateTypeLib2_fnSetGuid,
5173 ICreateTypeLib2_fnSetDocString,
5174 ICreateTypeLib2_fnSetHelpFileName,
5175 ICreateTypeLib2_fnSetHelpContext,
5176 ICreateTypeLib2_fnSetLcid,
5177 ICreateTypeLib2_fnSetLibFlags,
5178 ICreateTypeLib2_fnSaveAllChanges,
5180 ICreateTypeLib2_fnDeleteTypeInfo,
5181 ICreateTypeLib2_fnSetCustData,
5182 ICreateTypeLib2_fnSetHelpStringContext,
5183 ICreateTypeLib2_fnSetHelpStringDll
5186 static const ITypeLib2Vtbl typelib2vt =
5189 ITypeLib2_fnQueryInterface,
5190 ITypeLib2_fnAddRef,
5191 ITypeLib2_fnRelease,
5193 ITypeLib2_fnGetTypeInfoCount,
5194 ITypeLib2_fnGetTypeInfo,
5195 ITypeLib2_fnGetTypeInfoType,
5196 ITypeLib2_fnGetTypeInfoOfGuid,
5197 ITypeLib2_fnGetLibAttr,
5198 ITypeLib2_fnGetTypeComp,
5199 ITypeLib2_fnGetDocumentation,
5200 ITypeLib2_fnIsName,
5201 ITypeLib2_fnFindName,
5202 ITypeLib2_fnReleaseTLibAttr,
5204 ITypeLib2_fnGetCustData,
5205 ITypeLib2_fnGetLibStatistics,
5206 ITypeLib2_fnGetDocumentation2,
5207 ITypeLib2_fnGetAllCustData,
5210 static ICreateTypeLib2 *ICreateTypeLib2_Constructor(SYSKIND syskind, LPCOLESTR szFile)
5212 ICreateTypeLib2Impl *pCreateTypeLib2Impl;
5213 int failed = 0;
5215 TRACE("Constructing ICreateTypeLib2 (%d, %s)\n", syskind, debugstr_w(szFile));
5217 pCreateTypeLib2Impl = heap_alloc_zero(sizeof(ICreateTypeLib2Impl));
5218 if (!pCreateTypeLib2Impl) return NULL;
5220 pCreateTypeLib2Impl->filename = heap_alloc((strlenW(szFile) + 1) * sizeof(WCHAR));
5221 if (!pCreateTypeLib2Impl->filename) {
5222 heap_free(pCreateTypeLib2Impl);
5223 return NULL;
5225 strcpyW(pCreateTypeLib2Impl->filename, szFile);
5227 ctl2_init_header(pCreateTypeLib2Impl);
5228 ctl2_init_segdir(pCreateTypeLib2Impl);
5230 pCreateTypeLib2Impl->typelib_header.varflags |= syskind;
5233 * The following two calls return an offset or -1 if out of memory. We
5234 * specifically need an offset of 0, however, so...
5236 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
5237 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
5239 pCreateTypeLib2Impl->typelib_guidhash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_GUIDHASH];
5240 pCreateTypeLib2Impl->typelib_namehash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_NAMEHASH];
5242 memset(pCreateTypeLib2Impl->typelib_guidhash_segment, 0xff, 0x80);
5243 memset(pCreateTypeLib2Impl->typelib_namehash_segment, 0xff, 0x200);
5245 pCreateTypeLib2Impl->lpVtbl = &ctypelib2vt;
5246 pCreateTypeLib2Impl->lpVtblTypeLib2 = &typelib2vt;
5247 pCreateTypeLib2Impl->ref = 1;
5249 if (failed) {
5250 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)pCreateTypeLib2Impl);
5251 return 0;
5254 return (ICreateTypeLib2 *)pCreateTypeLib2Impl;
5257 /******************************************************************************
5258 * CreateTypeLib2 [OLEAUT32.180]
5260 * Obtains an ICreateTypeLib2 object for creating a new-style (MSFT) type
5261 * library.
5263 * NOTES
5265 * See also CreateTypeLib.
5267 * RETURNS
5268 * Success: S_OK
5269 * Failure: Status
5271 HRESULT WINAPI CreateTypeLib2(
5272 SYSKIND syskind, /* [I] System type library is for */
5273 LPCOLESTR szFile, /* [I] Type library file name */
5274 ICreateTypeLib2** ppctlib) /* [O] Storage for object returned */
5276 TRACE("(%d,%s,%p)\n", syskind, debugstr_w(szFile), ppctlib);
5278 if (!szFile) return E_INVALIDARG;
5279 *ppctlib = ICreateTypeLib2_Constructor(syskind, szFile);
5280 return (*ppctlib)? S_OK: E_OUTOFMEMORY;
5283 /******************************************************************************
5284 * ClearCustData (OLEAUT32.171)
5286 * Clear a custom data types' data.
5288 * PARAMS
5289 * lpCust [I] The custom data type instance
5291 * RETURNS
5292 * Nothing.
5294 void WINAPI ClearCustData(LPCUSTDATA lpCust)
5296 if (lpCust && lpCust->cCustData)
5298 if (lpCust->prgCustData)
5300 DWORD i;
5302 for (i = 0; i < lpCust->cCustData; i++)
5303 VariantClear(&lpCust->prgCustData[i].varValue);
5305 /* FIXME - Should be using a per-thread IMalloc */
5306 heap_free(lpCust->prgCustData);
5307 lpCust->prgCustData = NULL;
5309 lpCust->cCustData = 0;