oleaut32: COM Cleanup ICreateTypeLib2Imp.
[wine.git] / dlls / oleaut32 / typelib2.c
blob3cbd4b580b9b3fffaaa751d2ce5a5c9bb0c599dd
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 ICreateTypeInfo2 ICreateTypeInfo2_iface;
199 ITypeInfo2 ITypeInfo2_iface;
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_ICreateTypeInfo2(ICreateTypeInfo2 *iface)
217 return CONTAINING_RECORD(iface, ICreateTypeInfo2Impl, ICreateTypeInfo2_iface);
220 static inline ICreateTypeInfo2Impl *impl_from_ITypeInfo2( ITypeInfo2 *iface )
222 return CONTAINING_RECORD(iface, ICreateTypeInfo2Impl, ITypeInfo2_iface);
225 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface);
227 static CyclicList *alloc_cyclic_list_item(CyclicListElementType type)
229 CyclicList *ret = heap_alloc_zero(sizeof(CyclicList));
230 if (!ret)
231 return NULL;
232 ret->type = type;
233 return ret;
236 /*================== Internal functions ===================================*/
238 static inline UINT cti2_get_var_count(const MSFT_TypeInfoBase *typeinfo)
240 return typeinfo->cElement >> 16;
243 static inline UINT cti2_get_func_count(const MSFT_TypeInfoBase *typeinfo)
245 return typeinfo->cElement & 0xFFFF;
248 static inline INT ctl2_get_record_size(const CyclicList *iter)
250 return iter->u.data[0] & 0xFFFF;
253 static void ctl2_update_var_size(const ICreateTypeInfo2Impl *This, CyclicList *var, int size)
255 int old = ctl2_get_record_size(var), i;
257 if (old >= size) return;
259 /* initialize fields included in size but currently unused */
260 for (i = old/sizeof(int); i < (size/sizeof(int) - 1); i++)
262 /* HelpContext/HelpStringContext being 0 means it's not set */
263 var->u.data[i] = (i == 5 || i == 9) ? 0 : -1;
266 var->u.data[0] += size - old;
267 This->typedata->next->u.val += size - old;
270 /* NOTE: entry always assumed to be a function */
271 static inline INVOKEKIND ctl2_get_invokekind(const CyclicList *func)
273 /* INVOKEKIND uses bit flags up to 8 */
274 return (func->u.data[4] >> 3) & 0xF;
277 static inline SYSKIND ctl2_get_syskind(const ICreateTypeLib2Impl *This)
279 return This->typelib_header.varflags & 0xF;
282 /****************************************************************************
283 * ctl2_init_header
285 * Initializes the type library header of a new typelib.
287 static void ctl2_init_header(
288 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
290 This->typelib_header.magic1 = MSFT_SIGNATURE;
291 This->typelib_header.magic2 = 0x00010002;
292 This->typelib_header.posguid = -1;
293 This->typelib_header.lcid = This->typelib_header.lcid2 = GetUserDefaultLCID();
294 This->typelib_header.varflags = 0x40;
295 This->typelib_header.version = 0;
296 This->typelib_header.flags = 0;
297 This->typelib_header.nrtypeinfos = 0;
298 This->typelib_header.helpstring = -1;
299 This->typelib_header.helpstringcontext = 0;
300 This->typelib_header.helpcontext = 0;
301 This->typelib_header.nametablecount = 0;
302 This->typelib_header.nametablechars = 0;
303 This->typelib_header.NameOffset = -1;
304 This->typelib_header.helpfile = -1;
305 This->typelib_header.CustomDataOffset = -1;
306 This->typelib_header.res44 = 0x20;
307 This->typelib_header.res48 = 0x80;
308 This->typelib_header.dispatchpos = -1;
309 This->typelib_header.nimpinfos = 0;
310 This->helpStringDll = -1;
313 /****************************************************************************
314 * ctl2_init_segdir
316 * Initializes the segment directory of a new typelib.
318 static void ctl2_init_segdir(
319 ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
321 int i;
322 MSFT_pSeg *segdir;
324 segdir = &This->typelib_segdir[MSFT_SEG_TYPEINFO];
326 for (i = 0; i < 15; i++) {
327 segdir[i].offset = -1;
328 segdir[i].length = 0;
329 segdir[i].res08 = -1;
330 segdir[i].res0c = 0x0f;
334 /****************************************************************************
335 * ctl2_hash_guid
337 * Generates a hash key from a GUID.
339 * RETURNS
341 * The hash key for the GUID.
343 static int ctl2_hash_guid(
344 REFGUID guid) /* [I] The guid to find. */
346 int hash;
347 int i;
349 hash = 0;
350 for (i = 0; i < 8; i ++) {
351 hash ^= ((const short *)guid)[i];
354 return hash & 0x1f;
357 /****************************************************************************
358 * ctl2_find_guid
360 * Locates a guid in a type library.
362 * RETURNS
364 * The offset into the GUID segment of the guid, or -1 if not found.
366 static int ctl2_find_guid(
367 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
368 int hash_key, /* [I] The hash key for the guid. */
369 REFGUID guid) /* [I] The guid to find. */
371 int offset;
372 MSFT_GuidEntry *guidentry;
374 offset = This->typelib_guidhash_segment[hash_key];
375 while (offset != -1) {
376 guidentry = (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][offset];
378 if (IsEqualGUID(guidentry, guid)) return offset;
380 offset = guidentry->next_hash;
383 return offset;
386 /****************************************************************************
387 * ctl2_find_name
389 * Locates a name in a type library.
391 * RETURNS
393 * The offset into the NAME segment of the name, or -1 if not found.
395 * NOTES
397 * The name must be encoded as with ctl2_encode_name().
399 static int ctl2_find_name(
400 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
401 const char *name) /* [I] The encoded name to find. */
403 int offset;
404 int *namestruct;
406 offset = This->typelib_namehash_segment[name[2] & 0x7f];
407 while (offset != -1) {
408 namestruct = (int *)&This->typelib_segment_data[MSFT_SEG_NAME][offset];
410 if (!((namestruct[2] ^ *((const int *)name)) & 0xffff00ff)) {
411 /* hash codes and lengths match, final test */
412 if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
415 /* move to next item in hash bucket */
416 offset = namestruct[1];
419 return offset;
422 /****************************************************************************
423 * ctl2_encode_name
425 * Encodes a name string to a form suitable for storing into a type library
426 * or comparing to a name stored in a type library.
428 * RETURNS
430 * The length of the encoded name, including padding and length+hash fields.
432 * NOTES
434 * Will throw an exception if name or result are NULL. Is not multithread
435 * safe in the slightest.
437 static int ctl2_encode_name(
438 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (used for LCID only). */
439 const WCHAR *name, /* [I] The name string to encode. */
440 char **result) /* [O] A pointer to a pointer to receive the encoded name. */
442 int length;
443 static char converted_name[0x104];
444 int offset;
445 int value;
447 length = WideCharToMultiByte(CP_ACP, 0, name, strlenW(name), converted_name+4, 0x100, NULL, NULL);
448 converted_name[0] = length & 0xff;
450 converted_name[length + 4] = 0;
452 converted_name[1] = 0x00;
454 value = LHashValOfNameSysA(ctl2_get_syskind(This), This->typelib_header.lcid, converted_name + 4);
456 converted_name[2] = value;
457 converted_name[3] = value >> 8;
459 for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
461 *result = converted_name;
463 return (length + 7) & ~3;
466 /****************************************************************************
467 * ctl2_decode_name
469 * Converts string stored in typelib data to unicode.
471 static void ctl2_decode_name(
472 char *data, /* [I] String to be decoded */
473 WCHAR **string) /* [O] Decoded string */
475 int i, length;
476 static WCHAR converted_string[0x104];
478 length = data[0];
480 for(i=0; i<length; i++)
481 converted_string[i] = data[i+4];
482 converted_string[length] = '\0';
484 *string = converted_string;
487 /****************************************************************************
488 * ctl2_encode_string
490 * Encodes a string to a form suitable for storing into a type library or
491 * comparing to a string stored in a type library.
493 * RETURNS
495 * The length of the encoded string, including padding and length fields.
497 * NOTES
499 * Will throw an exception if string or result are NULL. Is not multithread
500 * safe in the slightest.
502 static int ctl2_encode_string(
503 ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (not used?). */
504 const WCHAR *string, /* [I] The string to encode. */
505 char **result) /* [O] A pointer to a pointer to receive the encoded string. */
507 int length;
508 static char converted_string[0x104];
509 int offset;
511 length = WideCharToMultiByte(CP_ACP, 0, string, strlenW(string), converted_string+2, 0x102, NULL, NULL);
512 converted_string[0] = length & 0xff;
513 converted_string[1] = (length >> 8) & 0xff;
515 for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
517 *result = converted_string;
519 return (length + 5) & ~3;
522 /****************************************************************************
523 * ctl2_decode_string
525 * Converts string stored in typelib data to unicode.
527 static void ctl2_decode_string(
528 unsigned char *data,/* [I] String to be decoded */
529 WCHAR **string) /* [O] Decoded string */
531 int i, length;
532 static WCHAR converted_string[0x104];
534 length = data[0] + (data[1]<<8);
535 if((length&0x3) == 1)
536 length >>= 2;
538 for(i=0; i<length; i++)
539 converted_string[i] = data[i+2];
540 converted_string[length] = '\0';
542 *string = converted_string;
545 /****************************************************************************
546 * ctl2_alloc_segment
548 * Allocates memory from a segment in a type library.
550 * RETURNS
552 * Success: The offset within the segment of the new data area.
553 * Failure: -1 (this is invariably an out of memory condition).
555 * BUGS
557 * Does not (yet) handle the case where the allocated segment memory needs to grow.
559 static int ctl2_alloc_segment(
560 ICreateTypeLib2Impl *This, /* [I] The type library in which to allocate. */
561 enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
562 int size, /* [I] The amount to allocate. */
563 int block_size) /* [I] Initial allocation block size, or 0 for default. */
565 int offset;
567 if(!This->typelib_segment_data[segment]) {
568 if (!block_size) block_size = 0x2000;
570 This->typelib_segment_block_length[segment] = block_size;
571 This->typelib_segment_data[segment] = heap_alloc(block_size);
572 if (!This->typelib_segment_data[segment]) return -1;
573 memset(This->typelib_segment_data[segment], 0x57, block_size);
576 while ((This->typelib_segdir[segment].length + size) > This->typelib_segment_block_length[segment]) {
577 unsigned char *block;
579 block_size = This->typelib_segment_block_length[segment];
580 block = heap_realloc(This->typelib_segment_data[segment], block_size << 1);
581 if (!block) return -1;
583 if (segment == MSFT_SEG_TYPEINFO) {
584 /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
585 ICreateTypeInfo2Impl *typeinfo;
587 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
588 typeinfo->typeinfo = (void *)&block[((unsigned char *)typeinfo->typeinfo) - This->typelib_segment_data[segment]];
592 memset(block + block_size, 0x57, block_size);
593 This->typelib_segment_block_length[segment] = block_size << 1;
594 This->typelib_segment_data[segment] = block;
597 offset = This->typelib_segdir[segment].length;
598 This->typelib_segdir[segment].length += size;
600 return offset;
603 /****************************************************************************
604 * ctl2_alloc_typeinfo
606 * Allocates and initializes a typeinfo structure in a type library.
608 * RETURNS
610 * Success: The offset of the new typeinfo.
611 * Failure: -1 (this is invariably an out of memory condition).
613 static int ctl2_alloc_typeinfo(
614 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
615 int nameoffset) /* [I] The offset of the name for this typeinfo. */
617 int offset;
618 MSFT_TypeInfoBase *typeinfo;
620 offset = ctl2_alloc_segment(This, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
621 if (offset == -1) return -1;
623 This->typelib_typeinfo_offsets[This->typelib_header.nrtypeinfos++] = offset;
625 typeinfo = (void *)(This->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
627 typeinfo->typekind = (This->typelib_header.nrtypeinfos - 1) << 16;
628 typeinfo->memoffset = -1; /* should be EOF if no elements */
629 typeinfo->res2 = 0;
630 typeinfo->res3 = 0;
631 typeinfo->res4 = 3;
632 typeinfo->res5 = 0;
633 typeinfo->cElement = 0;
634 typeinfo->res7 = 0;
635 typeinfo->res8 = 0;
636 typeinfo->res9 = 0;
637 typeinfo->resA = 0;
638 typeinfo->posguid = -1;
639 typeinfo->flags = 0;
640 typeinfo->NameOffset = nameoffset;
641 typeinfo->version = 0;
642 typeinfo->docstringoffs = -1;
643 typeinfo->helpstringcontext = 0;
644 typeinfo->helpcontext = 0;
645 typeinfo->oCustData = -1;
646 typeinfo->cbSizeVft = 0;
647 typeinfo->cImplTypes = 0;
648 typeinfo->size = 0;
649 typeinfo->datatype1 = -1;
650 typeinfo->datatype2 = 0;
651 typeinfo->res18 = 0;
652 typeinfo->res19 = -1;
654 return offset;
657 /****************************************************************************
658 * ctl2_alloc_guid
660 * Allocates and initializes a GUID structure in a type library. Also updates
661 * the GUID hash table as needed.
663 * RETURNS
665 * Success: The offset of the new GUID.
666 * Failure: -1 (this is invariably an out of memory condition).
668 static int ctl2_alloc_guid(
669 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
670 MSFT_GuidEntry *guid) /* [I] The GUID to store. */
672 int offset;
673 MSFT_GuidEntry *guid_space;
674 int hash_key;
676 hash_key = ctl2_hash_guid(&guid->guid);
678 offset = ctl2_find_guid(This, hash_key, &guid->guid);
679 if (offset != -1) return offset;
681 offset = ctl2_alloc_segment(This, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
682 if (offset == -1) return -1;
684 guid_space = (void *)(This->typelib_segment_data[MSFT_SEG_GUID] + offset);
685 *guid_space = *guid;
687 guid_space->next_hash = This->typelib_guidhash_segment[hash_key];
688 This->typelib_guidhash_segment[hash_key] = offset;
690 return offset;
693 /****************************************************************************
694 * ctl2_alloc_name
696 * Allocates and initializes a name within a type library. Also updates the
697 * name hash table as needed.
699 * RETURNS
701 * Success: The offset within the segment of the new name.
702 * Failure: -1 (this is invariably an out of memory condition).
704 static int ctl2_alloc_name(
705 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
706 const WCHAR *name) /* [I] The name to store. */
708 int length;
709 int offset;
710 MSFT_NameIntro *name_space;
711 char *encoded_name;
713 length = ctl2_encode_name(This, name, &encoded_name);
715 offset = ctl2_find_name(This, encoded_name);
716 if (offset != -1) return offset;
718 offset = ctl2_alloc_segment(This, MSFT_SEG_NAME, length + 8, 0);
719 if (offset == -1) return -1;
721 name_space = (void *)(This->typelib_segment_data[MSFT_SEG_NAME] + offset);
722 name_space->hreftype = -1;
723 name_space->next_hash = -1;
724 memcpy(&name_space->namelen, encoded_name, length);
726 if (This->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
727 name_space->next_hash = This->typelib_namehash_segment[encoded_name[2] & 0x7f];
729 This->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
731 This->typelib_header.nametablecount += 1;
732 This->typelib_header.nametablechars += *encoded_name;
734 return offset;
737 /****************************************************************************
738 * ctl2_alloc_string
740 * Allocates and initializes a string in a type library.
742 * RETURNS
744 * Success: The offset within the segment of the new string.
745 * Failure: -1 (this is invariably an out of memory condition).
747 static int ctl2_alloc_string(
748 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
749 const WCHAR *string) /* [I] The string to store. */
751 int length;
752 int offset;
753 unsigned char *string_space;
754 char *encoded_string;
756 length = ctl2_encode_string(This, string, &encoded_string);
758 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_STRING].length;
759 offset += (((This->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) |
760 This->typelib_segment_data[MSFT_SEG_STRING][offset + 0]) + 5) & ~3) {
761 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
764 offset = ctl2_alloc_segment(This, MSFT_SEG_STRING, length, 0);
765 if (offset == -1) return -1;
767 string_space = This->typelib_segment_data[MSFT_SEG_STRING] + offset;
768 memcpy(string_space, encoded_string, length);
770 return offset;
773 /****************************************************************************
774 * ctl2_alloc_importinfo
776 * Allocates and initializes an import information structure in a type library.
778 * RETURNS
780 * Success: The offset of the new importinfo.
781 * Failure: -1 (this is invariably an out of memory condition).
783 static int ctl2_alloc_importinfo(
784 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
785 MSFT_ImpInfo *impinfo) /* [I] The import information to store. */
787 int offset;
788 MSFT_ImpInfo *impinfo_space;
790 impinfo_space = (MSFT_ImpInfo*)&This->typelib_segment_data[MSFT_SEG_IMPORTINFO][0];
791 for (offset=0; offset<This->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
792 offset+=sizeof(MSFT_ImpInfo)) {
793 if(impinfo_space->oImpFile == impinfo->oImpFile
794 && impinfo_space->oGuid == impinfo->oGuid)
795 return offset;
797 impinfo_space += 1;
800 impinfo->flags |= This->typelib_header.nimpinfos++;
802 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
803 if (offset == -1) return -1;
805 impinfo_space = (void *)(This->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
806 *impinfo_space = *impinfo;
808 return offset;
811 /****************************************************************************
812 * ctl2_alloc_importfile
814 * Allocates and initializes an import file definition in a type library.
816 * RETURNS
818 * Success: The offset of the new importinfo.
819 * Failure: -1 (this is invariably an out of memory condition).
821 static int ctl2_alloc_importfile(
822 ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
823 int guidoffset, /* [I] The offset to the GUID for the imported library. */
824 LCID lcid, /* [I] The LCID of imported library. */
825 int major_version, /* [I] The major version number of the imported library. */
826 int minor_version, /* [I] The minor version number of the imported library. */
827 const WCHAR *filename) /* [I] The filename of the imported library. */
829 int length;
830 int offset;
831 MSFT_ImpFile *importfile;
832 char *encoded_string;
834 length = ctl2_encode_string(This, filename, &encoded_string);
836 encoded_string[0] <<= 2;
837 encoded_string[0] |= 1;
839 for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
840 offset += (((((This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) |
841 This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc]) >> 2) + 5) & 0xfffc) + 0xc) {
842 if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
845 offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
846 if (offset == -1) return -1;
848 importfile = (MSFT_ImpFile *)&This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
849 importfile->guid = guidoffset;
850 importfile->lcid = lcid;
851 importfile->version = major_version | (minor_version << 16);
852 memcpy(importfile->filename, encoded_string, length);
854 return offset;
857 /****************************************************************************
858 * ctl2_encode_variant
860 * Encodes a variant, inline if possible or in custom data segment
862 * RETURNS
864 * Success: S_OK
865 * Failure: Error code from winerror.h
867 static HRESULT ctl2_encode_variant(
868 ICreateTypeLib2Impl *This, /* [I] The typelib to allocate data in */
869 int *encoded_value, /* [O] The encoded default value or data offset */
870 VARIANT *value, /* [I] Default value to be encoded */
871 VARTYPE arg_type) /* [I] Argument type */
873 VARIANT v;
874 HRESULT hres;
875 int mask = 0;
877 TRACE("%p %d %d\n", This, V_VT(value), arg_type);
879 if(arg_type == VT_INT)
880 arg_type = VT_I4;
881 if(arg_type == VT_UINT)
882 arg_type = VT_UI4;
884 v = *value;
885 if(V_VT(value) != arg_type) {
886 hres = VariantChangeType(&v, value, 0, arg_type);
887 if(FAILED(hres))
888 return hres;
891 /* Check if default value can be stored in encoded_value */
892 switch(arg_type) {
893 case VT_I4:
894 case VT_UI4:
895 mask = 0x3ffffff;
896 if(V_UI4(&v)>0x3ffffff)
897 break;
898 case VT_I1:
899 case VT_UI1:
900 case VT_BOOL:
901 if(!mask)
902 mask = 0xff;
903 case VT_I2:
904 case VT_UI2:
905 if(!mask)
906 mask = 0xffff;
907 *encoded_value = (V_UI4(&v)&mask) | ((0x80+0x4*arg_type)<<24);
908 return S_OK;
911 switch(arg_type) {
912 case VT_I4:
913 case VT_R4:
914 case VT_UI4:
915 case VT_INT:
916 case VT_UINT:
917 case VT_HRESULT:
918 case VT_PTR: {
919 /* Construct the data to be allocated */
920 int data[2];
921 data[0] = arg_type + (V_UI4(&v)<<16);
922 data[1] = (V_UI4(&v)>>16) + 0x57570000;
924 /* Check if the data was already allocated */
925 /* Currently the structures doesn't allow to do it in a nice way */
926 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-8; *encoded_value+=4)
927 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8))
928 return S_OK;
930 /* Allocate the data */
931 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
932 if(*encoded_value == -1)
933 return E_OUTOFMEMORY;
935 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8);
936 return S_OK;
938 case VT_BSTR: {
939 /* Construct the data */
940 int i, len = (6+SysStringLen(V_BSTR(&v))+3) & ~0x3;
941 char *data = heap_alloc(len);
943 if(!data)
944 return E_OUTOFMEMORY;
946 *((unsigned short*)data) = arg_type;
947 *((unsigned*)(data+2)) = SysStringLen(V_BSTR(&v));
948 for(i=0; i<SysStringLen(V_BSTR(&v)); i++) {
949 if(V_BSTR(&v)[i] <= 0x7f)
950 data[i+6] = V_BSTR(&v)[i];
951 else
952 data[i+6] = '?';
954 WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), SysStringLen(V_BSTR(&v)), &data[6], len-6, NULL, NULL);
955 for(i=6+SysStringLen(V_BSTR(&v)); i<len; i++)
956 data[i] = 0x57;
958 /* Check if the data was already allocated */
959 for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-len; *encoded_value+=4)
960 if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len)) {
961 heap_free(data);
962 return S_OK;
965 /* Allocate the data */
966 *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, len, 0);
967 if(*encoded_value == -1) {
968 heap_free(data);
969 return E_OUTOFMEMORY;
972 memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len);
973 heap_free(data);
974 return S_OK;
976 default:
977 FIXME("Argument type not yet handled\n");
978 return E_NOTIMPL;
982 static int ctl2_find_custdata(
983 ICreateTypeLib2Impl *This,
984 REFGUID guid,
985 int offset)
987 while (offset != -1) {
988 MSFT_CDGuid *cdentry =
989 (MSFT_CDGuid *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
990 MSFT_GuidEntry *guidentry =
991 (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][cdentry->GuidOffset];
993 if (IsEqualGUID(guidentry, guid))
994 return offset;
996 offset = cdentry->next;
999 return -1;
1002 /****************************************************************************
1003 * ctl2_decode_variant
1005 * Decodes a variant
1007 * RETURNS
1009 * Success: S_OK
1010 * Failure: Error code from winerror.h
1012 static HRESULT ctl2_decode_variant(
1013 ICreateTypeLib2Impl *This, /* [I] The typelib that contains the variant */
1014 int data_offs, /* [I] Offset within the data array, or the encoded value itself */
1015 VARIANT *value) /* [O] Decoded value */
1017 unsigned char *encoded_data;
1018 VARTYPE type;
1020 if (data_offs & 0x80000000) {
1021 /* data_offs contains the encoded value */
1022 V_VT(value) = (data_offs & ~0x80000000) >> 26;
1023 V_UI4(value) = data_offs & ~0xFF000000;
1024 return S_OK;
1027 encoded_data = &This->typelib_segment_data[MSFT_SEG_CUSTDATA][data_offs];
1028 type = *encoded_data;
1030 switch(type) {
1031 case VT_I4:
1032 case VT_R4:
1033 case VT_UI4:
1034 case VT_INT:
1035 case VT_UINT:
1036 case VT_HRESULT:
1037 case VT_PTR: {
1038 V_VT(value) = type;
1039 V_UI4(value) = *(unsigned*)(encoded_data + 2);
1040 return S_OK;
1042 case VT_BSTR: {
1043 unsigned len, i;
1045 len = *(unsigned*)(encoded_data + 2);
1047 V_VT(value) = type;
1048 V_BSTR(value) = SysAllocStringByteLen(NULL, len * sizeof(OLECHAR));
1049 for (i = 0; i < len; ++i)
1050 V_BSTR(value)[i] = *(encoded_data + 6 + i);
1052 return S_OK;
1054 default:
1055 FIXME("Don't yet have decoder for this VARTYPE: %u\n", type);
1056 return E_NOTIMPL;
1060 /****************************************************************************
1061 * ctl2_set_custdata
1063 * Adds a custom data element to an object in a type library.
1065 * RETURNS
1067 * Success: S_OK.
1068 * Failure: One of E_INVALIDARG or E_OUTOFMEMORY.
1070 static HRESULT ctl2_set_custdata(
1071 ICreateTypeLib2Impl *This, /* [I] The type library to store the custom data in. */
1072 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
1073 VARIANT *pVarVal, /* [I] The custom data itself. */
1074 int *offset) /* [I/O] The list of custom data to prepend to. */
1076 MSFT_GuidEntry guidentry;
1077 HRESULT status;
1078 int dataoffset;
1079 int guidoffset;
1080 int custoffset;
1081 int *custdata;
1082 BOOL new_segment = FALSE;
1084 switch(V_VT(pVarVal))
1086 case VT_I4:
1087 case VT_R4:
1088 case VT_UI4:
1089 case VT_INT:
1090 case VT_UINT:
1091 case VT_HRESULT:
1092 case VT_BSTR:
1093 /* empty */
1094 break;
1095 default:
1096 return DISP_E_BADVARTYPE;
1099 guidentry.guid = *guid;
1101 guidentry.hreftype = -1;
1102 guidentry.next_hash = -1;
1104 guidoffset = ctl2_alloc_guid(This, &guidentry);
1105 if (guidoffset == -1) return E_OUTOFMEMORY;
1107 status = ctl2_encode_variant(This, &dataoffset, pVarVal, V_VT(pVarVal));
1108 if (status)
1109 return status;
1111 custoffset = ctl2_find_custdata(This, guid, *offset);
1112 if (custoffset == -1) {
1113 custoffset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATAGUID, 12, 0);
1114 if (custoffset == -1)
1115 return E_OUTOFMEMORY;
1116 new_segment = TRUE;
1119 custdata = (int *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
1120 custdata[0] = guidoffset;
1121 custdata[1] = dataoffset;
1122 if (new_segment) {
1123 custdata[2] = *offset;
1124 *offset = custoffset;
1127 return S_OK;
1130 /****************************************************************************
1131 * ctl2_encode_typedesc
1133 * Encodes a type description, storing information in the TYPEDESC and ARRAYDESC
1134 * segments as needed.
1136 * RETURNS
1138 * Success: 0.
1139 * Failure: -1.
1141 static int ctl2_encode_typedesc(
1142 ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the TYPEDESC. */
1143 const TYPEDESC *tdesc, /* [I] The type description to encode. */
1144 int *encoded_tdesc, /* [O] The encoded type description. */
1145 int *width, /* [O] The width of the type, or NULL. */
1146 int *alignment, /* [O] The alignment of the type, or NULL. */
1147 int *decoded_size) /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
1149 int default_tdesc;
1150 int scratch;
1151 int typeoffset;
1152 int arrayoffset;
1153 int *typedata;
1154 int *arraydata;
1155 int target_type;
1156 int child_size;
1158 default_tdesc = 0x80000000 | (tdesc->vt << 16) | tdesc->vt;
1159 if (!width) width = &scratch;
1160 if (!alignment) alignment = &scratch;
1161 if (!decoded_size) decoded_size = &scratch;
1163 *decoded_size = 0;
1165 switch (tdesc->vt) {
1166 case VT_UI1:
1167 case VT_I1:
1168 *encoded_tdesc = default_tdesc;
1169 *width = 1;
1170 *alignment = 1;
1171 break;
1173 case VT_INT:
1174 *encoded_tdesc = 0x80000000 | (VT_I4 << 16) | VT_INT;
1175 if (ctl2_get_syskind(This) == SYS_WIN16) {
1176 *width = 2;
1177 *alignment = 2;
1178 } else {
1179 *width = 4;
1180 *alignment = 4;
1182 break;
1184 case VT_UINT:
1185 *encoded_tdesc = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
1186 if (ctl2_get_syskind(This) == SYS_WIN16) {
1187 *width = 2;
1188 *alignment = 2;
1189 } else {
1190 *width = 4;
1191 *alignment = 4;
1193 break;
1195 case VT_UI2:
1196 case VT_I2:
1197 case VT_BOOL:
1198 *encoded_tdesc = default_tdesc;
1199 *width = 2;
1200 *alignment = 2;
1201 break;
1203 case VT_I4:
1204 case VT_UI4:
1205 case VT_R4:
1206 case VT_ERROR:
1207 case VT_BSTR:
1208 case VT_HRESULT:
1209 *encoded_tdesc = default_tdesc;
1210 *width = 4;
1211 *alignment = 4;
1212 break;
1214 case VT_CY:
1215 *encoded_tdesc = default_tdesc;
1216 *width = 8;
1217 *alignment = 4; /* guess? */
1218 break;
1220 case VT_VOID:
1221 *encoded_tdesc = 0x80000000 | (VT_EMPTY << 16) | tdesc->vt;
1222 *width = 0;
1223 *alignment = 1;
1224 break;
1226 case VT_PTR:
1227 case VT_SAFEARRAY:
1228 /* FIXME: Make with the error checking. */
1229 FIXME("PTR or SAFEARRAY vartype, may not work correctly.\n");
1231 ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1233 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1234 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1235 if (((typedata[0] & 0xffff) == tdesc->vt) && (typedata[1] == target_type)) break;
1238 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1239 int mix_field;
1241 if (target_type & 0x80000000) {
1242 mix_field = (target_type >> 16) & VT_TYPEMASK;
1243 } else {
1244 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1245 switch((typedata[0]>>16) & ~VT_ARRAY)
1247 case VT_UI1:
1248 case VT_I1:
1249 case VT_UI2:
1250 case VT_I2:
1251 case VT_I4:
1252 case VT_UI4:
1253 mix_field = typedata[0]>>16;
1254 break;
1255 default:
1256 mix_field = ((typedata[0] >> 16) == 0x7fff) ? 0x7fff : 0x7ffe;
1257 break;
1261 if (tdesc->vt == VT_PTR)
1262 mix_field |= VT_BYREF;
1263 else if (tdesc->vt == VT_SAFEARRAY)
1264 mix_field |= VT_ARRAY;
1266 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1267 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1269 typedata[0] = (mix_field << 16) | tdesc->vt;
1270 typedata[1] = target_type;
1273 *encoded_tdesc = typeoffset;
1275 *width = 4;
1276 *alignment = 4;
1277 *decoded_size = sizeof(TYPEDESC) + child_size;
1278 break;
1280 case VT_CARRAY:
1282 /* FIXME: Make with the error checking. */
1283 int num_dims = tdesc->u.lpadesc->cDims, elements = 1, dim;
1285 ctl2_encode_typedesc(This, &tdesc->u.lpadesc->tdescElem, &target_type, width, alignment, NULL);
1286 arrayoffset = ctl2_alloc_segment(This, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1287 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1289 arraydata[0] = target_type;
1290 arraydata[1] = num_dims;
1291 arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1292 arraydata += 2;
1294 for(dim = 0; dim < num_dims; dim++) {
1295 arraydata[0] = tdesc->u.lpadesc->rgbounds[dim].cElements;
1296 arraydata[1] = tdesc->u.lpadesc->rgbounds[dim].lLbound;
1297 elements *= tdesc->u.lpadesc->rgbounds[dim].cElements;
1298 arraydata += 2;
1300 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1301 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1303 typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1304 typedata[1] = arrayoffset;
1306 *encoded_tdesc = typeoffset;
1307 *width = *width * elements;
1308 *decoded_size = sizeof(ARRAYDESC) + (num_dims - 1) * sizeof(SAFEARRAYBOUND);
1310 break;
1312 case VT_USERDEFINED:
1314 const MSFT_TypeInfoBase *basetype;
1315 INT basevt = 0x7fff;
1317 TRACE("USERDEFINED.\n");
1318 if (tdesc->u.hreftype % sizeof(*basetype) == 0 && tdesc->u.hreftype < This->typelib_segdir[MSFT_SEG_TYPEINFO].length)
1320 basetype = (MSFT_TypeInfoBase*)&(This->typelib_segment_data[MSFT_SEG_TYPEINFO][tdesc->u.hreftype]);
1321 switch(basetype->typekind & 0xf)
1323 case TKIND_ENUM:
1324 basevt = VT_I4;
1325 break;
1326 default:
1327 FIXME("USERDEFINED basetype %d not handled\n", basetype->typekind & 0xf);
1328 break;
1331 for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1332 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1333 if ((typedata[0] == ((basevt << 16) | VT_USERDEFINED)) && (typedata[1] == tdesc->u.hreftype)) break;
1336 if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1337 typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1338 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1340 typedata[0] = (basevt << 16) | VT_USERDEFINED;
1341 typedata[1] = tdesc->u.hreftype;
1344 *encoded_tdesc = typeoffset;
1345 *width = 0;
1346 *alignment = 1;
1347 break;
1350 default:
1351 FIXME("Unrecognized type %d.\n", tdesc->vt);
1352 *encoded_tdesc = default_tdesc;
1353 *width = 0;
1354 *alignment = 1;
1355 break;
1358 return 0;
1361 /****************************************************************************
1362 * ctl2_decode_typedesc
1364 * Decodes a type description from an ICreateTypeLib2Impl.
1366 * RETURNS
1368 * Success: S_OK.
1369 * Failure: HRESULT error code.
1371 static HRESULT ctl2_decode_typedesc(
1372 ICreateTypeLib2Impl *This, /* [I] The type library from which to decode the TYPEDESC. */
1373 int encoded_tdesc, /* [I] The encoded type description. */
1374 TYPEDESC *tdesc) /* [O] The decoded type description. */
1376 int *typedata, i;
1377 HRESULT hres;
1379 if (encoded_tdesc & 0x80000000) {
1380 tdesc->vt = encoded_tdesc & VT_TYPEMASK;
1381 tdesc->u.lptdesc = NULL;
1382 return S_OK;
1385 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][encoded_tdesc];
1387 tdesc->vt = typedata[0] & 0xFFFF;
1389 switch(tdesc->vt) {
1390 case VT_PTR:
1391 case VT_SAFEARRAY:
1392 tdesc->u.lptdesc = heap_alloc_zero(sizeof(TYPEDESC));
1393 if (!tdesc->u.lptdesc)
1394 return E_OUTOFMEMORY;
1396 hres = ctl2_decode_typedesc(This, typedata[1], tdesc->u.lptdesc);
1397 if (FAILED(hres)) {
1398 heap_free(tdesc->u.lptdesc);
1399 return hres;
1402 return S_OK;
1404 case VT_CARRAY: {
1405 int arrayoffset, *arraydata, num_dims;
1407 arrayoffset = typedata[1];
1408 arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1409 num_dims = arraydata[1] & 0xFFFF;
1411 tdesc->u.lpadesc = heap_alloc_zero(sizeof(ARRAYDESC) + sizeof(SAFEARRAYBOUND) * (num_dims - 1));
1412 if (!tdesc->u.lpadesc)
1413 return E_OUTOFMEMORY;
1415 hres = ctl2_decode_typedesc(This, arraydata[0], &tdesc->u.lpadesc->tdescElem);
1416 if (FAILED(hres)) {
1417 heap_free(tdesc->u.lpadesc);
1418 return E_OUTOFMEMORY;
1421 for (i = 0; i < num_dims; ++i) {
1422 tdesc->u.lpadesc->rgbounds[i].cElements = arraydata[2 + i * 2];
1423 tdesc->u.lpadesc->rgbounds[i].lLbound = arraydata[3 + i * 2];
1426 return S_OK;
1428 case VT_USERDEFINED:
1429 tdesc->u.hreftype = typedata[1];
1430 return S_OK;
1431 default:
1432 FIXME("unable to decode typedesc (%08x): unknown VT: %d\n", encoded_tdesc, tdesc->vt);
1433 return E_NOTIMPL;
1437 /****************************************************************************
1438 * ctl2_find_nth_reference
1440 * Finds a reference by index into the linked list of reference records.
1442 * RETURNS
1444 * Success: Offset of the desired reference record.
1445 * Failure: -1.
1447 static int ctl2_find_nth_reference(
1448 ICreateTypeLib2Impl *This, /* [I] The type library in which to search. */
1449 int offset, /* [I] The starting offset of the reference list. */
1450 int index) /* [I] The index of the reference to find. */
1452 MSFT_RefRecord *ref;
1454 for (; index && (offset != -1); index--) {
1455 ref = (MSFT_RefRecord *)&This->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1456 offset = ref->onext;
1459 return offset;
1462 /****************************************************************************
1463 * ctl2_find_typeinfo_from_offset
1465 * Finds an ITypeInfo given an offset into the TYPEINFO segment.
1467 * RETURNS
1469 * Success: S_OK.
1470 * Failure: TYPE_E_ELEMENTNOTFOUND.
1472 static HRESULT ctl2_find_typeinfo_from_offset(
1473 ICreateTypeLib2Impl *This, /* [I] The typelib to find the typeinfo in. */
1474 int offset, /* [I] The offset of the desired typeinfo. */
1475 ITypeInfo **ppTinfo) /* [I] The typeinfo found. */
1477 void *typeinfodata;
1478 ICreateTypeInfo2Impl *typeinfo;
1480 typeinfodata = &This->typelib_segment_data[MSFT_SEG_TYPEINFO][offset];
1482 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
1483 if (typeinfo->typeinfo == typeinfodata) {
1484 *ppTinfo = (ITypeInfo *)&typeinfo->ITypeInfo2_iface;
1485 ITypeInfo2_AddRef(*ppTinfo);
1486 return S_OK;
1490 ERR("Failed to find typeinfo, invariant varied.\n");
1492 return TYPE_E_ELEMENTNOTFOUND;
1495 /****************************************************************************
1496 * funcrecord_reallochdr
1498 * Ensure FuncRecord data block contains header of required size
1500 * PARAMS
1502 * typedata [IO] - reference to pointer to data block
1503 * need [I] - required size of block in bytes
1505 * RETURNS
1507 * Number of additionally allocated bytes
1509 static INT funcrecord_reallochdr(INT **typedata, int need)
1511 int tail = (*typedata)[5]*((*typedata)[4]&0x1000?16:12);
1512 int hdr = (*typedata)[0] - tail;
1513 int i;
1515 if (hdr >= need)
1516 return 0;
1518 *typedata = heap_realloc(*typedata, need + tail);
1519 if (!*typedata)
1520 return -1;
1522 if (tail)
1523 memmove((char*)*typedata + need, (const char*)*typedata + hdr, tail);
1524 (*typedata)[0] = need + tail;
1526 /* fill in default values */
1527 for(i = (hdr+3)/4; (i+1)*4 <= need; i++)
1529 switch(i)
1531 case 2:
1532 (*typedata)[i] = 0;
1533 break;
1534 case 7:
1535 (*typedata)[i] = -1;
1536 break;
1537 case 8:
1538 (*typedata)[i] = -1;
1539 break;
1540 case 9:
1541 (*typedata)[i] = -1;
1542 break;
1543 case 10:
1544 (*typedata)[i] = -1;
1545 break;
1546 case 11:
1547 (*typedata)[i] = 0;
1548 break;
1549 case 12:
1550 (*typedata)[i] = -1;
1551 break;
1555 return need - hdr;
1558 /*================== ICreateTypeInfo2 Implementation ===================================*/
1560 /******************************************************************************
1561 * ICreateTypeInfo2_QueryInterface {OLEAUT32}
1564 static HRESULT WINAPI ICreateTypeInfo2_fnQueryInterface(
1565 ICreateTypeInfo2 * iface,
1566 REFIID riid,
1567 VOID **ppvObject)
1569 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1571 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
1573 *ppvObject=NULL;
1574 if(IsEqualIID(riid, &IID_IUnknown) ||
1575 IsEqualIID(riid,&IID_ICreateTypeInfo)||
1576 IsEqualIID(riid,&IID_ICreateTypeInfo2))
1578 *ppvObject = This;
1579 } else if (IsEqualIID(riid, &IID_ITypeInfo) ||
1580 IsEqualIID(riid, &IID_ITypeInfo2)) {
1581 *ppvObject = &This->ITypeInfo2_iface;
1584 if(*ppvObject)
1586 ICreateTypeInfo2_AddRef(iface);
1587 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
1588 return S_OK;
1590 TRACE("-- Interface: E_NOINTERFACE\n");
1591 return E_NOINTERFACE;
1594 /******************************************************************************
1595 * ICreateTypeInfo2_AddRef {OLEAUT32}
1597 static ULONG WINAPI ICreateTypeInfo2_fnAddRef(ICreateTypeInfo2 *iface)
1599 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1600 ULONG ref = InterlockedIncrement(&This->ref);
1602 TRACE("(%p)->ref was %u\n",This, ref - 1);
1604 if(ref==1 && This->typelib)
1605 ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This->typelib);
1607 return ref;
1610 /******************************************************************************
1611 * ICreateTypeInfo2_Release {OLEAUT32}
1613 static ULONG WINAPI ICreateTypeInfo2_fnRelease(ICreateTypeInfo2 *iface)
1615 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1616 ULONG ref = InterlockedDecrement(&This->ref);
1618 TRACE("(%p)->(%u)\n",This, ref);
1620 if (!ref) {
1621 if (This->typelib) {
1622 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)This->typelib);
1623 /* Keep This->typelib reference to make stored ICreateTypeInfo structure valid */
1624 /* This->typelib = NULL; */
1627 /* ICreateTypeLib2 frees all ICreateTypeInfos when it releases. */
1628 /* HeapFree(GetProcessHeap(),0,This); */
1629 return 0;
1632 return ref;
1636 /******************************************************************************
1637 * ICreateTypeInfo2_SetGuid {OLEAUT32}
1639 static HRESULT WINAPI ICreateTypeInfo2_fnSetGuid(ICreateTypeInfo2 *iface, REFGUID guid)
1641 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1643 MSFT_GuidEntry guidentry;
1644 int offset;
1646 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
1648 guidentry.guid = *guid;
1649 guidentry.hreftype = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1650 guidentry.next_hash = -1;
1652 offset = ctl2_alloc_guid(This->typelib, &guidentry);
1654 if (offset == -1) return E_OUTOFMEMORY;
1656 This->typeinfo->posguid = offset;
1658 if (IsEqualIID(guid, &IID_IDispatch)) {
1659 This->typelib->typelib_header.dispatchpos = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1662 return S_OK;
1665 /******************************************************************************
1666 * ICreateTypeInfo2_SetTypeFlags {OLEAUT32}
1668 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeFlags(ICreateTypeInfo2 *iface, UINT uTypeFlags)
1670 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1672 TRACE("(%p,0x%x)\n", iface, uTypeFlags);
1674 if(uTypeFlags & TYPEFLAG_FDUAL) {
1675 This->typeinfo->typekind |= 0x10;
1676 This->typeinfo->typekind &= ~0x0f;
1677 This->typeinfo->typekind |= TKIND_DISPATCH;
1679 if(!This->dual) {
1680 This->dual = heap_alloc(sizeof(ICreateTypeInfo2Impl));
1681 if(!This->dual)
1682 return E_OUTOFMEMORY;
1684 memcpy(This->dual, This, sizeof(ICreateTypeInfo2Impl));
1685 This->dual->ref = 0;
1686 This->dual->typekind = This->typekind==TKIND_DISPATCH ?
1687 TKIND_INTERFACE : TKIND_DISPATCH;
1688 This->dual->dual = This;
1691 /* Make sure dispatch is in typeinfos queue */
1692 if(This->typekind != TKIND_DISPATCH) {
1693 if(This->typelib->last_typeinfo == This)
1694 This->typelib->last_typeinfo = This->dual;
1696 if(This->typelib->typeinfos == This)
1697 This->typelib->typeinfos = This->dual;
1698 else {
1699 ICreateTypeInfo2Impl *iter;
1701 for(iter=This->typelib->typeinfos; iter->next_typeinfo!=This; iter=iter->next_typeinfo);
1702 iter->next_typeinfo = This->dual;
1704 } else
1705 iface = &This->dual->ICreateTypeInfo2_iface;
1708 if (uTypeFlags & (TYPEFLAG_FDISPATCHABLE|TYPEFLAG_FDUAL)) {
1709 static const WCHAR stdole2tlb[] = { 's','t','d','o','l','e','2','.','t','l','b',0 };
1710 ITypeLib *stdole;
1711 ITypeInfo *dispatch;
1712 HREFTYPE hreftype;
1713 HRESULT hres;
1715 hres = LoadTypeLib(stdole2tlb, &stdole);
1716 if(FAILED(hres))
1717 return hres;
1719 hres = ITypeLib_GetTypeInfoOfGuid(stdole, &IID_IDispatch, &dispatch);
1720 ITypeLib_Release(stdole);
1721 if(FAILED(hres))
1722 return hres;
1724 hres = ICreateTypeInfo2_AddRefTypeInfo(iface, dispatch, &hreftype);
1725 ITypeInfo_Release(dispatch);
1726 if(FAILED(hres))
1727 return hres;
1730 This->typeinfo->flags = uTypeFlags;
1731 return S_OK;
1734 /******************************************************************************
1735 * ICreateTypeInfo2_SetDocString {OLEAUT32}
1737 static HRESULT WINAPI ICreateTypeInfo2_fnSetDocString(
1738 ICreateTypeInfo2* iface,
1739 LPOLESTR pStrDoc)
1741 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1743 int offset;
1745 TRACE("(%p,%s)\n", iface, debugstr_w(pStrDoc));
1746 if (!pStrDoc)
1747 return E_INVALIDARG;
1749 offset = ctl2_alloc_string(This->typelib, pStrDoc);
1750 if (offset == -1) return E_OUTOFMEMORY;
1751 This->typeinfo->docstringoffs = offset;
1752 return S_OK;
1755 /******************************************************************************
1756 * ICreateTypeInfo2_SetHelpContext {OLEAUT32}
1758 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpContext(
1759 ICreateTypeInfo2* iface,
1760 DWORD dwHelpContext)
1762 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1764 TRACE("(%p,%d)\n", iface, dwHelpContext);
1766 This->typeinfo->helpcontext = dwHelpContext;
1768 return S_OK;
1771 /******************************************************************************
1772 * ICreateTypeInfo2_SetVersion {OLEAUT32}
1774 static HRESULT WINAPI ICreateTypeInfo2_fnSetVersion(
1775 ICreateTypeInfo2* iface,
1776 WORD wMajorVerNum,
1777 WORD wMinorVerNum)
1779 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1781 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
1783 This->typeinfo->version = wMajorVerNum | (wMinorVerNum << 16);
1784 return S_OK;
1787 /******************************************************************************
1788 * ICreateTypeInfo2_AddRefTypeInfo {OLEAUT32}
1790 static HRESULT WINAPI ICreateTypeInfo2_fnAddRefTypeInfo(
1791 ICreateTypeInfo2* iface,
1792 ITypeInfo* pTInfo,
1793 HREFTYPE* phRefType)
1795 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1797 ITypeLib *container;
1798 UINT index;
1799 HRESULT res;
1801 TRACE("(%p,%p,%p)\n", iface, pTInfo, phRefType);
1803 if(!pTInfo || !phRefType)
1804 return E_INVALIDARG;
1807 * Unfortunately, we can't rely on the passed-in TypeInfo even having the
1808 * same internal structure as one of ours. It could be from another
1809 * implementation of ITypeInfo. So we need to do the following...
1811 res = ITypeInfo_GetContainingTypeLib(pTInfo, &container, &index);
1812 if (FAILED(res)) {
1813 TRACE("failed to find containing typelib.\n");
1814 return res;
1817 if (container == (ITypeLib *)&This->typelib->lpVtblTypeLib2) {
1818 /* Process locally defined TypeInfo */
1819 *phRefType = This->typelib->typelib_typeinfo_offsets[index];
1820 } else {
1821 BSTR name;
1822 TLIBATTR *tlibattr;
1823 TYPEATTR *typeattr;
1824 TYPEKIND typekind;
1825 MSFT_GuidEntry guid, *check_guid;
1826 MSFT_ImpInfo impinfo;
1827 int guid_offset, import_offset;
1828 HRESULT hres;
1830 /* Allocate container GUID */
1831 hres = ITypeLib_GetLibAttr(container, &tlibattr);
1832 if(FAILED(hres)) {
1833 ITypeLib_Release(container);
1834 return hres;
1837 guid.guid = tlibattr->guid;
1838 guid.hreftype = This->typelib->typelib_segdir[MSFT_SEG_IMPORTFILES].length+2;
1839 guid.next_hash = -1;
1841 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1842 if(guid_offset == -1) {
1843 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1844 ITypeLib_Release(container);
1845 return E_OUTOFMEMORY;
1848 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1849 if(check_guid->hreftype == guid.hreftype)
1850 This->typelib->typelib_guids++;
1852 /* Get import file name */
1853 hres = QueryPathOfRegTypeLib(&guid.guid, tlibattr->wMajorVerNum,
1854 tlibattr->wMinorVerNum, tlibattr->lcid, &name);
1855 if(FAILED(hres)) {
1856 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1857 ITypeLib_Release(container);
1858 return hres;
1861 /* Import file */
1862 import_offset = ctl2_alloc_importfile(This->typelib, guid_offset, tlibattr->lcid,
1863 tlibattr->wMajorVerNum, tlibattr->wMinorVerNum, strrchrW(name, '\\')+1);
1864 ITypeLib_ReleaseTLibAttr(container, tlibattr);
1865 SysFreeString(name);
1867 if(import_offset == -1) {
1868 ITypeLib_Release(container);
1869 return E_OUTOFMEMORY;
1872 /* Allocate referenced guid */
1873 hres = ITypeInfo_GetTypeAttr(pTInfo, &typeattr);
1874 if(FAILED(hres)) {
1875 ITypeLib_Release(container);
1876 return hres;
1879 guid.guid = typeattr->guid;
1880 guid.hreftype = This->typelib->typeinfo_guids*12+1;
1881 guid.next_hash = -1;
1882 typekind = typeattr->typekind;
1883 ITypeInfo_ReleaseTypeAttr(pTInfo, typeattr);
1885 guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1886 if(guid_offset == -1) {
1887 ITypeLib_Release(container);
1888 return E_OUTOFMEMORY;
1891 check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1892 if(check_guid->hreftype == guid.hreftype)
1893 This->typelib->typeinfo_guids++;
1895 /* Allocate importinfo */
1896 impinfo.flags = (typekind<<24) | MSFT_IMPINFO_OFFSET_IS_GUID;
1897 impinfo.oImpFile = import_offset;
1898 impinfo.oGuid = guid_offset;
1899 *phRefType = ctl2_alloc_importinfo(This->typelib, &impinfo)+1;
1901 if(IsEqualGUID(&guid.guid, &IID_IDispatch))
1902 This->typelib->typelib_header.dispatchpos = *phRefType;
1905 ITypeLib_Release(container);
1906 return S_OK;
1909 /******************************************************************************
1910 * ICreateTypeInfo2_AddFuncDesc {OLEAUT32}
1912 static HRESULT WINAPI ICreateTypeInfo2_fnAddFuncDesc(
1913 ICreateTypeInfo2* iface,
1914 UINT index,
1915 FUNCDESC* pFuncDesc)
1917 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
1919 CyclicList *iter, *insert;
1920 int *typedata;
1921 int i, num_defaults = 0, num_retval = 0;
1922 int decoded_size;
1923 HRESULT hres;
1925 TRACE("(%p,%d,%p)\n", iface, index, pFuncDesc);
1927 if(!pFuncDesc || pFuncDesc->oVft&3)
1928 return E_INVALIDARG;
1930 TRACE("{%d,%p,%p,%d,%d,%d,%d,%d,%d,%d,{%d},%d}\n", pFuncDesc->memid,
1931 pFuncDesc->lprgscode, pFuncDesc->lprgelemdescParam, pFuncDesc->funckind,
1932 pFuncDesc->invkind, pFuncDesc->callconv, pFuncDesc->cParams,
1933 pFuncDesc->cParamsOpt, pFuncDesc->oVft, pFuncDesc->cScodes,
1934 pFuncDesc->elemdescFunc.tdesc.vt, pFuncDesc->wFuncFlags);
1936 if(pFuncDesc->cParamsOpt || pFuncDesc->cScodes)
1937 FIXME("Unimplemented parameter - created typelib will be incorrect\n");
1939 switch(This->typekind) {
1940 case TKIND_MODULE:
1941 if(pFuncDesc->funckind != FUNC_STATIC)
1942 return TYPE_E_BADMODULEKIND;
1943 break;
1944 case TKIND_DISPATCH:
1945 if(pFuncDesc->funckind != FUNC_DISPATCH)
1946 return TYPE_E_BADMODULEKIND;
1947 break;
1948 default:
1949 if(pFuncDesc->funckind != FUNC_PUREVIRTUAL)
1950 return TYPE_E_BADMODULEKIND;
1953 if(cti2_get_func_count(This->typeinfo) < index)
1954 return TYPE_E_ELEMENTNOTFOUND;
1956 if((pFuncDesc->invkind&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) &&
1957 !pFuncDesc->cParams)
1958 return TYPE_E_INCONSISTENTPROPFUNCS;
1960 /* get number of arguments with default values specified */
1961 for (i = 0; i < pFuncDesc->cParams; i++) {
1962 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT)
1963 num_defaults++;
1964 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FRETVAL)
1965 num_retval++;
1968 if (!This->typedata) {
1969 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
1970 if(!This->typedata)
1971 return E_OUTOFMEMORY;
1973 This->typedata->next = This->typedata;
1975 if(This->dual)
1976 This->dual->typedata = This->typedata;
1979 /* allocate type data space for us */
1980 insert = alloc_cyclic_list_item(CyclicListFunc);
1981 if(!insert)
1982 return E_OUTOFMEMORY;
1983 insert->u.data = heap_alloc(FIELD_OFFSET(MSFT_FuncRecord, HelpContext) +
1984 sizeof(int[(num_defaults?4:3)])*pFuncDesc->cParams);
1985 if(!insert->u.data) {
1986 heap_free(insert);
1987 return E_OUTOFMEMORY;
1990 /* fill out the basic type information */
1991 typedata = insert->u.data;
1992 typedata[0] = FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
1993 ctl2_encode_typedesc(This->typelib, &pFuncDesc->elemdescFunc.tdesc, &typedata[1], NULL, NULL, &decoded_size);
1994 typedata[2] = pFuncDesc->wFuncFlags;
1995 typedata[3] = ((sizeof(FUNCDESC) + decoded_size) << 16) | (unsigned short)(pFuncDesc->oVft?pFuncDesc->oVft+1:0);
1996 typedata[4] = (pFuncDesc->callconv << 8) | (pFuncDesc->invkind << 3) | pFuncDesc->funckind;
1997 if(num_defaults) typedata[4] |= 0x1000;
1998 if (num_retval) typedata[4] |= 0x4000;
1999 typedata[5] = pFuncDesc->cParams;
2001 /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
2002 /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
2003 typedata[3] += (sizeof(ELEMDESC) * pFuncDesc->cParams) << 16;
2004 typedata[3] += (sizeof(PARAMDESCEX) * num_defaults) << 16;
2006 /* add default values */
2007 if(num_defaults) {
2008 for (i = 0; i < pFuncDesc->cParams; i++)
2009 if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
2010 hres = ctl2_encode_variant(This->typelib, typedata+6+i,
2011 &pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue,
2012 pFuncDesc->lprgelemdescParam[i].tdesc.vt);
2014 if(FAILED(hres)) {
2015 heap_free(insert->u.data);
2016 heap_free(insert);
2017 return hres;
2019 } else
2020 typedata[6+i] = 0xffffffff;
2022 num_defaults = pFuncDesc->cParams;
2025 /* add arguments */
2026 for (i = 0; i < pFuncDesc->cParams; i++) {
2027 ctl2_encode_typedesc(This->typelib, &pFuncDesc->lprgelemdescParam[i].tdesc,
2028 &typedata[6+num_defaults+(i*3)], NULL, NULL, &decoded_size);
2029 typedata[7+num_defaults+(i*3)] = -1;
2030 typedata[8+num_defaults+(i*3)] = pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags;
2031 typedata[3] += decoded_size << 16;
2034 /* update the index data */
2035 insert->indice = pFuncDesc->memid;
2036 insert->name = -1;
2038 /* insert type data to list */
2039 if(index == cti2_get_func_count(This->typeinfo)) {
2040 insert->next = This->typedata->next;
2041 This->typedata->next = insert;
2042 This->typedata = insert;
2044 if(This->dual)
2045 This->dual->typedata = This->typedata;
2046 } else {
2047 iter = This->typedata->next;
2048 for(i=0; i<index; i++)
2049 iter = iter->next;
2051 insert->next = iter->next;
2052 iter->next = insert;
2055 /* update type data size */
2056 This->typedata->next->u.val += FIELD_OFFSET(MSFT_FuncRecord, HelpContext) + pFuncDesc->cParams*(num_defaults?16:12);
2058 /* Increment the number of function elements */
2059 This->typeinfo->cElement += 1;
2061 return S_OK;
2064 /******************************************************************************
2065 * ICreateTypeInfo2_AddImplType {OLEAUT32}
2067 static HRESULT WINAPI ICreateTypeInfo2_fnAddImplType(
2068 ICreateTypeInfo2* iface,
2069 UINT index,
2070 HREFTYPE hRefType)
2072 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2074 TRACE("(%p,%d,%d)\n", iface, index, hRefType);
2076 if (This->typekind == TKIND_COCLASS) {
2077 int offset;
2078 MSFT_RefRecord *ref;
2080 if (index == 0) {
2081 if (This->typeinfo->datatype1 != -1) return TYPE_E_ELEMENTNOTFOUND;
2083 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2084 if (offset == -1) return E_OUTOFMEMORY;
2086 This->typeinfo->datatype1 = offset;
2087 } else {
2088 int lastoffset;
2090 lastoffset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index - 1);
2091 if (lastoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
2093 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][lastoffset];
2094 if (ref->onext != -1) return TYPE_E_ELEMENTNOTFOUND;
2096 offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
2097 if (offset == -1) return E_OUTOFMEMORY;
2099 ref->onext = offset;
2102 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2104 ref->reftype = hRefType;
2105 ref->flags = 0;
2106 ref->oCustData = -1;
2107 ref->onext = -1;
2108 This->typeinfo->cImplTypes++;
2109 } else if (This->typekind == TKIND_INTERFACE) {
2110 if (This->typeinfo->cImplTypes && index==1)
2111 return TYPE_E_BADMODULEKIND;
2113 if( index != 0) return TYPE_E_ELEMENTNOTFOUND;
2115 This->typeinfo->datatype1 = hRefType;
2116 This->typeinfo->cImplTypes = 1;
2117 } else if (This->typekind == TKIND_DISPATCH) {
2118 if(index != 0) return TYPE_E_ELEMENTNOTFOUND;
2120 /* FIXME: Check if referenced typeinfo is IDispatch */
2121 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2122 This->typeinfo->cImplTypes = 1;
2123 } else {
2124 FIXME("AddImplType unsupported on typekind %d\n", This->typekind);
2125 return E_OUTOFMEMORY;
2128 return S_OK;
2131 /******************************************************************************
2132 * ICreateTypeInfo2_SetImplTypeFlags {OLEAUT32}
2134 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeFlags(
2135 ICreateTypeInfo2* iface,
2136 UINT index,
2137 INT implTypeFlags)
2139 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2140 int offset;
2141 MSFT_RefRecord *ref;
2143 TRACE("(%p,%d,0x%x)\n", iface, index, implTypeFlags);
2145 if (This->typekind != TKIND_COCLASS) {
2146 return TYPE_E_BADMODULEKIND;
2149 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
2150 if (offset == -1) return TYPE_E_ELEMENTNOTFOUND;
2152 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
2153 ref->flags = implTypeFlags;
2155 return S_OK;
2158 /******************************************************************************
2159 * ICreateTypeInfo2_SetAlignment {OLEAUT32}
2161 static HRESULT WINAPI ICreateTypeInfo2_fnSetAlignment(
2162 ICreateTypeInfo2* iface,
2163 WORD cbAlignment)
2165 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2167 TRACE("(%p,%d)\n", iface, cbAlignment);
2169 if (!cbAlignment) return E_INVALIDARG;
2170 if (cbAlignment > 16) return E_INVALIDARG;
2172 This->typeinfo->typekind &= ~0xffc0;
2173 This->typeinfo->typekind |= cbAlignment << 6;
2175 /* FIXME: There's probably some way to simplify this. */
2176 switch (This->typekind) {
2177 case TKIND_ALIAS:
2178 default:
2179 break;
2181 case TKIND_ENUM:
2182 case TKIND_INTERFACE:
2183 case TKIND_DISPATCH:
2184 case TKIND_COCLASS:
2185 if (cbAlignment > 4) cbAlignment = 4;
2186 break;
2188 case TKIND_RECORD:
2189 case TKIND_MODULE:
2190 case TKIND_UNION:
2191 cbAlignment = 1;
2192 break;
2195 This->typeinfo->typekind |= cbAlignment << 11;
2197 return S_OK;
2200 /******************************************************************************
2201 * ICreateTypeInfo2_SetSchema {OLEAUT32}
2203 static HRESULT WINAPI ICreateTypeInfo2_fnSetSchema(
2204 ICreateTypeInfo2* iface,
2205 LPOLESTR pStrSchema)
2207 FIXME("(%p,%s), stub!\n", iface, debugstr_w(pStrSchema));
2208 return E_OUTOFMEMORY;
2211 /******************************************************************************
2212 * ICreateTypeInfo2_AddVarDesc {OLEAUT32}
2214 static HRESULT WINAPI ICreateTypeInfo2_fnAddVarDesc(
2215 ICreateTypeInfo2* iface,
2216 UINT index,
2217 VARDESC* pVarDesc)
2219 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2221 HRESULT status = S_OK;
2222 CyclicList *insert;
2223 INT *typedata;
2224 int var_datawidth;
2225 int var_alignment;
2226 int var_type_size;
2227 int alignment;
2229 TRACE("(%p,%d,%p)\n", iface, index, pVarDesc);
2230 TRACE("%d, %p, %d, {{%x, %d}, {%p, %x}}, 0x%x, %d\n", pVarDesc->memid, pVarDesc->lpstrSchema, pVarDesc->u.oInst,
2231 pVarDesc->elemdescVar.tdesc.u.hreftype, pVarDesc->elemdescVar.tdesc.vt,
2232 pVarDesc->elemdescVar.u.paramdesc.pparamdescex, pVarDesc->elemdescVar.u.paramdesc.wParamFlags,
2233 pVarDesc->wVarFlags, pVarDesc->varkind);
2235 if (cti2_get_var_count(This->typeinfo) != index)
2236 return TYPE_E_ELEMENTNOTFOUND;
2238 if (!This->typedata) {
2239 This->typedata = alloc_cyclic_list_item(CyclicListSentinel);
2240 if(!This->typedata)
2241 return E_OUTOFMEMORY;
2243 This->typedata->next = This->typedata;
2245 if(This->dual)
2246 This->dual->typedata = This->typedata;
2249 insert = alloc_cyclic_list_item(CyclicListVar);
2250 if(!insert)
2251 return E_OUTOFMEMORY;
2253 /* allocate whole structure, it's fixed size always */
2254 insert->u.data = heap_alloc(sizeof(MSFT_VarRecord));
2255 if(!insert->u.data) {
2256 heap_free(insert);
2257 return E_OUTOFMEMORY;
2260 insert->next = This->typedata->next;
2261 This->typedata->next = insert;
2262 This->typedata = insert;
2264 if(This->dual)
2265 This->dual->typedata = This->typedata;
2267 This->typedata->next->u.val += FIELD_OFFSET(MSFT_VarRecord, HelpContext);
2268 typedata = This->typedata->u.data;
2270 /* fill out the basic type information */
2272 /* no optional fields initially */
2273 typedata[0] = FIELD_OFFSET(MSFT_VarRecord, HelpContext) | (index << 16);
2274 typedata[2] = pVarDesc->wVarFlags;
2275 typedata[3] = (sizeof(VARDESC) << 16) | pVarDesc->varkind;
2277 /* update the index data */
2278 insert->indice = 0x40000000 + index;
2279 insert->name = -1;
2281 /* figure out type widths and whatnot */
2282 ctl2_encode_typedesc(This->typelib, &pVarDesc->elemdescVar.tdesc,
2283 &typedata[1], &var_datawidth, &var_alignment,
2284 &var_type_size);
2286 if (pVarDesc->varkind != VAR_CONST)
2288 /* pad out starting position to data width */
2289 This->datawidth += var_alignment - 1;
2290 This->datawidth &= ~(var_alignment - 1);
2291 typedata[4] = This->datawidth;
2293 /* add the new variable to the total data width */
2294 This->datawidth += var_datawidth;
2295 if(This->dual)
2296 This->dual->datawidth = This->datawidth;
2298 /* add type description size to total required allocation */
2299 typedata[3] += var_type_size << 16;
2301 /* fix type alignment */
2302 alignment = (This->typeinfo->typekind >> 11) & 0x1f;
2303 if (alignment < var_alignment) {
2304 alignment = var_alignment;
2305 This->typeinfo->typekind &= ~0xf800;
2306 This->typeinfo->typekind |= alignment << 11;
2309 /* ??? */
2310 if (!This->typeinfo->res2) This->typeinfo->res2 = 0x1a;
2311 if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
2312 This->typeinfo->res2 <<= 1;
2315 /* ??? */
2316 if (This->typeinfo->res3 == -1) This->typeinfo->res3 = 0;
2317 This->typeinfo->res3 += 0x2c;
2319 /* pad data width to alignment */
2320 This->typeinfo->size = (This->datawidth + (alignment - 1)) & ~(alignment - 1);
2321 } else {
2322 VARIANT *value = pVarDesc->DUMMYUNIONNAME.lpvarValue;
2323 status = ctl2_encode_variant(This->typelib, typedata+4, value, V_VT(value));
2324 /* ??? native sets size 0x34 */
2325 typedata[3] += 0x10 << 16;
2328 /* increment the number of variable elements */
2329 This->typeinfo->cElement += 0x10000;
2331 return status;
2334 /******************************************************************************
2335 * ICreateTypeInfo2_SetFuncAndParamNames {OLEAUT32}
2337 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncAndParamNames(
2338 ICreateTypeInfo2* iface,
2339 UINT index,
2340 LPOLESTR* names,
2341 UINT cNames)
2343 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2344 CyclicList *iter, *iter2;
2345 int offset, len, i;
2346 unsigned char *namedata;
2348 TRACE("(%p %d %p %d)\n", This, index, names, cNames);
2350 if(!names)
2351 return E_INVALIDARG;
2353 if(index >= cti2_get_func_count(This->typeinfo) || cNames == 0)
2354 return TYPE_E_ELEMENTNOTFOUND;
2356 for(iter=This->typedata->next->next, i=0; /* empty */; iter=iter->next)
2357 if (iter->type == CyclicListFunc)
2358 if (i++ >= index)
2359 break;
2361 /* cNames == cParams for put or putref accessor, cParams+1 otherwise */
2362 if(cNames != iter->u.data[5] + (ctl2_get_invokekind(iter) & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF) ? 0 : 1))
2363 return TYPE_E_ELEMENTNOTFOUND;
2365 TRACE("function name %s\n", debugstr_w(names[0]));
2366 len = ctl2_encode_name(This->typelib, names[0], (char**)&namedata);
2367 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2369 int cmp = memcmp(namedata, This->typelib->typelib_segment_data[MSFT_SEG_NAME]+iter2->name+8, len);
2370 if (iter2->name != -1 && cmp == 0) {
2371 if (iter2->type == CyclicListFunc) {
2372 INVOKEKIND inv1 = ctl2_get_invokekind(iter);
2373 INVOKEKIND inv2 = ctl2_get_invokekind(iter2);
2375 /* it's allowed to have PUT, PUTREF and GET methods with the same name */
2376 if ((inv1 != inv2) &&
2377 (inv1 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)) &&
2378 (inv2 & (INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF|INVOKE_PROPERTYGET)))
2379 continue;
2382 return TYPE_E_AMBIGUOUSNAME;
2386 offset = ctl2_alloc_name(This->typelib, names[0]);
2387 if(offset == -1)
2388 return E_OUTOFMEMORY;
2390 iter->name = offset;
2392 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2393 if (*((INT*)namedata) == -1)
2394 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2396 len = ctl2_get_record_size(iter)/4 - iter->u.data[5]*3;
2398 for (i = 1; i < cNames; i++) {
2399 offset = ctl2_alloc_name(This->typelib, names[i]);
2400 iter->u.data[len + ((i-1)*3) + 1] = offset;
2403 return S_OK;
2406 /******************************************************************************
2407 * ICreateTypeInfo2_SetVarName {OLEAUT32}
2409 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarName(
2410 ICreateTypeInfo2* iface,
2411 UINT index,
2412 LPOLESTR szName)
2414 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2415 CyclicList *iter;
2416 int offset, i;
2417 unsigned char *namedata;
2419 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(szName));
2421 if (cti2_get_var_count(This->typeinfo) <= index)
2422 return TYPE_E_ELEMENTNOTFOUND;
2424 offset = ctl2_alloc_name(This->typelib, szName);
2425 if (offset == -1) return E_OUTOFMEMORY;
2427 namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2428 if (*((INT *)namedata) == -1) {
2429 *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2430 namedata[9] |= 0x10;
2432 if (This->typekind == TKIND_ENUM) {
2433 namedata[9] |= 0x20;
2436 for(iter = This->typedata->next->next, i = 0; /* empty */; iter = iter->next)
2437 if (iter->type == CyclicListVar)
2438 if (i++ >= index)
2439 break;
2441 iter->name = offset;
2442 return S_OK;
2445 /******************************************************************************
2446 * ICreateTypeInfo2_SetTypeDescAlias {OLEAUT32}
2448 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeDescAlias(
2449 ICreateTypeInfo2* iface,
2450 TYPEDESC* pTDescAlias)
2452 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2454 int encoded_typedesc;
2455 int width;
2457 if (This->typekind != TKIND_ALIAS) {
2458 return TYPE_E_WRONGTYPEKIND;
2461 FIXME("(%p,%p), hack!\n", iface, pTDescAlias);
2463 if (ctl2_encode_typedesc(This->typelib, pTDescAlias, &encoded_typedesc, &width, NULL, NULL) == -1) {
2464 return E_OUTOFMEMORY;
2467 This->typeinfo->size = width;
2468 This->typeinfo->datatype1 = encoded_typedesc;
2470 return S_OK;
2473 /******************************************************************************
2474 * ICreateTypeInfo2_DefineFuncAsDllEntry {OLEAUT32}
2476 static HRESULT WINAPI ICreateTypeInfo2_fnDefineFuncAsDllEntry(
2477 ICreateTypeInfo2* iface,
2478 UINT index,
2479 LPOLESTR szDllName,
2480 LPOLESTR szProcName)
2482 FIXME("(%p,%d,%s,%s), stub!\n", iface, index, debugstr_w(szDllName), debugstr_w(szProcName));
2483 return E_OUTOFMEMORY;
2486 /******************************************************************************
2487 * ICreateTypeInfo2_SetFuncDocString {OLEAUT32}
2489 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncDocString(
2490 ICreateTypeInfo2* iface,
2491 UINT index,
2492 LPOLESTR szDocString)
2494 FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2495 return E_OUTOFMEMORY;
2498 /******************************************************************************
2499 * ICreateTypeInfo2_SetVarDocString {OLEAUT32}
2501 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarDocString(
2502 ICreateTypeInfo2* iface,
2503 UINT index,
2504 LPOLESTR docstring)
2506 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2507 CyclicList *iter;
2509 TRACE("(%p,%d,%s)\n", This, index, debugstr_w(docstring));
2511 if (!docstring) return E_INVALIDARG;
2513 if (cti2_get_var_count(This->typeinfo) <= index)
2514 return TYPE_E_ELEMENTNOTFOUND;
2516 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2517 if (iter->type == CyclicListVar)
2519 if (index-- == 0)
2521 int offset = ctl2_alloc_string(This->typelib, docstring);
2523 if (offset == -1) return E_OUTOFMEMORY;
2524 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, res9));
2525 iter->u.data[6] = offset;
2526 return S_OK;
2530 return TYPE_E_ELEMENTNOTFOUND;
2533 /******************************************************************************
2534 * ICreateTypeInfo2_SetFuncHelpContext {OLEAUT32}
2536 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpContext(
2537 ICreateTypeInfo2* iface,
2538 UINT index,
2539 DWORD dwHelpContext)
2541 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2542 CyclicList *func;
2544 TRACE("(%p,%d,%d)\n", iface, index, dwHelpContext);
2546 if(cti2_get_func_count(This->typeinfo) < index)
2547 return TYPE_E_ELEMENTNOTFOUND;
2549 if(cti2_get_func_count(This->typeinfo) == index && This->typedata->type == CyclicListFunc)
2550 func = This->typedata;
2551 else
2552 for(func=This->typedata->next->next; func!=This->typedata; func=func->next)
2553 if (func->type == CyclicListFunc)
2554 if(index-- == 0)
2555 break;
2557 This->typedata->next->u.val += funcrecord_reallochdr(&func->u.data, 7*sizeof(int));
2558 if(!func->u.data)
2559 return E_OUTOFMEMORY;
2561 func->u.data[6] = dwHelpContext;
2562 return S_OK;
2565 /******************************************************************************
2566 * ICreateTypeInfo2_SetVarHelpContext {OLEAUT32}
2568 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpContext(
2569 ICreateTypeInfo2* iface,
2570 UINT index,
2571 DWORD context)
2573 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2574 CyclicList *iter;
2576 TRACE("(%p,%d,%d)\n", This, index, context);
2578 if (cti2_get_var_count(This->typeinfo) <= index)
2579 return TYPE_E_ELEMENTNOTFOUND;
2581 for (iter = This->typedata->next->next; iter != This->typedata->next; iter = iter->next)
2582 if (iter->type == CyclicListVar)
2584 if (index-- == 0)
2586 ctl2_update_var_size(This, iter, FIELD_OFFSET(MSFT_VarRecord, HelpString));
2587 iter->u.data[5] = context;
2588 return S_OK;
2592 return TYPE_E_ELEMENTNOTFOUND;
2595 /******************************************************************************
2596 * ICreateTypeInfo2_SetMops {OLEAUT32}
2598 static HRESULT WINAPI ICreateTypeInfo2_fnSetMops(
2599 ICreateTypeInfo2* iface,
2600 UINT index,
2601 BSTR bstrMops)
2603 FIXME("(%p,%d,%p), stub!\n", iface, index, bstrMops);
2604 return E_OUTOFMEMORY;
2607 /******************************************************************************
2608 * ICreateTypeInfo2_SetTypeIdldesc {OLEAUT32}
2610 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeIdldesc(
2611 ICreateTypeInfo2* iface,
2612 IDLDESC* pIdlDesc)
2614 FIXME("(%p,%p), stub!\n", iface, pIdlDesc);
2615 return E_OUTOFMEMORY;
2618 /******************************************************************************
2619 * ICreateTypeInfo2_LayOut {OLEAUT32}
2621 static HRESULT WINAPI ICreateTypeInfo2_fnLayOut(
2622 ICreateTypeInfo2* iface)
2624 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2625 CyclicList *iter, *iter2, *last = NULL, **typedata;
2626 HREFTYPE hreftype;
2627 HRESULT hres;
2628 unsigned user_vft = 0;
2629 int i;
2631 TRACE("(%p)\n", This);
2633 /* FIXME: LayOut should be run on all ImplTypes */
2634 if(This->typekind == TKIND_COCLASS || This->typekind == TKIND_ALIAS)
2635 return S_OK;
2637 /* Validate inheritance */
2638 This->typeinfo->datatype2 = 0;
2639 hreftype = This->typeinfo->datatype1;
2641 /* Process internally defined interfaces */
2642 for(i=0; i<This->typelib->typelib_header.nrtypeinfos; i++) {
2643 MSFT_TypeInfoBase *header;
2645 if(hreftype&1)
2646 break;
2648 header = (MSFT_TypeInfoBase*)&(This->typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][hreftype]);
2649 This->typeinfo->datatype2 += (header->cElement<<16) + 1;
2650 hreftype = header->datatype1;
2652 if(i == This->typelib->typelib_header.nrtypeinfos)
2653 return TYPE_E_CIRCULARTYPE;
2655 /* Process externally defined interfaces */
2656 if(hreftype != -1) {
2657 ITypeInfo *cur, *next;
2658 TYPEATTR *typeattr;
2660 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&next);
2661 if(FAILED(hres))
2662 return hres;
2664 hres = ITypeInfo_GetRefTypeInfo(next, hreftype, &cur);
2665 ITypeInfo_Release(next);
2666 if(FAILED(hres))
2667 return hres;
2670 while(1) {
2671 hres = ITypeInfo_GetTypeAttr(cur, &typeattr);
2672 if(FAILED(hres)) {
2673 ITypeInfo_Release(cur);
2674 return hres;
2677 if(IsEqualGUID(&typeattr->guid, &IID_IDispatch))
2678 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2680 This->typeinfo->datatype2 += (typeattr->cFuncs<<16) + 1;
2681 ITypeInfo_ReleaseTypeAttr(cur, typeattr);
2683 hres = ITypeInfo_GetRefTypeOfImplType(cur, 0, &hreftype);
2684 if(hres == TYPE_E_ELEMENTNOTFOUND)
2685 break;
2686 if(FAILED(hres)) {
2687 ITypeInfo_Release(cur);
2688 return hres;
2691 hres = ITypeInfo_GetRefTypeInfo(cur, hreftype, &next);
2692 if(FAILED(hres)) {
2693 ITypeInfo_Release(cur);
2694 return hres;
2697 ITypeInfo_Release(cur);
2698 cur = next;
2700 ITypeInfo_Release(cur);
2703 /* Get cbSizeVft of inherited interface */
2704 /* Makes LayOut running recursively */
2705 if(This->typeinfo->datatype1 != -1) {
2706 ITypeInfo *cur, *inherited;
2707 TYPEATTR *typeattr;
2709 hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&cur);
2710 if(FAILED(hres))
2711 return hres;
2713 hres = ITypeInfo_GetRefTypeInfo(cur, This->typeinfo->datatype1, &inherited);
2714 ITypeInfo_Release(cur);
2715 if(FAILED(hres))
2716 return hres;
2718 hres = ITypeInfo_GetTypeAttr(inherited, &typeattr);
2719 if(FAILED(hres)) {
2720 ITypeInfo_Release(inherited);
2721 return hres;
2724 This->typeinfo->cbSizeVft = typeattr->cbSizeVft * 4 / sizeof(void *);
2726 ITypeInfo_ReleaseTypeAttr(inherited, typeattr);
2727 ITypeInfo_Release(inherited);
2728 } else
2729 This->typeinfo->cbSizeVft = 0;
2731 if(!This->typedata)
2732 return S_OK;
2734 typedata = heap_alloc(sizeof(CyclicList*)*cti2_get_func_count(This->typeinfo));
2735 if(!typedata)
2736 return E_OUTOFMEMORY;
2738 /* Assign IDs and VTBL entries */
2739 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next)
2740 if (iter->type == CyclicListFunc)
2741 last = iter;
2743 if(last && last->u.data[3]&1)
2744 user_vft = last->u.data[3]&0xffff;
2746 i = 0;
2747 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
2748 /* Assign MEMBERID if MEMBERID_NIL was specified */
2749 if(iter->indice == MEMBERID_NIL) {
2750 iter->indice = 0x60000000 + i + (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 = 0x60000000 + This->typeinfo->cElement + (This->typeinfo->datatype2<<16);
2757 for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2758 if(iter == iter2) continue;
2759 if(iter2->indice == iter->indice) {
2760 ++iter->indice;
2761 iter2 = This->typedata->next;
2765 break;
2770 if (iter->type != CyclicListFunc)
2771 continue;
2773 typedata[i] = iter;
2775 iter->u.data[0] = ctl2_get_record_size(iter) | (i<<16);
2777 if((iter->u.data[3]&1) != (user_vft&1)) {
2778 heap_free(typedata);
2779 return TYPE_E_INVALIDID;
2782 if(user_vft&1) {
2783 if(user_vft < (iter->u.data[3]&0xffff))
2784 user_vft = (iter->u.data[3]&0xffff);
2786 if((iter->u.data[3]&0xffff) < This->typeinfo->cbSizeVft) {
2787 heap_free(typedata);
2788 return TYPE_E_INVALIDID;
2790 } else if(This->typekind != TKIND_MODULE) {
2791 iter->u.data[3] = (iter->u.data[3]&0xffff0000) | This->typeinfo->cbSizeVft;
2792 This->typeinfo->cbSizeVft += 4;
2795 /* Construct a list of elements with the same memberid */
2796 iter->u.data[4] = (iter->u.data[4]&0xffff) | (i<<16);
2797 for(iter2=This->typedata->next->next; iter2!=iter; iter2=iter2->next) {
2798 if(iter->indice == iter2->indice) {
2799 int v1, v2;
2801 v1 = iter->u.data[4] >> 16;
2802 v2 = iter2->u.data[4] >> 16;
2804 iter->u.data[4] = (iter->u.data[4]&0xffff) | (v2<<16);
2805 iter2->u.data[4] = (iter2->u.data[4]&0xffff) | (v1<<16);
2806 break;
2810 i++;
2813 if(user_vft)
2814 This->typeinfo->cbSizeVft = user_vft+3;
2816 for(i=0; i< cti2_get_func_count(This->typeinfo); i++) {
2817 if(typedata[i]->u.data[4]>>16 > i) {
2818 INVOKEKIND inv = ctl2_get_invokekind(typedata[i]);
2820 i = typedata[i]->u.data[4] >> 16;
2822 while(i > typedata[i]->u.data[4]>>16) {
2823 INVOKEKIND invkind = ctl2_get_invokekind(typedata[i]);
2825 if(inv & invkind) {
2826 heap_free(typedata);
2827 return TYPE_E_DUPLICATEID;
2830 i = typedata[i]->u.data[4] >> 16;
2831 inv |= invkind;
2834 if(inv & INVOKE_FUNC) {
2835 heap_free(typedata);
2836 return TYPE_E_INCONSISTENTPROPFUNCS;
2841 heap_free(typedata);
2842 return S_OK;
2845 /******************************************************************************
2846 * ICreateTypeInfo2_DeleteFuncDesc {OLEAUT32}
2848 * Delete a function description from a type.
2850 * RETURNS
2852 * Success: S_OK.
2853 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2855 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDesc(
2856 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2857 UINT index) /* [I] The index of the function to delete. */
2859 FIXME("(%p,%d), stub!\n", iface, index);
2860 return E_OUTOFMEMORY;
2863 /******************************************************************************
2864 * ICreateTypeInfo2_DeleteFuncDescByMemId {OLEAUT32}
2866 * Delete a function description from a type.
2868 * RETURNS
2870 * Success: S_OK.
2871 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2873 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDescByMemId(
2874 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2875 MEMBERID memid, /* [I] The member id of the function to delete. */
2876 INVOKEKIND invKind) /* [I] The invocation type of the function to delete. (?) */
2878 FIXME("(%p,%d,%d), stub!\n", iface, memid, invKind);
2879 return E_OUTOFMEMORY;
2882 /******************************************************************************
2883 * ICreateTypeInfo2_DeleteVarDesc {OLEAUT32}
2885 * Delete a variable description from a type.
2887 * RETURNS
2889 * Success: S_OK.
2890 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2891 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2893 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDesc(
2894 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2895 UINT index) /* [I] The index of the variable description to delete. */
2897 FIXME("(%p,%d), stub!\n", iface, index);
2898 return E_OUTOFMEMORY;
2901 /******************************************************************************
2902 * ICreateTypeInfo2_DeleteVarDescByMemId {OLEAUT32}
2904 * Delete a variable description from a type.
2906 * RETURNS
2908 * Success: S_OK.
2909 * Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2910 * TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2912 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDescByMemId(
2913 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2914 MEMBERID memid) /* [I] The member id of the variable description to delete. */
2916 FIXME("(%p,%d), stub!\n", iface, memid);
2917 return E_OUTOFMEMORY;
2920 /******************************************************************************
2921 * ICreateTypeInfo2_DeleteImplType {OLEAUT32}
2923 * Delete an interface implementation from a type. (?)
2925 * RETURNS
2927 * Success: S_OK.
2928 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2930 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteImplType(
2931 ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete. */
2932 UINT index) /* [I] The index of the interface to delete. */
2934 FIXME("(%p,%d), stub!\n", iface, index);
2935 return E_OUTOFMEMORY;
2938 /******************************************************************************
2939 * ICreateTypeInfo2_SetCustData {OLEAUT32}
2941 * Set the custom data for a type.
2943 * RETURNS
2945 * Success: S_OK.
2946 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2948 static HRESULT WINAPI ICreateTypeInfo2_fnSetCustData(
2949 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2950 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2951 VARIANT* pVarVal) /* [I] The custom data. */
2953 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2955 TRACE("(%p,%s,%p)!\n", iface, debugstr_guid(guid), pVarVal);
2957 if (!pVarVal)
2958 return E_INVALIDARG;
2960 return ctl2_set_custdata(This->typelib, guid, pVarVal, &This->typeinfo->oCustData);
2963 /******************************************************************************
2964 * ICreateTypeInfo2_SetFuncCustData {OLEAUT32}
2966 * Set the custom data for a function.
2968 * RETURNS
2970 * Success: S_OK.
2971 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2973 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncCustData(
2974 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2975 UINT index, /* [I] The index of the function for which to set the custom data. */
2976 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
2977 VARIANT* pVarVal) /* [I] The custom data. */
2979 ICreateTypeInfo2Impl *This = impl_from_ICreateTypeInfo2(iface);
2980 CyclicList *iter;
2982 TRACE("(%p,%d,%s,%p)\n", iface, index, debugstr_guid(guid), pVarVal);
2984 if(index >= cti2_get_func_count(This->typeinfo))
2985 return TYPE_E_ELEMENTNOTFOUND;
2987 for(iter=This->typedata->next->next; /* empty */; iter=iter->next)
2988 if (iter->type == CyclicListFunc)
2989 if (index-- == 0)
2990 break;
2992 This->typedata->next->u.val += funcrecord_reallochdr(&iter->u.data, 13*sizeof(int));
2993 if(!iter->u.data)
2994 return E_OUTOFMEMORY;
2996 iter->u.data[4] |= 0x80;
2997 return ctl2_set_custdata(This->typelib, guid, pVarVal, &iter->u.data[12]);
3000 /******************************************************************************
3001 * ICreateTypeInfo2_SetParamCustData {OLEAUT32}
3003 * Set the custom data for a function parameter.
3005 * RETURNS
3007 * Success: S_OK.
3008 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3010 static HRESULT WINAPI ICreateTypeInfo2_fnSetParamCustData(
3011 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3012 UINT indexFunc, /* [I] The index of the function on which the parameter resides. */
3013 UINT indexParam, /* [I] The index of the parameter on which to set the custom data. */
3014 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3015 VARIANT* pVarVal) /* [I] The custom data. */
3017 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3018 return E_OUTOFMEMORY;
3021 /******************************************************************************
3022 * ICreateTypeInfo2_SetVarCustData {OLEAUT32}
3024 * Set the custom data for a variable.
3026 * RETURNS
3028 * Success: S_OK.
3029 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3031 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarCustData(
3032 ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
3033 UINT index, /* [I] The index of the variable on which to set the custom data. */
3034 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3035 VARIANT* pVarVal) /* [I] The custom data. */
3037 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3038 return E_OUTOFMEMORY;
3041 /******************************************************************************
3042 * ICreateTypeInfo2_SetImplTypeCustData {OLEAUT32}
3044 * Set the custom data for an implemented interface.
3046 * RETURNS
3048 * Success: S_OK.
3049 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3051 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeCustData(
3052 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the custom data. */
3053 UINT index, /* [I] The index of the implemented interface on which to set the custom data. */
3054 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
3055 VARIANT* pVarVal) /* [I] The custom data. */
3057 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3058 return E_OUTOFMEMORY;
3061 /******************************************************************************
3062 * ICreateTypeInfo2_SetHelpStringContext {OLEAUT32}
3064 * Set the help string context for the typeinfo.
3066 * RETURNS
3068 * Success: S_OK.
3069 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3071 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpStringContext(
3072 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3073 ULONG dwHelpStringContext) /* [I] The help string context. */
3075 FIXME("(%p,%d), stub!\n", iface, dwHelpStringContext);
3076 return E_OUTOFMEMORY;
3079 /******************************************************************************
3080 * ICreateTypeInfo2_SetFuncHelpStringContext {OLEAUT32}
3082 * Set the help string context for a function.
3084 * RETURNS
3086 * Success: S_OK.
3087 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3089 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpStringContext(
3090 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3091 UINT index, /* [I] The index for the function on which to set the help string context. */
3092 ULONG dwHelpStringContext) /* [I] The help string context. */
3094 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3095 return E_OUTOFMEMORY;
3098 /******************************************************************************
3099 * ICreateTypeInfo2_SetVarHelpStringContext {OLEAUT32}
3101 * Set the help string context for a variable.
3103 * RETURNS
3105 * Success: S_OK.
3106 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3108 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpStringContext(
3109 ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the help string context. */
3110 UINT index, /* [I] The index of the variable on which to set the help string context. */
3111 ULONG dwHelpStringContext) /* [I] The help string context */
3113 FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
3114 return E_OUTOFMEMORY;
3117 /******************************************************************************
3118 * ICreateTypeInfo2_Invalidate {OLEAUT32}
3120 * Undocumented function. (!)
3122 static HRESULT WINAPI ICreateTypeInfo2_fnInvalidate(
3123 ICreateTypeInfo2* iface)
3125 FIXME("(%p), stub!\n", iface);
3126 return E_OUTOFMEMORY;
3129 /******************************************************************************
3130 * ICreateTypeInfo2_SetName {OLEAUT32}
3132 * Set the name for a typeinfo.
3134 * RETURNS
3136 * Success: S_OK.
3137 * Failure: One of STG_E_INSUFFICIENTMEMORY, E_OUTOFMEMORY, E_INVALIDARG or TYPE_E_INVALIDSTATE.
3139 static HRESULT WINAPI ICreateTypeInfo2_fnSetName(
3140 ICreateTypeInfo2* iface,
3141 LPOLESTR szName)
3143 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
3144 return E_OUTOFMEMORY;
3147 /*================== ITypeInfo2 Implementation ===================================*/
3149 /******************************************************************************
3150 * ITypeInfo2_QueryInterface {OLEAUT32}
3152 static HRESULT WINAPI ITypeInfo2_fnQueryInterface(ITypeInfo2 * iface, REFIID riid, LPVOID * ppv)
3154 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3156 return ICreateTypeInfo2_QueryInterface(&This->ICreateTypeInfo2_iface, riid, ppv);
3159 /******************************************************************************
3160 * ITypeInfo2_AddRef {OLEAUT32}
3162 static ULONG WINAPI ITypeInfo2_fnAddRef(ITypeInfo2 * iface)
3164 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3166 return ICreateTypeInfo2_AddRef(&This->ICreateTypeInfo2_iface);
3169 /******************************************************************************
3170 * ITypeInfo2_Release {OLEAUT32}
3172 static ULONG WINAPI ITypeInfo2_fnRelease(ITypeInfo2 * iface)
3174 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3176 return ICreateTypeInfo2_Release(&This->ICreateTypeInfo2_iface);
3179 /******************************************************************************
3180 * ITypeInfo2_GetTypeAttr {OLEAUT32}
3182 static HRESULT WINAPI ITypeInfo2_fnGetTypeAttr(
3183 ITypeInfo2* iface,
3184 TYPEATTR** ppTypeAttr)
3186 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3187 HRESULT hres;
3189 TRACE("(%p,%p)\n", iface, ppTypeAttr);
3191 if(!ppTypeAttr)
3192 return E_INVALIDARG;
3194 hres = ICreateTypeInfo_LayOut(&This->ICreateTypeInfo2_iface);
3195 if(FAILED(hres))
3196 return hres;
3198 *ppTypeAttr = heap_alloc_zero(sizeof(TYPEATTR));
3199 if(!*ppTypeAttr)
3200 return E_OUTOFMEMORY;
3202 if(This->typeinfo->posguid != -1) {
3203 MSFT_GuidEntry *guid;
3205 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][This->typeinfo->posguid];
3206 (*ppTypeAttr)->guid = guid->guid;
3209 (*ppTypeAttr)->lcid = This->typelib->typelib_header.lcid;
3210 (*ppTypeAttr)->cbSizeInstance = This->typeinfo->size;
3211 (*ppTypeAttr)->typekind = This->typekind;
3212 (*ppTypeAttr)->cFuncs = cti2_get_func_count(This->typeinfo);
3213 if(This->typeinfo->flags&TYPEFLAG_FDUAL && This->typekind==TKIND_DISPATCH)
3214 (*ppTypeAttr)->cFuncs += sizeof(IDispatchVtbl)/sizeof(void*);
3215 (*ppTypeAttr)->cVars = cti2_get_var_count(This->typeinfo);
3216 (*ppTypeAttr)->cImplTypes = This->typeinfo->cImplTypes;
3217 (*ppTypeAttr)->cbSizeVft = This->typekind == TKIND_DISPATCH ? sizeof(IDispatchVtbl) : This->typeinfo->cbSizeVft;
3218 (*ppTypeAttr)->cbAlignment = (This->typeinfo->typekind>>11) & 0x1f;
3219 (*ppTypeAttr)->wTypeFlags = This->typeinfo->flags;
3220 (*ppTypeAttr)->wMajorVerNum = LOWORD(This->typeinfo->version);
3221 (*ppTypeAttr)->wMinorVerNum = HIWORD(This->typeinfo->version);
3223 if((*ppTypeAttr)->typekind == TKIND_ALIAS)
3224 FIXME("TKIND_ALIAS handling not implemented\n");
3226 return S_OK;
3229 /******************************************************************************
3230 * ITypeInfo2_GetTypeComp {OLEAUT32}
3232 static HRESULT WINAPI ITypeInfo2_fnGetTypeComp(
3233 ITypeInfo2* iface,
3234 ITypeComp** ppTComp)
3236 FIXME("(%p,%p), stub!\n", iface, ppTComp);
3237 return E_OUTOFMEMORY;
3240 /******************************************************************************
3241 * ITypeInfo2_GetFuncDesc {OLEAUT32}
3243 static HRESULT WINAPI ITypeInfo2_fnGetFuncDesc(
3244 ITypeInfo2* iface,
3245 UINT index,
3246 FUNCDESC** ppFuncDesc)
3248 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3249 int i, *typedata, num_defaults = 0, hdr_len, tail, has_defaults;
3250 CyclicList *desc;
3251 HRESULT hres;
3253 TRACE("(%p,%d,%p), semi-stub\n", iface, index, ppFuncDesc);
3255 if (!ppFuncDesc)
3256 return E_INVALIDARG;
3258 if (index >= cti2_get_func_count(This->typeinfo))
3259 return TYPE_E_ELEMENTNOTFOUND;
3261 hres = ICreateTypeInfo2_LayOut(&This->ICreateTypeInfo2_iface);
3262 if (FAILED(hres))
3263 return hres;
3265 desc = This->typedata->next;
3266 for (i = index; i >= 0; ) {
3267 desc = desc->next;
3268 if (desc->type == CyclicListFunc)
3269 --i;
3272 typedata = desc->u.data;
3274 *ppFuncDesc = heap_alloc_zero(sizeof(FUNCDESC));
3275 if (!*ppFuncDesc)
3276 return E_OUTOFMEMORY;
3278 (*ppFuncDesc)->memid = desc->indice;
3279 (*ppFuncDesc)->lprgscode = NULL; /* FIXME: Unimplemented */
3280 (*ppFuncDesc)->funckind = typedata[4] & 0x7;
3281 (*ppFuncDesc)->invkind = (typedata[4] >> 3) & 0xF;
3282 (*ppFuncDesc)->callconv = (typedata[4] >> 8) & 0xF;
3283 (*ppFuncDesc)->cParams = typedata[5];
3284 (*ppFuncDesc)->cParamsOpt = 0; /* FIXME: Unimplemented*/
3285 (*ppFuncDesc)->oVft = typedata[3] & 0xFFFF;
3286 if ((*ppFuncDesc)->oVft)
3287 --(*ppFuncDesc)->oVft;
3288 (*ppFuncDesc)->cScodes = 0; /* FIXME: Unimplemented*/
3289 hres = ctl2_decode_typedesc(This->typelib, typedata[1],
3290 &(*ppFuncDesc)->elemdescFunc.tdesc);
3291 if (FAILED(hres)) {
3292 heap_free(*ppFuncDesc);
3293 return hres;
3295 (*ppFuncDesc)->wFuncFlags = typedata[2];
3297 has_defaults = typedata[4] & 0x1000;
3298 tail = typedata[5] * (has_defaults ? 16 : 12);
3299 hdr_len = (ctl2_get_record_size(desc) - tail) / sizeof(int);
3301 if ((*ppFuncDesc)->cParams > 0) {
3302 (*ppFuncDesc)->lprgelemdescParam = heap_alloc_zero((*ppFuncDesc)->cParams * sizeof(ELEMDESC));
3303 if (!(*ppFuncDesc)->lprgelemdescParam) {
3304 heap_free(*ppFuncDesc);
3305 return E_OUTOFMEMORY;
3307 if (has_defaults) {
3308 num_defaults = (*ppFuncDesc)->cParams;
3310 for (i = 0; i < num_defaults; ++i) {
3311 if (typedata[hdr_len + i] != 0xFFFFFFFF) {
3312 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags |= PARAMFLAG_FHASDEFAULT;
3314 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex = heap_alloc(sizeof(PARAMDESCEX));
3315 if (!(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex) {
3316 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3317 return E_OUTOFMEMORY;
3320 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->cBytes = sizeof(PARAMDESCEX);
3321 VariantInit(&(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3322 hres = ctl2_decode_variant(This->typelib, typedata[hdr_len + i],
3323 &(*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3324 if (FAILED(hres)) {
3325 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3326 return hres;
3332 for (i = 0; i < (*ppFuncDesc)->cParams; ++i) {
3333 hres = ctl2_decode_typedesc(This->typelib, typedata[hdr_len + num_defaults + (i * 3)],
3334 &((*ppFuncDesc)->lprgelemdescParam + i)->tdesc);
3335 if (FAILED(hres)) {
3336 ITypeInfo2_ReleaseFuncDesc(iface, *ppFuncDesc);
3337 return hres;
3339 (*ppFuncDesc)->lprgelemdescParam[i].u.paramdesc.wParamFlags = typedata[hdr_len + num_defaults + (i * 3) + 2];
3343 return S_OK;
3346 /******************************************************************************
3347 * ITypeInfo2_GetVarDesc {OLEAUT32}
3349 static HRESULT WINAPI ITypeInfo2_fnGetVarDesc(
3350 ITypeInfo2* iface,
3351 UINT index,
3352 VARDESC** ppVarDesc)
3354 FIXME("(%p,%d,%p), stub!\n", iface, index, ppVarDesc);
3355 return E_OUTOFMEMORY;
3358 /******************************************************************************
3359 * ITypeInfo2_GetNames {OLEAUT32}
3361 static HRESULT WINAPI ITypeInfo2_fnGetNames(
3362 ITypeInfo2* iface,
3363 MEMBERID memid,
3364 BSTR* rgBstrNames,
3365 UINT cMaxNames,
3366 UINT* pcNames)
3368 FIXME("(%p,%d,%p,%d,%p), stub!\n", iface, memid, rgBstrNames, cMaxNames, pcNames);
3369 return E_OUTOFMEMORY;
3372 /******************************************************************************
3373 * ITypeInfo2_GetRefTypeOfImplType {OLEAUT32}
3375 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeOfImplType(
3376 ITypeInfo2* iface,
3377 UINT index,
3378 HREFTYPE* pRefType)
3380 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3381 MSFT_RefRecord *ref;
3382 int offset;
3384 TRACE("(%p,%d,%p)\n", iface, index, pRefType);
3386 if(!pRefType)
3387 return E_INVALIDARG;
3389 if(This->typeinfo->flags&TYPEFLAG_FDUAL) {
3390 if(index == -1) {
3391 *pRefType = -2;
3392 return S_OK;
3395 if(This->typekind == TKIND_DISPATCH)
3396 return ITypeInfo2_GetRefTypeOfImplType(&This->dual->ITypeInfo2_iface,
3397 index, pRefType);
3400 if(index>=This->typeinfo->cImplTypes)
3401 return TYPE_E_ELEMENTNOTFOUND;
3403 if(This->typekind == TKIND_INTERFACE) {
3404 *pRefType = This->typeinfo->datatype1 + 2;
3405 return S_OK;
3408 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3409 if(offset == -1)
3410 return TYPE_E_ELEMENTNOTFOUND;
3412 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3413 *pRefType = ref->reftype;
3414 return S_OK;
3417 /******************************************************************************
3418 * ITypeInfo2_GetImplTypeFlags {OLEAUT32}
3420 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeFlags(
3421 ITypeInfo2* iface,
3422 UINT index,
3423 INT* pImplTypeFlags)
3425 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3426 int offset;
3427 MSFT_RefRecord *ref;
3429 TRACE("(%p,%d,%p)\n", iface, index, pImplTypeFlags);
3431 if(!pImplTypeFlags)
3432 return E_INVALIDARG;
3434 if(index >= This->typeinfo->cImplTypes)
3435 return TYPE_E_ELEMENTNOTFOUND;
3437 if(This->typekind != TKIND_COCLASS) {
3438 *pImplTypeFlags = 0;
3439 return S_OK;
3442 offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3443 if(offset == -1)
3444 return TYPE_E_ELEMENTNOTFOUND;
3446 ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3447 *pImplTypeFlags = ref->flags;
3448 return S_OK;
3451 /******************************************************************************
3452 * ITypeInfo2_GetIDsOfNames {OLEAUT32}
3454 static HRESULT WINAPI ITypeInfo2_fnGetIDsOfNames(
3455 ITypeInfo2* iface,
3456 LPOLESTR* rgszNames,
3457 UINT cNames,
3458 MEMBERID* pMemId)
3460 FIXME("(%p,%p,%d,%p), stub!\n", iface, rgszNames, cNames, pMemId);
3461 return E_OUTOFMEMORY;
3464 /******************************************************************************
3465 * ITypeInfo2_Invoke {OLEAUT32}
3467 static HRESULT WINAPI ITypeInfo2_fnInvoke(
3468 ITypeInfo2* iface,
3469 PVOID pvInstance,
3470 MEMBERID memid,
3471 WORD wFlags,
3472 DISPPARAMS* pDispParams,
3473 VARIANT* pVarResult,
3474 EXCEPINFO* pExcepInfo,
3475 UINT* puArgErr)
3477 FIXME("(%p,%p,%d,%x,%p,%p,%p,%p), stub!\n", iface, pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
3478 return E_OUTOFMEMORY;
3481 /******************************************************************************
3482 * ITypeInfo2_GetDocumentation {OLEAUT32}
3484 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation(
3485 ITypeInfo2* iface,
3486 MEMBERID memid,
3487 BSTR* pBstrName,
3488 BSTR* pBstrDocString,
3489 DWORD* pdwHelpContext,
3490 BSTR* pBstrHelpFile)
3492 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3493 HRESULT status = TYPE_E_ELEMENTNOTFOUND;
3494 INT nameoffset, docstringoffset, helpcontext;
3496 TRACE("(%p,%d,%p,%p,%p,%p)\n", iface, memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
3498 if (memid == -1)
3500 nameoffset = This->typeinfo->NameOffset;
3501 docstringoffset = This->typeinfo->docstringoffs;
3502 helpcontext = This->typeinfo->helpcontext;
3503 status = S_OK;
3504 } else {
3505 CyclicList *iter;
3506 if (This->typedata) {
3507 for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
3508 if (iter->indice == memid) {
3509 if (iter->type == CyclicListFunc) {
3510 const int *typedata = iter->u.data;
3511 int size = ctl2_get_record_size(iter) - typedata[5]*(typedata[4]&0x1000?16:12);
3513 nameoffset = iter->name;
3514 /* FIXME implement this once SetFuncDocString is implemented */
3515 docstringoffset = -1;
3516 helpcontext = (size < 7*sizeof(int)) ? 0 : typedata[6];
3518 status = S_OK;
3519 } else {
3520 FIXME("Not implemented for variable members\n");
3523 break;
3529 if (!status) {
3530 WCHAR *string;
3531 if (pBstrName) {
3532 if (nameoffset == -1)
3533 *pBstrName = NULL;
3534 else {
3535 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3536 typelib_segment_data[MSFT_SEG_NAME][nameoffset];
3537 ctl2_decode_name((char*)&name->namelen, &string);
3538 *pBstrName = SysAllocString(string);
3539 if(!*pBstrName)
3540 return E_OUTOFMEMORY;
3544 if (pBstrDocString) {
3545 if (docstringoffset == -1)
3546 *pBstrDocString = NULL;
3547 else {
3548 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->typelib->
3549 typelib_segment_data[MSFT_SEG_NAME][docstringoffset];
3550 ctl2_decode_name((char*)&name->namelen, &string);
3551 *pBstrDocString = SysAllocString(string);
3552 if(!*pBstrDocString) {
3553 if (pBstrName) SysFreeString(*pBstrName);
3554 return E_OUTOFMEMORY;
3559 if (pdwHelpContext) {
3560 *pdwHelpContext = helpcontext;
3563 if (pBstrHelpFile) {
3564 status = ITypeLib_GetDocumentation((ITypeLib*)&This->typelib->lpVtblTypeLib2,
3565 -1, NULL, NULL, NULL, pBstrHelpFile);
3566 if (status) {
3567 if (pBstrName) SysFreeString(*pBstrName);
3568 if (pBstrDocString) SysFreeString(*pBstrDocString);
3573 return status;
3576 /******************************************************************************
3577 * ITypeInfo2_GetDllEntry {OLEAUT32}
3579 static HRESULT WINAPI ITypeInfo2_fnGetDllEntry(
3580 ITypeInfo2* iface,
3581 MEMBERID memid,
3582 INVOKEKIND invKind,
3583 BSTR* pBstrDllName,
3584 BSTR* pBstrName,
3585 WORD* pwOrdinal)
3587 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
3588 return E_OUTOFMEMORY;
3591 /******************************************************************************
3592 * ITypeInfo2_GetRefTypeInfo {OLEAUT32}
3594 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeInfo(
3595 ITypeInfo2* iface,
3596 HREFTYPE hRefType,
3597 ITypeInfo** ppTInfo)
3599 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3601 TRACE("(%p,%d,%p)\n", iface, hRefType, ppTInfo);
3603 if(!ppTInfo)
3604 return E_INVALIDARG;
3606 if(hRefType==-2 && This->dual) {
3607 *ppTInfo = (ITypeInfo *)&This->dual->ITypeInfo2_iface;
3608 ITypeInfo_AddRef(*ppTInfo);
3609 return S_OK;
3612 if(hRefType&1) {
3613 ITypeLib *tl;
3614 MSFT_ImpInfo *impinfo;
3615 MSFT_ImpFile *impfile;
3616 MSFT_GuidEntry *guid;
3617 WCHAR *filename;
3618 HRESULT hres;
3620 if((hRefType&(~0x3)) >= This->typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length)
3621 return E_FAIL;
3623 impinfo = (MSFT_ImpInfo*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][hRefType&(~0x3)];
3624 impfile = (MSFT_ImpFile*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][impinfo->oImpFile];
3625 guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo->oGuid];
3627 ctl2_decode_string((unsigned char*)impfile->filename, &filename);
3629 hres = LoadTypeLib(filename, &tl);
3630 if(FAILED(hres))
3631 return hres;
3633 hres = ITypeLib_GetTypeInfoOfGuid(tl, &guid->guid, ppTInfo);
3635 ITypeLib_Release(tl);
3636 return hres;
3637 } else {
3638 ICreateTypeInfo2Impl *iter;
3639 int i = 0;
3641 for(iter=This->typelib->typeinfos; iter; iter=iter->next_typeinfo) {
3642 if(This->typelib->typelib_typeinfo_offsets[i] == (hRefType&(~0x3))) {
3643 *ppTInfo = (ITypeInfo *)&iter->ITypeInfo2_iface;
3645 ITypeLib_AddRef(*ppTInfo);
3646 return S_OK;
3648 i++;
3652 return E_FAIL;
3655 /******************************************************************************
3656 * ITypeInfo2_AddressOfMember {OLEAUT32}
3658 static HRESULT WINAPI ITypeInfo2_fnAddressOfMember(
3659 ITypeInfo2* iface,
3660 MEMBERID memid,
3661 INVOKEKIND invKind,
3662 PVOID* ppv)
3664 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, ppv);
3665 return E_OUTOFMEMORY;
3668 /******************************************************************************
3669 * ITypeInfo2_CreateInstance {OLEAUT32}
3671 static HRESULT WINAPI ITypeInfo2_fnCreateInstance(
3672 ITypeInfo2* iface,
3673 IUnknown* pUnkOuter,
3674 REFIID riid,
3675 PVOID* ppvObj)
3677 FIXME("(%p,%p,%s,%p), stub!\n", iface, pUnkOuter, debugstr_guid(riid), ppvObj);
3678 return E_OUTOFMEMORY;
3681 /******************************************************************************
3682 * ITypeInfo2_GetMops {OLEAUT32}
3684 static HRESULT WINAPI ITypeInfo2_fnGetMops(
3685 ITypeInfo2* iface,
3686 MEMBERID memid,
3687 BSTR* pBstrMops)
3689 FIXME("(%p,%d,%p), stub!\n", iface, memid, pBstrMops);
3690 return E_OUTOFMEMORY;
3693 /******************************************************************************
3694 * ITypeInfo2_GetContainingTypeLib {OLEAUT32}
3696 static HRESULT WINAPI ITypeInfo2_fnGetContainingTypeLib(
3697 ITypeInfo2* iface,
3698 ITypeLib** ppTLib,
3699 UINT* pIndex)
3701 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3703 TRACE("(%p,%p,%p)\n", iface, ppTLib, pIndex);
3705 *ppTLib = (ITypeLib *)&This->typelib->lpVtblTypeLib2;
3706 ICreateTypeLib_AddRef((ICreateTypeLib*)This->typelib);
3707 *pIndex = This->typeinfo->typekind >> 16;
3709 return S_OK;
3712 static void release_typedesc(TYPEDESC *tdesc)
3714 while (tdesc) {
3715 TYPEDESC *next;
3716 if (tdesc->vt == VT_USERDEFINED)
3717 next = NULL;
3718 else
3719 next = tdesc->u.lptdesc;
3720 heap_free(tdesc);
3721 tdesc = next;
3725 /******************************************************************************
3726 * ITypeInfo2_ReleaseTypeAttr {OLEAUT32}
3728 static void WINAPI ITypeInfo2_fnReleaseTypeAttr(
3729 ITypeInfo2* iface,
3730 TYPEATTR* pTypeAttr)
3732 TRACE("(%p,%p)\n", iface, pTypeAttr);
3734 if (pTypeAttr->tdescAlias.vt != VT_USERDEFINED)
3735 release_typedesc(pTypeAttr->tdescAlias.u.lptdesc);
3737 heap_free(pTypeAttr);
3740 /******************************************************************************
3741 * ITypeInfo2_ReleaseFuncDesc {OLEAUT32}
3743 static void WINAPI ITypeInfo2_fnReleaseFuncDesc(
3744 ITypeInfo2* iface,
3745 FUNCDESC* pFuncDesc)
3747 int i;
3749 TRACE("(%p,%p)\n", iface, pFuncDesc);
3751 heap_free(pFuncDesc->lprgscode);
3753 if (pFuncDesc->lprgelemdescParam) {
3754 for (i = 0; i < pFuncDesc->cParams; ++i) {
3755 if (pFuncDesc->lprgelemdescParam[i].tdesc.vt != VT_USERDEFINED)
3756 release_typedesc(pFuncDesc->lprgelemdescParam[i].tdesc.u.lptdesc);
3758 if (pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex)
3760 VariantClear(&pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue);
3761 heap_free(pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex);
3764 heap_free(pFuncDesc->lprgelemdescParam);
3767 heap_free(pFuncDesc->elemdescFunc.u.paramdesc.pparamdescex);
3769 if (pFuncDesc->elemdescFunc.tdesc.vt != VT_USERDEFINED)
3770 release_typedesc(pFuncDesc->elemdescFunc.tdesc.u.lptdesc);
3772 heap_free(pFuncDesc);
3775 /******************************************************************************
3776 * ITypeInfo2_ReleaseVarDesc {OLEAUT32}
3778 static void WINAPI ITypeInfo2_fnReleaseVarDesc(
3779 ITypeInfo2* iface,
3780 VARDESC* pVarDesc)
3782 FIXME("(%p,%p), stub!\n", iface, pVarDesc);
3785 /******************************************************************************
3786 * ITypeInfo2_GetTypeKind {OLEAUT32}
3788 * Get the TYPEKIND value for a TypeInfo.
3790 * RETURNS
3792 * Success: S_OK.
3793 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3795 static HRESULT WINAPI ITypeInfo2_fnGetTypeKind(
3796 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typekind for. */
3797 TYPEKIND* pTypeKind) /* [O] The typekind for this TypeInfo. */
3799 FIXME("(%p,%p), stub!\n", iface, pTypeKind);
3800 return E_OUTOFMEMORY;
3803 /******************************************************************************
3804 * ITypeInfo2_GetTypeFlags {OLEAUT32}
3806 * Get the Type Flags for a TypeInfo.
3808 * RETURNS
3810 * Success: S_OK.
3811 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3813 static HRESULT WINAPI ITypeInfo2_fnGetTypeFlags(
3814 ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typeflags for. */
3815 ULONG* pTypeFlags) /* [O] The type flags for this TypeInfo. */
3817 FIXME("(%p,%p), stub!\n", iface, pTypeFlags);
3818 return E_OUTOFMEMORY;
3821 /******************************************************************************
3822 * ITypeInfo2_GetFuncIndexOfMemId {OLEAUT32}
3824 * Gets the index of a function given its member id.
3826 * RETURNS
3828 * Success: S_OK.
3829 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3831 static HRESULT WINAPI ITypeInfo2_fnGetFuncIndexOfMemId(
3832 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the function. */
3833 MEMBERID memid, /* [I] The member id for the function. */
3834 INVOKEKIND invKind, /* [I] The invocation kind for the function. */
3835 UINT* pFuncIndex) /* [O] The index of the function. */
3837 FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, pFuncIndex);
3838 return E_OUTOFMEMORY;
3841 /******************************************************************************
3842 * ITypeInfo2_GetVarIndexOfMemId {OLEAUT32}
3844 * Gets the index of a variable given its member id.
3846 * RETURNS
3848 * Success: S_OK.
3849 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3851 static HRESULT WINAPI ITypeInfo2_fnGetVarIndexOfMemId(
3852 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the variable. */
3853 MEMBERID memid, /* [I] The member id for the variable. */
3854 UINT* pVarIndex) /* [O] The index of the variable. */
3856 FIXME("(%p,%d,%p), stub!\n", iface, memid, pVarIndex);
3857 return E_OUTOFMEMORY;
3860 /******************************************************************************
3861 * ITypeInfo2_GetCustData {OLEAUT32}
3863 * Gets a custom data element from a TypeInfo.
3865 * RETURNS
3867 * Success: S_OK.
3868 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3870 static HRESULT WINAPI ITypeInfo2_fnGetCustData(
3871 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3872 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3873 VARIANT* pVarVal) /* [O] The custom data. */
3875 ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3876 MSFT_CDGuid *cdentry;
3877 int offset;
3879 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
3881 if (!guid || !pVarVal)
3882 return E_INVALIDARG;
3884 VariantClear(pVarVal);
3886 offset = ctl2_find_custdata(This->typelib, guid, This->typeinfo->oCustData);
3887 if (offset == -1)
3888 return S_OK;
3890 cdentry = (MSFT_CDGuid *)&This->typelib->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][offset];
3891 return ctl2_decode_variant(This->typelib, cdentry->DataOffset, pVarVal);
3894 /******************************************************************************
3895 * ITypeInfo2_GetFuncCustData {OLEAUT32}
3897 * Gets a custom data element from a function.
3899 * RETURNS
3901 * Success: S_OK.
3902 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3904 static HRESULT WINAPI ITypeInfo2_fnGetFuncCustData(
3905 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3906 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
3907 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3908 VARIANT* pVarVal) /* [O] The custom data. */
3910 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3911 return E_OUTOFMEMORY;
3914 /******************************************************************************
3915 * ITypeInfo2_GetParamCustData {OLEAUT32}
3917 * Gets a custom data element from a parameter.
3919 * RETURNS
3921 * Success: S_OK.
3922 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3924 static HRESULT WINAPI ITypeInfo2_fnGetParamCustData(
3925 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3926 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
3927 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
3928 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3929 VARIANT* pVarVal) /* [O] The custom data. */
3931 FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3932 return E_OUTOFMEMORY;
3935 /******************************************************************************
3936 * ITypeInfo2_GetVarCustData {OLEAUT32}
3938 * Gets a custom data element from a variable.
3940 * RETURNS
3942 * Success: S_OK.
3943 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3945 static HRESULT WINAPI ITypeInfo2_fnGetVarCustData(
3946 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3947 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
3948 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3949 VARIANT* pVarVal) /* [O] The custom data. */
3951 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3952 return E_OUTOFMEMORY;
3955 /******************************************************************************
3956 * ITypeInfo2_GetImplTypeCustData {OLEAUT32}
3958 * Gets a custom data element from an implemented type of a TypeInfo.
3960 * RETURNS
3962 * Success: S_OK.
3963 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3965 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeCustData(
3966 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3967 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
3968 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
3969 VARIANT* pVarVal) /* [O] The custom data. */
3971 FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3972 return E_OUTOFMEMORY;
3975 /******************************************************************************
3976 * ITypeInfo2_GetDocumentation2 {OLEAUT32}
3978 * Gets some documentation from a TypeInfo in a locale-aware fashion.
3980 * RETURNS
3982 * Success: S_OK.
3983 * Failure: One of STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
3985 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation2(
3986 ITypeInfo2* iface, /* [I] The TypeInfo to retrieve the documentation from. */
3987 MEMBERID memid, /* [I] The member id (why?). */
3988 LCID lcid, /* [I] The locale (why?). */
3989 BSTR* pbstrHelpString, /* [O] The help string. */
3990 DWORD* pdwHelpStringContext, /* [O] The help string context. */
3991 BSTR* pbstrHelpStringDll) /* [O] The help file name. */
3993 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
3994 return E_OUTOFMEMORY;
3997 /******************************************************************************
3998 * ITypeInfo2_GetAllCustData {OLEAUT32}
4000 * Gets all of the custom data associated with a TypeInfo.
4002 * RETURNS
4004 * Success: S_OK.
4005 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4007 static HRESULT WINAPI ITypeInfo2_fnGetAllCustData(
4008 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4009 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4011 FIXME("(%p,%p), stub!\n", iface, pCustData);
4012 return E_OUTOFMEMORY;
4015 /******************************************************************************
4016 * ITypeInfo2_GetAllFuncCustData {OLEAUT32}
4018 * Gets all of the custom data associated with a function.
4020 * RETURNS
4022 * Success: S_OK.
4023 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4025 static HRESULT WINAPI ITypeInfo2_fnGetAllFuncCustData(
4026 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4027 UINT index, /* [I] The index of the function for which to retrieve the custom data. */
4028 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4030 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4031 return E_OUTOFMEMORY;
4034 /******************************************************************************
4035 * ITypeInfo2_GetAllParamCustData {OLEAUT32}
4037 * Gets all of the custom data associated with a parameter.
4039 * RETURNS
4041 * Success: S_OK.
4042 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4044 static HRESULT WINAPI ITypeInfo2_fnGetAllParamCustData(
4045 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4046 UINT indexFunc, /* [I] The index of the function for which to retrieve the custom data. */
4047 UINT indexParam, /* [I] The index of the parameter for which to retrieve the custom data. */
4048 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4050 FIXME("(%p,%d,%d,%p), stub!\n", iface, indexFunc, indexParam, pCustData);
4051 return E_OUTOFMEMORY;
4054 /******************************************************************************
4055 * ITypeInfo2_GetAllVarCustData {OLEAUT32}
4057 * Gets all of the custom data associated with a variable.
4059 * RETURNS
4061 * Success: S_OK.
4062 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4064 static HRESULT WINAPI ITypeInfo2_fnGetAllVarCustData(
4065 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4066 UINT index, /* [I] The index of the variable for which to retrieve the custom data. */
4067 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4069 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4070 return E_OUTOFMEMORY;
4073 /******************************************************************************
4074 * ITypeInfo2_GetAllImplTypeCustData {OLEAUT32}
4076 * Gets all of the custom data associated with an implemented type.
4078 * RETURNS
4080 * Success: S_OK.
4081 * Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
4083 static HRESULT WINAPI ITypeInfo2_fnGetAllImplTypeCustData(
4084 ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
4085 UINT index, /* [I] The index of the implemented type for which to retrieve the custom data. */
4086 CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
4088 FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
4089 return E_OUTOFMEMORY;
4093 /*================== ICreateTypeInfo2 & ITypeInfo2 VTABLEs And Creation ===================================*/
4095 static const ICreateTypeInfo2Vtbl ctypeinfo2vt =
4098 ICreateTypeInfo2_fnQueryInterface,
4099 ICreateTypeInfo2_fnAddRef,
4100 ICreateTypeInfo2_fnRelease,
4102 ICreateTypeInfo2_fnSetGuid,
4103 ICreateTypeInfo2_fnSetTypeFlags,
4104 ICreateTypeInfo2_fnSetDocString,
4105 ICreateTypeInfo2_fnSetHelpContext,
4106 ICreateTypeInfo2_fnSetVersion,
4107 ICreateTypeInfo2_fnAddRefTypeInfo,
4108 ICreateTypeInfo2_fnAddFuncDesc,
4109 ICreateTypeInfo2_fnAddImplType,
4110 ICreateTypeInfo2_fnSetImplTypeFlags,
4111 ICreateTypeInfo2_fnSetAlignment,
4112 ICreateTypeInfo2_fnSetSchema,
4113 ICreateTypeInfo2_fnAddVarDesc,
4114 ICreateTypeInfo2_fnSetFuncAndParamNames,
4115 ICreateTypeInfo2_fnSetVarName,
4116 ICreateTypeInfo2_fnSetTypeDescAlias,
4117 ICreateTypeInfo2_fnDefineFuncAsDllEntry,
4118 ICreateTypeInfo2_fnSetFuncDocString,
4119 ICreateTypeInfo2_fnSetVarDocString,
4120 ICreateTypeInfo2_fnSetFuncHelpContext,
4121 ICreateTypeInfo2_fnSetVarHelpContext,
4122 ICreateTypeInfo2_fnSetMops,
4123 ICreateTypeInfo2_fnSetTypeIdldesc,
4124 ICreateTypeInfo2_fnLayOut,
4126 ICreateTypeInfo2_fnDeleteFuncDesc,
4127 ICreateTypeInfo2_fnDeleteFuncDescByMemId,
4128 ICreateTypeInfo2_fnDeleteVarDesc,
4129 ICreateTypeInfo2_fnDeleteVarDescByMemId,
4130 ICreateTypeInfo2_fnDeleteImplType,
4131 ICreateTypeInfo2_fnSetCustData,
4132 ICreateTypeInfo2_fnSetFuncCustData,
4133 ICreateTypeInfo2_fnSetParamCustData,
4134 ICreateTypeInfo2_fnSetVarCustData,
4135 ICreateTypeInfo2_fnSetImplTypeCustData,
4136 ICreateTypeInfo2_fnSetHelpStringContext,
4137 ICreateTypeInfo2_fnSetFuncHelpStringContext,
4138 ICreateTypeInfo2_fnSetVarHelpStringContext,
4139 ICreateTypeInfo2_fnInvalidate,
4140 ICreateTypeInfo2_fnSetName
4143 static const ITypeInfo2Vtbl typeinfo2vt =
4146 ITypeInfo2_fnQueryInterface,
4147 ITypeInfo2_fnAddRef,
4148 ITypeInfo2_fnRelease,
4150 ITypeInfo2_fnGetTypeAttr,
4151 ITypeInfo2_fnGetTypeComp,
4152 ITypeInfo2_fnGetFuncDesc,
4153 ITypeInfo2_fnGetVarDesc,
4154 ITypeInfo2_fnGetNames,
4155 ITypeInfo2_fnGetRefTypeOfImplType,
4156 ITypeInfo2_fnGetImplTypeFlags,
4157 ITypeInfo2_fnGetIDsOfNames,
4158 ITypeInfo2_fnInvoke,
4159 ITypeInfo2_fnGetDocumentation,
4160 ITypeInfo2_fnGetDllEntry,
4161 ITypeInfo2_fnGetRefTypeInfo,
4162 ITypeInfo2_fnAddressOfMember,
4163 ITypeInfo2_fnCreateInstance,
4164 ITypeInfo2_fnGetMops,
4165 ITypeInfo2_fnGetContainingTypeLib,
4166 ITypeInfo2_fnReleaseTypeAttr,
4167 ITypeInfo2_fnReleaseFuncDesc,
4168 ITypeInfo2_fnReleaseVarDesc,
4170 ITypeInfo2_fnGetTypeKind,
4171 ITypeInfo2_fnGetTypeFlags,
4172 ITypeInfo2_fnGetFuncIndexOfMemId,
4173 ITypeInfo2_fnGetVarIndexOfMemId,
4174 ITypeInfo2_fnGetCustData,
4175 ITypeInfo2_fnGetFuncCustData,
4176 ITypeInfo2_fnGetParamCustData,
4177 ITypeInfo2_fnGetVarCustData,
4178 ITypeInfo2_fnGetImplTypeCustData,
4179 ITypeInfo2_fnGetDocumentation2,
4180 ITypeInfo2_fnGetAllCustData,
4181 ITypeInfo2_fnGetAllFuncCustData,
4182 ITypeInfo2_fnGetAllParamCustData,
4183 ITypeInfo2_fnGetAllVarCustData,
4184 ITypeInfo2_fnGetAllImplTypeCustData,
4187 static ICreateTypeInfo2 *ICreateTypeInfo2_Constructor(ICreateTypeLib2Impl *typelib, WCHAR *szName, TYPEKIND tkind)
4189 ICreateTypeInfo2Impl *pCreateTypeInfo2Impl;
4191 int nameoffset;
4192 int typeinfo_offset;
4193 MSFT_TypeInfoBase *typeinfo;
4195 TRACE("Constructing ICreateTypeInfo2 for %s with tkind %d\n", debugstr_w(szName), tkind);
4197 pCreateTypeInfo2Impl = heap_alloc_zero(sizeof(ICreateTypeInfo2Impl));
4198 if (!pCreateTypeInfo2Impl) return NULL;
4200 pCreateTypeInfo2Impl->ICreateTypeInfo2_iface.lpVtbl = &ctypeinfo2vt;
4201 pCreateTypeInfo2Impl->ITypeInfo2_iface.lpVtbl = &typeinfo2vt;
4202 pCreateTypeInfo2Impl->ref = 1;
4204 pCreateTypeInfo2Impl->typelib = typelib;
4205 ICreateTypeLib_AddRef((ICreateTypeLib*)typelib);
4207 nameoffset = ctl2_alloc_name(typelib, szName);
4208 typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
4209 typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
4211 typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
4212 *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
4214 pCreateTypeInfo2Impl->typeinfo = typeinfo;
4216 pCreateTypeInfo2Impl->typekind = tkind;
4217 typeinfo->typekind |= tkind | 0x20;
4218 ICreateTypeInfo2_SetAlignment(&pCreateTypeInfo2Impl->ICreateTypeInfo2_iface, 4);
4220 switch (tkind) {
4221 case TKIND_ENUM:
4222 case TKIND_INTERFACE:
4223 case TKIND_DISPATCH:
4224 case TKIND_COCLASS:
4225 typeinfo->size = 4;
4226 break;
4228 case TKIND_RECORD:
4229 case TKIND_UNION:
4230 typeinfo->size = 0;
4231 break;
4233 case TKIND_MODULE:
4234 typeinfo->size = 2;
4235 break;
4237 case TKIND_ALIAS:
4238 typeinfo->size = -0x75;
4239 break;
4241 default:
4242 FIXME("(%s,%d), unrecognized typekind %d\n", debugstr_w(szName), tkind, tkind);
4243 typeinfo->size = 0xdeadbeef;
4244 break;
4247 if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = pCreateTypeInfo2Impl;
4248 typelib->last_typeinfo = pCreateTypeInfo2Impl;
4249 if (!typelib->typeinfos) typelib->typeinfos = pCreateTypeInfo2Impl;
4251 TRACE(" -- %p\n", pCreateTypeInfo2Impl);
4253 return &pCreateTypeInfo2Impl->ICreateTypeInfo2_iface;
4257 /*================== ICreateTypeLib2 Implementation ===================================*/
4259 /******************************************************************************
4260 * ICreateTypeLib2_QueryInterface {OLEAUT32}
4262 static HRESULT WINAPI ICreateTypeLib2_fnQueryInterface(
4263 ICreateTypeLib2 * iface,
4264 REFIID riid,
4265 VOID **ppvObject)
4267 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4269 TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
4271 *ppvObject=NULL;
4272 if(IsEqualIID(riid, &IID_IUnknown) ||
4273 IsEqualIID(riid,&IID_ICreateTypeLib)||
4274 IsEqualIID(riid,&IID_ICreateTypeLib2))
4276 *ppvObject = This;
4277 } else if (IsEqualIID(riid, &IID_ITypeLib) ||
4278 IsEqualIID(riid, &IID_ITypeLib2)) {
4279 *ppvObject = &This->lpVtblTypeLib2;
4282 if(*ppvObject)
4284 ICreateTypeLib2_AddRef(iface);
4285 TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
4286 return S_OK;
4288 TRACE("-- Interface: E_NOINTERFACE\n");
4289 return E_NOINTERFACE;
4292 /******************************************************************************
4293 * ICreateTypeLib2_AddRef {OLEAUT32}
4295 static ULONG WINAPI ICreateTypeLib2_fnAddRef(ICreateTypeLib2 *iface)
4297 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4298 ULONG ref = InterlockedIncrement(&This->ref);
4300 TRACE("(%p)->(%u)\n", This, ref);
4302 return ref;
4305 /******************************************************************************
4306 * ICreateTypeLib2_Release {OLEAUT32}
4308 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface)
4310 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4311 ULONG ref = InterlockedDecrement(&This->ref);
4313 TRACE("(%p)->(%u)\n", This, ref);
4315 if (!ref) {
4316 int i;
4318 for (i = 0; i < MSFT_SEG_MAX; i++) {
4319 heap_free(This->typelib_segment_data[i]);
4320 This->typelib_segment_data[i] = NULL;
4323 heap_free(This->filename);
4324 This->filename = NULL;
4326 while (This->typeinfos) {
4327 ICreateTypeInfo2Impl *typeinfo = This->typeinfos;
4328 This->typeinfos = typeinfo->next_typeinfo;
4329 if(typeinfo->typedata) {
4330 CyclicList *iter, *rem;
4332 rem = typeinfo->typedata->next;
4333 typeinfo->typedata->next = NULL;
4334 iter = rem->next;
4335 heap_free(rem);
4337 while(iter) {
4338 rem = iter;
4339 iter = iter->next;
4340 heap_free(rem->u.data);
4341 heap_free(rem);
4345 heap_free(typeinfo->dual);
4346 heap_free(typeinfo);
4349 heap_free(This);
4352 return ref;
4356 /******************************************************************************
4357 * ICreateTypeLib2_CreateTypeInfo {OLEAUT32}
4359 static HRESULT WINAPI ICreateTypeLib2_fnCreateTypeInfo(
4360 ICreateTypeLib2 * iface,
4361 LPOLESTR szName,
4362 TYPEKIND tkind,
4363 ICreateTypeInfo **tinfo)
4365 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4366 char *name;
4368 TRACE("(%p,%s,%d,%p)\n", iface, debugstr_w(szName), tkind, tinfo);
4370 if (!szName || !tinfo) return E_INVALIDARG;
4372 ctl2_encode_name(This, szName, &name);
4373 if(ctl2_find_name(This, name) != -1)
4374 return TYPE_E_NAMECONFLICT;
4376 *tinfo = (ICreateTypeInfo *)ICreateTypeInfo2_Constructor(This, szName, tkind);
4377 if (!*tinfo) return E_OUTOFMEMORY;
4379 return S_OK;
4382 /******************************************************************************
4383 * ICreateTypeLib2_SetName {OLEAUT32}
4385 static HRESULT WINAPI ICreateTypeLib2_fnSetName(
4386 ICreateTypeLib2 * iface,
4387 LPOLESTR szName)
4389 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4391 int offset;
4393 TRACE("(%p,%s)\n", iface, debugstr_w(szName));
4395 offset = ctl2_alloc_name(This, szName);
4396 if (offset == -1) return E_OUTOFMEMORY;
4397 This->typelib_header.NameOffset = offset;
4398 return S_OK;
4401 /******************************************************************************
4402 * ICreateTypeLib2_SetVersion {OLEAUT32}
4404 static HRESULT WINAPI ICreateTypeLib2_fnSetVersion(ICreateTypeLib2 * iface, WORD wMajorVerNum, WORD wMinorVerNum)
4406 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4408 TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
4410 This->typelib_header.version = wMajorVerNum | (wMinorVerNum << 16);
4411 return S_OK;
4414 /******************************************************************************
4415 * ICreateTypeLib2_SetGuid {OLEAUT32}
4417 static HRESULT WINAPI ICreateTypeLib2_fnSetGuid(ICreateTypeLib2 * iface, REFGUID guid)
4419 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4421 MSFT_GuidEntry guidentry;
4422 int offset;
4424 TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
4426 guidentry.guid = *guid;
4427 guidentry.hreftype = -2;
4428 guidentry.next_hash = -1;
4430 offset = ctl2_alloc_guid(This, &guidentry);
4432 if (offset == -1) return E_OUTOFMEMORY;
4434 This->typelib_header.posguid = offset;
4436 return S_OK;
4439 /******************************************************************************
4440 * ICreateTypeLib2_SetDocString {OLEAUT32}
4442 static HRESULT WINAPI ICreateTypeLib2_fnSetDocString(ICreateTypeLib2 * iface, LPOLESTR szDoc)
4444 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4446 int offset;
4448 TRACE("(%p,%s)\n", iface, debugstr_w(szDoc));
4449 if (!szDoc)
4450 return E_INVALIDARG;
4452 offset = ctl2_alloc_string(This, szDoc);
4453 if (offset == -1) return E_OUTOFMEMORY;
4454 This->typelib_header.helpstring = offset;
4455 return S_OK;
4458 /******************************************************************************
4459 * ICreateTypeLib2_SetHelpFileName {OLEAUT32}
4461 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpFileName(ICreateTypeLib2 * iface, LPOLESTR szHelpFileName)
4463 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4465 int offset;
4467 TRACE("(%p,%s)\n", iface, debugstr_w(szHelpFileName));
4469 offset = ctl2_alloc_string(This, szHelpFileName);
4470 if (offset == -1) return E_OUTOFMEMORY;
4471 This->typelib_header.helpfile = offset;
4472 This->typelib_header.varflags |= 0x10;
4473 return S_OK;
4476 /******************************************************************************
4477 * ICreateTypeLib2_SetHelpContext {OLEAUT32}
4479 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpContext(ICreateTypeLib2 * iface, DWORD dwHelpContext)
4481 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4483 TRACE("(%p,%d)\n", iface, dwHelpContext);
4484 This->typelib_header.helpcontext = dwHelpContext;
4485 return S_OK;
4488 /******************************************************************************
4489 * ICreateTypeLib2_SetLcid {OLEAUT32}
4491 * Sets both the lcid and lcid2 members in the header to lcid.
4493 * As a special case if lcid == LOCALE_NEUTRAL (0), then the first header lcid
4494 * is set to US English while the second one is set to 0.
4496 static HRESULT WINAPI ICreateTypeLib2_fnSetLcid(ICreateTypeLib2 * iface, LCID lcid)
4498 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4500 TRACE("(%p,%d)\n", iface, lcid);
4502 This->typelib_header.lcid = This->typelib_header.lcid2 = lcid;
4504 if(lcid == LOCALE_NEUTRAL) This->typelib_header.lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
4506 return S_OK;
4509 /******************************************************************************
4510 * ICreateTypeLib2_SetLibFlags {OLEAUT32}
4512 static HRESULT WINAPI ICreateTypeLib2_fnSetLibFlags(ICreateTypeLib2 * iface, UINT uLibFlags)
4514 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4516 TRACE("(%p,0x%x)\n", iface, uLibFlags);
4518 This->typelib_header.flags = uLibFlags;
4520 return S_OK;
4523 static int ctl2_write_chunk(HANDLE hFile, const void *segment, int length)
4525 DWORD dwWritten;
4526 if (!WriteFile(hFile, segment, length, &dwWritten, 0)) {
4527 CloseHandle(hFile);
4528 return 0;
4530 return -1;
4533 static int ctl2_write_segment(ICreateTypeLib2Impl *This, HANDLE hFile, int segment)
4535 DWORD dwWritten;
4536 if (!WriteFile(hFile, This->typelib_segment_data[segment],
4537 This->typelib_segdir[segment].length, &dwWritten, 0)) {
4538 CloseHandle(hFile);
4539 return 0;
4542 return -1;
4545 static HRESULT ctl2_finalize_typeinfos(ICreateTypeLib2Impl *This, int filesize)
4547 ICreateTypeInfo2Impl *typeinfo;
4548 HRESULT hres;
4550 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4551 typeinfo->typeinfo->memoffset = filesize;
4553 hres = ICreateTypeInfo2_fnLayOut(&typeinfo->ICreateTypeInfo2_iface);
4554 if(FAILED(hres))
4555 return hres;
4557 if (typeinfo->typedata)
4558 filesize += typeinfo->typedata->next->u.val
4559 + cti2_get_var_count(typeinfo->typeinfo) * 12
4560 + cti2_get_func_count(typeinfo->typeinfo) * 12 + 4;
4563 return S_OK;
4566 static int ctl2_finalize_segment(ICreateTypeLib2Impl *This, int filepos, int segment)
4568 if (This->typelib_segdir[segment].length) {
4569 This->typelib_segdir[segment].offset = filepos;
4570 } else {
4571 This->typelib_segdir[segment].offset = -1;
4574 return This->typelib_segdir[segment].length;
4577 static void ctl2_write_typeinfos(ICreateTypeLib2Impl *This, HANDLE hFile)
4579 ICreateTypeInfo2Impl *typeinfo;
4581 for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4582 CyclicList *iter;
4583 int offset = 0;
4585 if (!typeinfo->typedata) continue;
4587 iter = typeinfo->typedata->next;
4588 ctl2_write_chunk(hFile, &iter->u.val, sizeof(int));
4589 for(iter=iter->next; iter!=typeinfo->typedata->next; iter=iter->next)
4590 ctl2_write_chunk(hFile, iter->u.data, ctl2_get_record_size(iter));
4592 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4593 ctl2_write_chunk(hFile, &iter->indice, sizeof(int));
4595 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4596 ctl2_write_chunk(hFile, &iter->name, sizeof(int));
4598 for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next) {
4599 ctl2_write_chunk(hFile, &offset, sizeof(int));
4600 offset += ctl2_get_record_size(iter);
4605 /******************************************************************************
4606 * ICreateTypeLib2_SaveAllChanges {OLEAUT32}
4608 static HRESULT WINAPI ICreateTypeLib2_fnSaveAllChanges(ICreateTypeLib2 * iface)
4610 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4612 int retval;
4613 int filepos;
4614 HANDLE hFile;
4615 HRESULT hres;
4617 TRACE("(%p)\n", iface);
4619 retval = TYPE_E_IOERROR;
4621 hFile = CreateFileW(This->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4622 if (hFile == INVALID_HANDLE_VALUE) return retval;
4624 filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
4625 filepos += This->typelib_header.nrtypeinfos * 4;
4627 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEINFO);
4628 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUIDHASH);
4629 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUID);
4630 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_REFERENCES);
4631 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTINFO);
4632 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTFILES);
4633 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAMEHASH);
4634 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAME);
4635 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_STRING);
4636 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEDESC);
4637 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_ARRAYDESC);
4638 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATA);
4639 filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATAGUID);
4641 hres = ctl2_finalize_typeinfos(This, filepos);
4642 if(FAILED(hres)) {
4643 CloseHandle(hFile);
4644 return hres;
4647 if (!ctl2_write_chunk(hFile, &This->typelib_header, sizeof(This->typelib_header))) return retval;
4648 if (This->typelib_header.varflags & HELPDLLFLAG)
4649 if (!ctl2_write_chunk(hFile, &This->helpStringDll, sizeof(This->helpStringDll))) return retval;
4650 if (!ctl2_write_chunk(hFile, This->typelib_typeinfo_offsets, This->typelib_header.nrtypeinfos * 4)) return retval;
4651 if (!ctl2_write_chunk(hFile, This->typelib_segdir, sizeof(This->typelib_segdir))) return retval;
4652 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEINFO )) return retval;
4653 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUIDHASH )) return retval;
4654 if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUID )) return retval;
4655 if (!ctl2_write_segment(This, hFile, MSFT_SEG_REFERENCES )) return retval;
4656 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTINFO )) return retval;
4657 if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTFILES )) return retval;
4658 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAMEHASH )) return retval;
4659 if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAME )) return retval;
4660 if (!ctl2_write_segment(This, hFile, MSFT_SEG_STRING )) return retval;
4661 if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEDESC )) return retval;
4662 if (!ctl2_write_segment(This, hFile, MSFT_SEG_ARRAYDESC )) return retval;
4663 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATA )) return retval;
4664 if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATAGUID)) return retval;
4666 ctl2_write_typeinfos(This, hFile);
4668 if (!CloseHandle(hFile)) return retval;
4670 return S_OK;
4674 /******************************************************************************
4675 * ICreateTypeLib2_DeleteTypeInfo {OLEAUT32}
4677 * Deletes a named TypeInfo from a type library.
4679 * RETURNS
4681 * Success: S_OK
4682 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4684 static HRESULT WINAPI ICreateTypeLib2_fnDeleteTypeInfo(
4685 ICreateTypeLib2 * iface, /* [I] The type library to delete from. */
4686 LPOLESTR szName) /* [I] The name of the typeinfo to delete. */
4688 FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
4689 return E_OUTOFMEMORY;
4692 /******************************************************************************
4693 * ICreateTypeLib2_SetCustData {OLEAUT32}
4695 * Sets custom data for a type library.
4697 * RETURNS
4699 * Success: S_OK
4700 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4702 static HRESULT WINAPI ICreateTypeLib2_fnSetCustData(
4703 ICreateTypeLib2 * iface, /* [I] The type library to store the custom data in. */
4704 REFGUID guid, /* [I] The GUID used as a key to retrieve the custom data. */
4705 VARIANT *pVarVal) /* [I] The custom data itself. */
4707 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4709 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
4711 return ctl2_set_custdata(This, guid, pVarVal, &This->typelib_header.CustomDataOffset);
4714 /******************************************************************************
4715 * ICreateTypeLib2_SetHelpStringContext {OLEAUT32}
4717 * Sets a context number for the library help string.
4719 * PARAMS
4720 * iface [I] The type library to set the help string context for.
4721 * dwContext [I] The help string context.
4723 * RETURNS
4724 * Success: S_OK
4725 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4727 static
4728 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringContext(ICreateTypeLib2 * iface,
4729 ULONG dwContext)
4731 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4733 TRACE("(%p,%d)\n", iface, dwContext);
4735 This->typelib_header.helpstringcontext = dwContext;
4736 return S_OK;
4739 /******************************************************************************
4740 * ICreateTypeLib2_SetHelpStringDll {OLEAUT32}
4742 * Set the DLL used to look up localized help strings.
4744 * PARAMS
4745 * iface [I] The type library to set the help DLL for.
4746 * szDllName [I] The name of the help DLL.
4748 * RETURNS
4749 * Success: S_OK
4750 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
4752 static
4753 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringDll(ICreateTypeLib2 * iface,
4754 LPOLESTR szDllName)
4756 ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4757 int offset;
4759 TRACE("(%p,%s)\n", iface, debugstr_w(szDllName));
4760 if (!szDllName)
4761 return E_INVALIDARG;
4763 offset = ctl2_alloc_string(This, szDllName);
4764 if (offset == -1)
4765 return E_OUTOFMEMORY;
4766 This->typelib_header.varflags |= HELPDLLFLAG;
4767 This->helpStringDll = offset;
4768 return S_OK;
4771 /*================== ITypeLib2 Implementation ===================================*/
4773 /******************************************************************************
4774 * ITypeLib2_QueryInterface {OLEAUT32}
4776 static HRESULT WINAPI ITypeLib2_fnQueryInterface(ITypeLib2 * iface, REFIID riid, LPVOID * ppv)
4778 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4780 return ICreateTypeLib2_QueryInterface((ICreateTypeLib2 *)This, riid, ppv);
4783 /******************************************************************************
4784 * ITypeLib2_AddRef {OLEAUT32}
4786 static ULONG WINAPI ITypeLib2_fnAddRef(ITypeLib2 * iface)
4788 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4790 return ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This);
4793 /******************************************************************************
4794 * ITypeLib2_Release {OLEAUT32}
4796 static ULONG WINAPI ITypeLib2_fnRelease(ITypeLib2 * iface)
4798 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4800 return ICreateTypeLib2_Release((ICreateTypeLib2 *)This);
4803 /******************************************************************************
4804 * ITypeLib2_GetTypeInfoCount {OLEAUT32}
4806 static UINT WINAPI ITypeLib2_fnGetTypeInfoCount(
4807 ITypeLib2 * iface)
4809 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4811 TRACE("(%p)\n", iface);
4813 return This->typelib_header.nrtypeinfos;
4816 /******************************************************************************
4817 * ITypeLib2_GetTypeInfo {OLEAUT32}
4819 static HRESULT WINAPI ITypeLib2_fnGetTypeInfo(
4820 ITypeLib2 * iface,
4821 UINT index,
4822 ITypeInfo** ppTInfo)
4824 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4826 TRACE("(%p,%d,%p)\n", iface, index, ppTInfo);
4828 if (!ppTInfo) return E_INVALIDARG;
4830 if (index >= This->typelib_header.nrtypeinfos) {
4831 return TYPE_E_ELEMENTNOTFOUND;
4834 return ctl2_find_typeinfo_from_offset(This, This->typelib_typeinfo_offsets[index], ppTInfo);
4837 /******************************************************************************
4838 * ITypeLib2_GetTypeInfoType {OLEAUT32}
4840 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoType(
4841 ITypeLib2 * iface,
4842 UINT index,
4843 TYPEKIND* kind)
4845 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4847 TRACE("(%p,%d,%p)\n", iface, index, kind);
4849 if (!kind) return E_INVALIDARG;
4851 if (index >= This->typelib_header.nrtypeinfos) {
4852 return TYPE_E_ELEMENTNOTFOUND;
4855 *kind = (This->typelib_segment_data[MSFT_SEG_TYPEINFO][This->typelib_typeinfo_offsets[index]]) & 0xF;
4857 return S_OK;
4860 /******************************************************************************
4861 * ITypeLib2_GetTypeInfoOfGuid {OLEAUT32}
4863 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoOfGuid(
4864 ITypeLib2 * iface,
4865 REFGUID guid,
4866 ITypeInfo** ppTinfo)
4868 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4870 int guidoffset;
4871 int typeinfo;
4873 TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), ppTinfo);
4875 guidoffset = ctl2_find_guid(This, ctl2_hash_guid(guid), guid);
4876 if (guidoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
4878 typeinfo = ((MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][guidoffset])->hreftype;
4879 if (typeinfo < 0) return TYPE_E_ELEMENTNOTFOUND;
4881 return ctl2_find_typeinfo_from_offset(This, typeinfo, ppTinfo);
4884 /******************************************************************************
4885 * ITypeLib2_GetLibAttr {OLEAUT32}
4887 static HRESULT WINAPI ITypeLib2_fnGetLibAttr(
4888 ITypeLib2 * iface,
4889 TLIBATTR** ppTLibAttr)
4891 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4893 TRACE("(%p,%p)\n", This, ppTLibAttr);
4895 if(!ppTLibAttr)
4896 return E_INVALIDARG;
4898 *ppTLibAttr = heap_alloc_zero(sizeof(TLIBATTR));
4899 if(!*ppTLibAttr)
4900 return E_OUTOFMEMORY;
4902 if(This->typelib_header.posguid != -1) {
4903 MSFT_GuidEntry *guid;
4905 guid = (MSFT_GuidEntry*)&This->typelib_segment_data[MSFT_SEG_GUID][This->typelib_header.posguid];
4906 (*ppTLibAttr)->guid = guid->guid;
4909 (*ppTLibAttr)->lcid = This->typelib_header.lcid;
4910 (*ppTLibAttr)->syskind = ctl2_get_syskind(This);
4911 (*ppTLibAttr)->wMajorVerNum = LOWORD(This->typelib_header.version);
4912 (*ppTLibAttr)->wMinorVerNum = HIWORD(This->typelib_header.version);
4913 (*ppTLibAttr)->wLibFlags = This->typelib_header.flags;
4914 return S_OK;
4917 /******************************************************************************
4918 * ITypeLib2_GetTypeComp {OLEAUT32}
4920 static HRESULT WINAPI ITypeLib2_fnGetTypeComp(
4921 ITypeLib2 * iface,
4922 ITypeComp** ppTComp)
4924 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4926 FIXME("(%p,%p), stub!\n", This, ppTComp);
4928 return E_OUTOFMEMORY;
4931 /******************************************************************************
4932 * ITypeLib2_GetDocumentation {OLEAUT32}
4934 static HRESULT WINAPI ITypeLib2_fnGetDocumentation(
4935 ITypeLib2 * iface,
4936 INT index,
4937 BSTR* pBstrName,
4938 BSTR* pBstrDocString,
4939 DWORD* pdwHelpContext,
4940 BSTR* pBstrHelpFile)
4942 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4943 WCHAR *string;
4945 TRACE("(%p,%d,%p,%p,%p,%p)\n", This, index, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4947 if(index != -1) {
4948 ICreateTypeInfo2Impl *iter;
4950 for(iter=This->typeinfos; iter!=NULL && index!=0; iter=iter->next_typeinfo)
4951 index--;
4953 if(!iter)
4954 return TYPE_E_ELEMENTNOTFOUND;
4956 return ITypeInfo_GetDocumentation(&iter->ITypeInfo2_iface,
4957 -1, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4960 if(pBstrName) {
4961 if(This->typelib_header.NameOffset == -1)
4962 *pBstrName = NULL;
4963 else {
4964 MSFT_NameIntro *name = (MSFT_NameIntro*)&This->
4965 typelib_segment_data[MSFT_SEG_NAME][This->typelib_header.NameOffset];
4967 ctl2_decode_name((char*)&name->namelen, &string);
4969 *pBstrName = SysAllocString(string);
4970 if(!*pBstrName)
4971 return E_OUTOFMEMORY;
4975 if(pBstrDocString) {
4976 if(This->typelib_header.helpstring == -1)
4977 *pBstrDocString = NULL;
4978 else {
4979 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpstring], &string);
4981 *pBstrDocString = SysAllocString(string);
4982 if(!*pBstrDocString) {
4983 if(pBstrName) SysFreeString(*pBstrName);
4984 return E_OUTOFMEMORY;
4989 if(pdwHelpContext)
4990 *pdwHelpContext = This->typelib_header.helpcontext;
4992 if(pBstrHelpFile) {
4993 if(This->typelib_header.helpfile == -1)
4994 *pBstrHelpFile = NULL;
4995 else {
4996 ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpfile], &string);
4998 *pBstrHelpFile = SysAllocString(string);
4999 if(!*pBstrHelpFile) {
5000 if(pBstrName) SysFreeString(*pBstrName);
5001 if(pBstrDocString) SysFreeString(*pBstrDocString);
5002 return E_OUTOFMEMORY;
5007 return S_OK;
5010 /******************************************************************************
5011 * ITypeLib2_IsName {OLEAUT32}
5013 static HRESULT WINAPI ITypeLib2_fnIsName(
5014 ITypeLib2 * iface,
5015 LPOLESTR szNameBuf,
5016 ULONG lHashVal,
5017 BOOL* pfName)
5019 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5021 char *encoded_name;
5022 int nameoffset;
5023 MSFT_NameIntro *nameintro;
5025 TRACE("(%p,%s,%x,%p)\n", iface, debugstr_w(szNameBuf), lHashVal, pfName);
5027 ctl2_encode_name(This, szNameBuf, &encoded_name);
5028 nameoffset = ctl2_find_name(This, encoded_name);
5030 *pfName = 0;
5032 if (nameoffset == -1) return S_OK;
5034 nameintro = (MSFT_NameIntro *)(&This->typelib_segment_data[MSFT_SEG_NAME][nameoffset]);
5035 if (nameintro->hreftype == -1) return S_OK;
5037 *pfName = 1;
5039 FIXME("Should be decoding our copy of the name over szNameBuf.\n");
5041 return S_OK;
5044 /******************************************************************************
5045 * ITypeLib2_FindName {OLEAUT32}
5047 static HRESULT WINAPI ITypeLib2_fnFindName(
5048 ITypeLib2 * iface,
5049 LPOLESTR szNameBuf,
5050 ULONG lHashVal,
5051 ITypeInfo** ppTInfo,
5052 MEMBERID* rgMemId,
5053 USHORT* pcFound)
5055 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5057 FIXME("(%p,%s,%x,%p,%p,%p), stub!\n", This, debugstr_w(szNameBuf), lHashVal, ppTInfo, rgMemId, pcFound);
5059 return E_OUTOFMEMORY;
5062 /******************************************************************************
5063 * ITypeLib2_ReleaseTLibAttr {OLEAUT32}
5065 static void WINAPI ITypeLib2_fnReleaseTLibAttr(
5066 ITypeLib2 * iface,
5067 TLIBATTR* attr)
5069 TRACE("(%p,%p)\n", iface, attr);
5070 heap_free(attr);
5073 /******************************************************************************
5074 * ICreateTypeLib2_GetCustData {OLEAUT32}
5076 * Retrieves a custom data value stored on a type library.
5078 * RETURNS
5080 * Success: S_OK
5081 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5083 static HRESULT WINAPI ITypeLib2_fnGetCustData(
5084 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5085 REFGUID guid, /* [I] The GUID under which the custom data is stored. */
5086 VARIANT* pVarVal) /* [O] The custom data. */
5088 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5090 FIXME("(%p,%s,%p), stub!\n", This, debugstr_guid(guid), pVarVal);
5092 return E_OUTOFMEMORY;
5095 /******************************************************************************
5096 * ICreateTypeLib2_GetLibStatistics {OLEAUT32}
5098 * Retrieves some statistics about names in a type library, supposedly for
5099 * hash table optimization purposes.
5101 * RETURNS
5103 * Success: S_OK
5104 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5106 static HRESULT WINAPI ITypeLib2_fnGetLibStatistics(
5107 ITypeLib2 * iface, /* [I] The type library to get statistics about. */
5108 ULONG* pcUniqueNames, /* [O] The number of unique names in the type library. */
5109 ULONG* pcchUniqueNames) /* [O] The number of changed (?) characters in names in the type library. */
5111 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5113 FIXME("(%p,%p,%p), stub!\n", This, pcUniqueNames, pcchUniqueNames);
5115 return E_OUTOFMEMORY;
5118 /******************************************************************************
5119 * ICreateTypeLib2_GetDocumentation2 {OLEAUT32}
5121 * Obtain locale-aware help string information.
5123 * RETURNS
5125 * Success: S_OK
5126 * Failure: STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
5128 static HRESULT WINAPI ITypeLib2_fnGetDocumentation2(
5129 ITypeLib2 * iface,
5130 INT index,
5131 LCID lcid,
5132 BSTR* pbstrHelpString,
5133 DWORD* pdwHelpStringContext,
5134 BSTR* pbstrHelpStringDll)
5136 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5138 FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", This, index, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
5140 return E_OUTOFMEMORY;
5143 /******************************************************************************
5144 * ICreateTypeLib2_GetAllCustData {OLEAUT32}
5146 * Retrieve all of the custom data for a type library.
5148 * RETURNS
5150 * Success: S_OK
5151 * Failure: E_OUTOFMEMORY or E_INVALIDARG.
5153 static HRESULT WINAPI ITypeLib2_fnGetAllCustData(
5154 ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
5155 CUSTDATA* pCustData) /* [O] The structure in which to place the custom data. */
5157 ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
5159 FIXME("(%p,%p), stub!\n", This, pCustData);
5161 return E_OUTOFMEMORY;
5165 /*================== ICreateTypeLib2 & ITypeLib2 VTABLEs And Creation ===================================*/
5167 static const ICreateTypeLib2Vtbl ctypelib2vt =
5170 ICreateTypeLib2_fnQueryInterface,
5171 ICreateTypeLib2_fnAddRef,
5172 ICreateTypeLib2_fnRelease,
5174 ICreateTypeLib2_fnCreateTypeInfo,
5175 ICreateTypeLib2_fnSetName,
5176 ICreateTypeLib2_fnSetVersion,
5177 ICreateTypeLib2_fnSetGuid,
5178 ICreateTypeLib2_fnSetDocString,
5179 ICreateTypeLib2_fnSetHelpFileName,
5180 ICreateTypeLib2_fnSetHelpContext,
5181 ICreateTypeLib2_fnSetLcid,
5182 ICreateTypeLib2_fnSetLibFlags,
5183 ICreateTypeLib2_fnSaveAllChanges,
5185 ICreateTypeLib2_fnDeleteTypeInfo,
5186 ICreateTypeLib2_fnSetCustData,
5187 ICreateTypeLib2_fnSetHelpStringContext,
5188 ICreateTypeLib2_fnSetHelpStringDll
5191 static const ITypeLib2Vtbl typelib2vt =
5194 ITypeLib2_fnQueryInterface,
5195 ITypeLib2_fnAddRef,
5196 ITypeLib2_fnRelease,
5198 ITypeLib2_fnGetTypeInfoCount,
5199 ITypeLib2_fnGetTypeInfo,
5200 ITypeLib2_fnGetTypeInfoType,
5201 ITypeLib2_fnGetTypeInfoOfGuid,
5202 ITypeLib2_fnGetLibAttr,
5203 ITypeLib2_fnGetTypeComp,
5204 ITypeLib2_fnGetDocumentation,
5205 ITypeLib2_fnIsName,
5206 ITypeLib2_fnFindName,
5207 ITypeLib2_fnReleaseTLibAttr,
5209 ITypeLib2_fnGetCustData,
5210 ITypeLib2_fnGetLibStatistics,
5211 ITypeLib2_fnGetDocumentation2,
5212 ITypeLib2_fnGetAllCustData,
5215 static ICreateTypeLib2 *ICreateTypeLib2_Constructor(SYSKIND syskind, LPCOLESTR szFile)
5217 ICreateTypeLib2Impl *pCreateTypeLib2Impl;
5218 int failed = 0;
5220 TRACE("Constructing ICreateTypeLib2 (%d, %s)\n", syskind, debugstr_w(szFile));
5222 pCreateTypeLib2Impl = heap_alloc_zero(sizeof(ICreateTypeLib2Impl));
5223 if (!pCreateTypeLib2Impl) return NULL;
5225 pCreateTypeLib2Impl->filename = heap_alloc((strlenW(szFile) + 1) * sizeof(WCHAR));
5226 if (!pCreateTypeLib2Impl->filename) {
5227 heap_free(pCreateTypeLib2Impl);
5228 return NULL;
5230 strcpyW(pCreateTypeLib2Impl->filename, szFile);
5232 ctl2_init_header(pCreateTypeLib2Impl);
5233 ctl2_init_segdir(pCreateTypeLib2Impl);
5235 pCreateTypeLib2Impl->typelib_header.varflags |= syskind;
5238 * The following two calls return an offset or -1 if out of memory. We
5239 * specifically need an offset of 0, however, so...
5241 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
5242 if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
5244 pCreateTypeLib2Impl->typelib_guidhash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_GUIDHASH];
5245 pCreateTypeLib2Impl->typelib_namehash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_NAMEHASH];
5247 memset(pCreateTypeLib2Impl->typelib_guidhash_segment, 0xff, 0x80);
5248 memset(pCreateTypeLib2Impl->typelib_namehash_segment, 0xff, 0x200);
5250 pCreateTypeLib2Impl->lpVtbl = &ctypelib2vt;
5251 pCreateTypeLib2Impl->lpVtblTypeLib2 = &typelib2vt;
5252 pCreateTypeLib2Impl->ref = 1;
5254 if (failed) {
5255 ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)pCreateTypeLib2Impl);
5256 return 0;
5259 return (ICreateTypeLib2 *)pCreateTypeLib2Impl;
5262 /******************************************************************************
5263 * CreateTypeLib2 [OLEAUT32.180]
5265 * Obtains an ICreateTypeLib2 object for creating a new-style (MSFT) type
5266 * library.
5268 * NOTES
5270 * See also CreateTypeLib.
5272 * RETURNS
5273 * Success: S_OK
5274 * Failure: Status
5276 HRESULT WINAPI CreateTypeLib2(
5277 SYSKIND syskind, /* [I] System type library is for */
5278 LPCOLESTR szFile, /* [I] Type library file name */
5279 ICreateTypeLib2** ppctlib) /* [O] Storage for object returned */
5281 TRACE("(%d,%s,%p)\n", syskind, debugstr_w(szFile), ppctlib);
5283 if (!szFile) return E_INVALIDARG;
5284 *ppctlib = ICreateTypeLib2_Constructor(syskind, szFile);
5285 return (*ppctlib)? S_OK: E_OUTOFMEMORY;
5288 /******************************************************************************
5289 * ClearCustData (OLEAUT32.171)
5291 * Clear a custom data types' data.
5293 * PARAMS
5294 * lpCust [I] The custom data type instance
5296 * RETURNS
5297 * Nothing.
5299 void WINAPI ClearCustData(LPCUSTDATA lpCust)
5301 if (lpCust && lpCust->cCustData)
5303 if (lpCust->prgCustData)
5305 DWORD i;
5307 for (i = 0; i < lpCust->cCustData; i++)
5308 VariantClear(&lpCust->prgCustData[i].varValue);
5310 /* FIXME - Should be using a per-thread IMalloc */
5311 heap_free(lpCust->prgCustData);
5312 lpCust->prgCustData = NULL;
5314 lpCust->cCustData = 0;