d3d10: Add argument check to ID3D10Effect::GetTechniqueByName().
[wine] / dlls / oleaut32 / typelib2.c
1 /*
2  *      TYPELIB2
3  *
4  *      Copyright 2004  Alastair Bridgewater
5  *
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.
10  *
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.
15  *
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
19  *
20  * --------------------------------------------------------------------------------------
21  *  Known problems:
22  *
23  *    Badly incomplete.
24  *
25  *    Only works on little-endian systems.
26  *
27  */
28
29 #include "config.h"
30 #include "wine/port.h"
31
32 #include <stdlib.h>
33 #include <string.h>
34 #include <stdarg.h>
35 #include <stdio.h>
36 #include <ctype.h>
37
38 #define COBJMACROS
39 #define NONAMELESSUNION
40 #define NONAMELESSSTRUCT
41
42 #include "winerror.h"
43 #include "windef.h"
44 #include "winbase.h"
45 #include "winnls.h"
46 #include "winreg.h"
47 #include "winuser.h"
48
49 #include "wine/unicode.h"
50 #include "objbase.h"
51 #include "typelib.h"
52 #include "wine/debug.h"
53
54 WINE_DEFAULT_DEBUG_CHANNEL(typelib2);
55 /* WINE_DEFAULT_DEBUG_CHANNEL(ole); */
56
57
58 /******************************************************************************
59  * ICreateTypeLib2 {OLEAUT32}
60  *
61  * NOTES
62  *  The ICreateTypeLib2 interface provides an interface whereby one may create
63  *  new type library (.tlb) files.
64  *
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).
69  *
70  * METHODS
71  */
72
73 /******************************************************************************
74  * ICreateTypeInfo2 {OLEAUT32}
75  *
76  * NOTES
77  *  The ICreateTypeInfo2 interface provides an interface whereby one may add
78  *  type information to type library (.tlb) files.
79  *
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).
84  *
85  * METHODS
86  */
87
88 /******************************************************************************
89  * ITypeLib2 {OLEAUT32}
90  *
91  * NOTES
92  *  The ITypeLib2 interface provides an interface whereby one may query MSFT
93  *  format type library (.tlb) files.
94  *
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).
99  *
100  * METHODS
101  */
102
103 /******************************************************************************
104  * ITypeInfo2 {OLEAUT32}
105  *
106  * NOTES
107  *  The ITypeInfo2 interface provides an interface whereby one may query type
108  *  information stored in MSFT format type library (.tlb) files.
109  *
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).
114  *
115  * METHODS
116  */
117
118 /*================== Implementation Structures ===================================*/
119
120 /* Used for storing cyclic list. Tail address is kept */
121 typedef struct tagCyclicList {
122     struct tagCyclicList *next;
123     int indice;
124     int name;
125
126     union {
127         int val;
128         int *data;
129     }u;
130 } CyclicList;
131
132 enum MSFT_segment_index {
133     MSFT_SEG_TYPEINFO = 0,  /* type information */
134     MSFT_SEG_IMPORTINFO,    /* import information */
135     MSFT_SEG_IMPORTFILES,   /* import filenames */
136     MSFT_SEG_REFERENCES,    /* references (?) */
137     MSFT_SEG_GUIDHASH,      /* hash table for guids? */
138     MSFT_SEG_GUID,          /* guid storage */
139     MSFT_SEG_NAMEHASH,      /* hash table for names */
140     MSFT_SEG_NAME,          /* name storage */
141     MSFT_SEG_STRING,        /* string storage */
142     MSFT_SEG_TYPEDESC,      /* type descriptions */
143     MSFT_SEG_ARRAYDESC,     /* array descriptions */
144     MSFT_SEG_CUSTDATA,      /* custom data */
145     MSFT_SEG_CUSTDATAGUID,  /* custom data guids */
146     MSFT_SEG_UNKNOWN,       /* ??? */
147     MSFT_SEG_UNKNOWN2,      /* ??? */
148     MSFT_SEG_MAX            /* total number of segments */
149 };
150
151 typedef struct tagMSFT_ImpFile {
152     int guid;
153     LCID lcid;
154     int version;
155     char filename[0]; /* preceded by two bytes of encoded (length << 2) + flags in the low two bits. */
156 } MSFT_ImpFile;
157
158 typedef struct tagICreateTypeLib2Impl
159 {
160     const ICreateTypeLib2Vtbl *lpVtbl;
161     const ITypeLib2Vtbl       *lpVtblTypeLib2;
162
163     LONG ref;
164
165     WCHAR *filename;
166
167     MSFT_Header typelib_header;
168     INT helpStringDll;
169     MSFT_pSeg typelib_segdir[MSFT_SEG_MAX];
170     char *typelib_segment_data[MSFT_SEG_MAX];
171     int typelib_segment_block_length[MSFT_SEG_MAX];
172
173     int typelib_guids; /* Number of defined typelib guids */
174     int typeinfo_guids; /* Number of defined typeinfo guids */
175
176     INT typelib_typeinfo_offsets[0x200]; /* Hope that's enough. */
177
178     INT *typelib_namehash_segment;
179     INT *typelib_guidhash_segment;
180
181     struct tagICreateTypeInfo2Impl *typeinfos;
182     struct tagICreateTypeInfo2Impl *last_typeinfo;
183 } ICreateTypeLib2Impl;
184
185 static inline ICreateTypeLib2Impl *impl_from_ITypeLib2( ITypeLib2 *iface )
186 {
187     return (ICreateTypeLib2Impl *)((char*)iface - FIELD_OFFSET(ICreateTypeLib2Impl, lpVtblTypeLib2));
188 }
189
190 typedef struct tagICreateTypeInfo2Impl
191 {
192     const ICreateTypeInfo2Vtbl *lpVtbl;
193     const ITypeInfo2Vtbl       *lpVtblTypeInfo2;
194
195     LONG ref;
196
197     ICreateTypeLib2Impl *typelib;
198     MSFT_TypeInfoBase *typeinfo;
199
200     struct tagCyclicList *typedata; /* tail of cyclic list */
201
202     TYPEKIND typekind;
203     int datawidth;
204
205     struct tagICreateTypeInfo2Impl *next_typeinfo;
206     struct tagICreateTypeInfo2Impl *dual;
207 } ICreateTypeInfo2Impl;
208
209 static inline ICreateTypeInfo2Impl *impl_from_ITypeInfo2( ITypeInfo2 *iface )
210 {
211     return (ICreateTypeInfo2Impl *)((char*)iface - FIELD_OFFSET(ICreateTypeInfo2Impl, lpVtblTypeInfo2));
212 }
213
214 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface);
215
216
217 /*================== Internal functions ===================================*/
218
219 /****************************************************************************
220  *      ctl2_init_header
221  *
222  *  Initializes the type library header of a new typelib.
223  */
224 static void ctl2_init_header(
225         ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
226 {
227     This->typelib_header.magic1 = 0x5446534d;
228     This->typelib_header.magic2 = 0x00010002;
229     This->typelib_header.posguid = -1;
230     This->typelib_header.lcid = This->typelib_header.lcid2 = GetUserDefaultLCID();
231     This->typelib_header.varflags = 0x40;
232     This->typelib_header.version = 0;
233     This->typelib_header.flags = 0;
234     This->typelib_header.nrtypeinfos = 0;
235     This->typelib_header.helpstring = -1;
236     This->typelib_header.helpstringcontext = 0;
237     This->typelib_header.helpcontext = 0;
238     This->typelib_header.nametablecount = 0;
239     This->typelib_header.nametablechars = 0;
240     This->typelib_header.NameOffset = -1;
241     This->typelib_header.helpfile = -1;
242     This->typelib_header.CustomDataOffset = -1;
243     This->typelib_header.res44 = 0x20;
244     This->typelib_header.res48 = 0x80;
245     This->typelib_header.dispatchpos = -1;
246     This->typelib_header.nimpinfos = 0;
247     This->helpStringDll = -1;
248 }
249
250 /****************************************************************************
251  *      ctl2_init_segdir
252  *
253  *  Initializes the segment directory of a new typelib.
254  */
255 static void ctl2_init_segdir(
256         ICreateTypeLib2Impl *This) /* [I] The typelib to initialize. */
257 {
258     int i;
259     MSFT_pSeg *segdir;
260
261     segdir = &This->typelib_segdir[MSFT_SEG_TYPEINFO];
262
263     for (i = 0; i < 15; i++) {
264         segdir[i].offset = -1;
265         segdir[i].length = 0;
266         segdir[i].res08 = -1;
267         segdir[i].res0c = 0x0f;
268     }
269 }
270
271 /****************************************************************************
272  *      ctl2_hash_guid
273  *
274  *  Generates a hash key from a GUID.
275  *
276  * RETURNS
277  *
278  *  The hash key for the GUID.
279  */
280 static int ctl2_hash_guid(
281         REFGUID guid)                /* [I] The guid to find. */
282 {
283     int hash;
284     int i;
285
286     hash = 0;
287     for (i = 0; i < 8; i ++) {
288         hash ^= ((const short *)guid)[i];
289     }
290
291     return hash & 0x1f;
292 }
293
294 /****************************************************************************
295  *      ctl2_find_guid
296  *
297  *  Locates a guid in a type library.
298  *
299  * RETURNS
300  *
301  *  The offset into the GUID segment of the guid, or -1 if not found.
302  */
303 static int ctl2_find_guid(
304         ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
305         int hash_key,              /* [I] The hash key for the guid. */
306         REFGUID guid)                /* [I] The guid to find. */
307 {
308     int offset;
309     MSFT_GuidEntry *guidentry;
310
311     offset = This->typelib_guidhash_segment[hash_key];
312     while (offset != -1) {
313         guidentry = (MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][offset];
314
315         if (!memcmp(guidentry, guid, sizeof(GUID))) return offset;
316
317         offset = guidentry->next_hash;
318     }
319
320     return offset;
321 }
322
323 /****************************************************************************
324  *      ctl2_find_name
325  *
326  *  Locates a name in a type library.
327  *
328  * RETURNS
329  *
330  *  The offset into the NAME segment of the name, or -1 if not found.
331  *
332  * NOTES
333  *
334  *  The name must be encoded as with ctl2_encode_name().
335  */
336 static int ctl2_find_name(
337         ICreateTypeLib2Impl *This, /* [I] The typelib to operate against. */
338         const char *name)          /* [I] The encoded name to find. */
339 {
340     int offset;
341     int *namestruct;
342
343     offset = This->typelib_namehash_segment[name[2] & 0x7f];
344     while (offset != -1) {
345         namestruct = (int *)&This->typelib_segment_data[MSFT_SEG_NAME][offset];
346
347         if (!((namestruct[2] ^ *((const int *)name)) & 0xffff00ff)) {
348             /* hash codes and lengths match, final test */
349             if (!strncasecmp(name+4, (void *)(namestruct+3), name[0])) break;
350         }
351
352         /* move to next item in hash bucket */
353         offset = namestruct[1];
354     }
355
356     return offset;
357 }
358
359 /****************************************************************************
360  *      ctl2_encode_name
361  *
362  *  Encodes a name string to a form suitable for storing into a type library
363  *  or comparing to a name stored in a type library.
364  *
365  * RETURNS
366  *
367  *  The length of the encoded name, including padding and length+hash fields.
368  *
369  * NOTES
370  *
371  *  Will throw an exception if name or result are NULL. Is not multithread
372  *  safe in the slightest.
373  */
374 static int ctl2_encode_name(
375         ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (used for LCID only). */
376         const WCHAR *name,         /* [I] The name string to encode. */
377         char **result)             /* [O] A pointer to a pointer to receive the encoded name. */
378 {
379     int length;
380     static char converted_name[0x104];
381     int offset;
382     int value;
383
384     length = WideCharToMultiByte(CP_ACP, 0, name, strlenW(name), converted_name+4, 0x100, NULL, NULL);
385     converted_name[0] = length & 0xff;
386
387     converted_name[length + 4] = 0;
388
389     converted_name[1] = 0x00;
390
391     value = LHashValOfNameSysA(This->typelib_header.varflags & 0x0f, This->typelib_header.lcid, converted_name + 4);
392
393     converted_name[2] = value;
394     converted_name[3] = value >> 8;
395
396     for (offset = (4 - length) & 3; offset; offset--) converted_name[length + offset + 3] = 0x57;
397
398     *result = converted_name;
399
400     return (length + 7) & ~3;
401 }
402
403 /****************************************************************************
404  *      ctl2_decode_name
405  *
406  * Converts string stored in typelib data to unicode.
407  */
408 static void ctl2_decode_name(
409         char *data,         /* [I] String to be decoded */
410         WCHAR **string)     /* [O] Decoded string */
411 {
412     int i, length;
413     static WCHAR converted_string[0x104];
414
415     length = data[0];
416
417     for(i=0; i<length; i++)
418         converted_string[i] = data[i+4];
419     converted_string[length] = '\0';
420
421     *string = converted_string;
422 }
423
424 /****************************************************************************
425  *      ctl2_encode_string
426  *
427  *  Encodes a string to a form suitable for storing into a type library or
428  *  comparing to a string stored in a type library.
429  *
430  * RETURNS
431  *
432  *  The length of the encoded string, including padding and length fields.
433  *
434  * NOTES
435  *
436  *  Will throw an exception if string or result are NULL. Is not multithread
437  *  safe in the slightest.
438  */
439 static int ctl2_encode_string(
440         ICreateTypeLib2Impl *This, /* [I] The typelib to operate against (not used?). */
441         const WCHAR *string,       /* [I] The string to encode. */
442         char **result)             /* [O] A pointer to a pointer to receive the encoded string. */
443 {
444     int length;
445     static char converted_string[0x104];
446     int offset;
447
448     length = WideCharToMultiByte(CP_ACP, 0, string, strlenW(string), converted_string+2, 0x102, NULL, NULL);
449     converted_string[0] = length & 0xff;
450     converted_string[1] = (length >> 8) & 0xff;
451
452     for (offset = (4 - (length + 2)) & 3; offset; offset--) converted_string[length + offset + 1] = 0x57;
453
454     *result = converted_string;
455
456     return (length + 5) & ~3;
457 }
458
459 /****************************************************************************
460  *      ctl2_decode_string
461  *
462  * Converts string stored in typelib data to unicode.
463  */
464 static void ctl2_decode_string(
465         char *data,         /* [I] String to be decoded */
466         WCHAR **string)     /* [O] Decoded string */
467 {
468     int i, length;
469     static WCHAR converted_string[0x104];
470
471     length = data[0] + (data[1]<<8);
472     if((length&0x3) == 1)
473         length >>= 2;
474
475     for(i=0; i<length; i++)
476         converted_string[i] = data[i+2];
477     converted_string[length] = '\0';
478
479     *string = converted_string;
480 }
481
482 /****************************************************************************
483  *      ctl2_alloc_segment
484  *
485  *  Allocates memory from a segment in a type library.
486  *
487  * RETURNS
488  *
489  *  Success: The offset within the segment of the new data area.
490  *  Failure: -1 (this is invariably an out of memory condition).
491  *
492  * BUGS
493  *
494  *  Does not (yet) handle the case where the allocated segment memory needs to grow.
495  */
496 static int ctl2_alloc_segment(
497         ICreateTypeLib2Impl *This,       /* [I] The type library in which to allocate. */
498         enum MSFT_segment_index segment, /* [I] The segment in which to allocate. */
499         int size,                        /* [I] The amount to allocate. */
500         int block_size)                  /* [I] Initial allocation block size, or 0 for default. */
501 {
502     int offset;
503
504     if(!This->typelib_segment_data[segment]) {
505         if (!block_size) block_size = 0x2000;
506
507         This->typelib_segment_block_length[segment] = block_size;
508         This->typelib_segment_data[segment] = HeapAlloc(GetProcessHeap(), 0, block_size);
509         if (!This->typelib_segment_data[segment]) return -1;
510         memset(This->typelib_segment_data[segment], 0x57, block_size);
511     }
512
513     while ((This->typelib_segdir[segment].length + size) > This->typelib_segment_block_length[segment]) {
514         char *block;
515
516         block_size = This->typelib_segment_block_length[segment];
517         block = HeapReAlloc(GetProcessHeap(), 0, This->typelib_segment_data[segment], block_size << 1);
518         if (!block) return -1;
519
520         if (segment == MSFT_SEG_TYPEINFO) {
521             /* TypeInfos have a direct pointer to their memory space, so we have to fix them up. */
522             ICreateTypeInfo2Impl *typeinfo;
523
524             for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
525                 typeinfo->typeinfo = (void *)&block[((char *)typeinfo->typeinfo) - This->typelib_segment_data[segment]];
526             }
527         }
528
529         memset(block + block_size, 0x57, block_size);
530         This->typelib_segment_block_length[segment] = block_size << 1;
531         This->typelib_segment_data[segment] = block;
532     }
533
534     offset = This->typelib_segdir[segment].length;
535     This->typelib_segdir[segment].length += size;
536
537     return offset;
538 }
539
540 /****************************************************************************
541  *      ctl2_alloc_typeinfo
542  *
543  *  Allocates and initializes a typeinfo structure in a type library.
544  *
545  * RETURNS
546  *
547  *  Success: The offset of the new typeinfo.
548  *  Failure: -1 (this is invariably an out of memory condition).
549  */
550 static int ctl2_alloc_typeinfo(
551         ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
552         int nameoffset)            /* [I] The offset of the name for this typeinfo. */
553 {
554     int offset;
555     MSFT_TypeInfoBase *typeinfo;
556
557     offset = ctl2_alloc_segment(This, MSFT_SEG_TYPEINFO, sizeof(MSFT_TypeInfoBase), 0);
558     if (offset == -1) return -1;
559
560     This->typelib_typeinfo_offsets[This->typelib_header.nrtypeinfos++] = offset;
561
562     typeinfo = (void *)(This->typelib_segment_data[MSFT_SEG_TYPEINFO] + offset);
563
564     typeinfo->typekind = (This->typelib_header.nrtypeinfos - 1) << 16;
565     typeinfo->memoffset = -1; /* should be EOF if no elements */
566     typeinfo->res2 = 0;
567     typeinfo->res3 = 0;
568     typeinfo->res4 = 3;
569     typeinfo->res5 = 0;
570     typeinfo->cElement = 0;
571     typeinfo->res7 = 0;
572     typeinfo->res8 = 0;
573     typeinfo->res9 = 0;
574     typeinfo->resA = 0;
575     typeinfo->posguid = -1;
576     typeinfo->flags = 0;
577     typeinfo->NameOffset = nameoffset;
578     typeinfo->version = 0;
579     typeinfo->docstringoffs = -1;
580     typeinfo->helpstringcontext = 0;
581     typeinfo->helpcontext = 0;
582     typeinfo->oCustData = -1;
583     typeinfo->cbSizeVft = 0;
584     typeinfo->cImplTypes = 0;
585     typeinfo->size = 0;
586     typeinfo->datatype1 = -1;
587     typeinfo->datatype2 = 0;
588     typeinfo->res18 = 0;
589     typeinfo->res19 = -1;
590
591     return offset;
592 }
593
594 /****************************************************************************
595  *      ctl2_alloc_guid
596  *
597  *  Allocates and initializes a GUID structure in a type library. Also updates
598  *  the GUID hash table as needed.
599  *
600  * RETURNS
601  *
602  *  Success: The offset of the new GUID.
603  *  Failure: -1 (this is invariably an out of memory condition).
604  */
605 static int ctl2_alloc_guid(
606         ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
607         MSFT_GuidEntry *guid)      /* [I] The GUID to store. */
608 {
609     int offset;
610     MSFT_GuidEntry *guid_space;
611     int hash_key;
612
613     hash_key = ctl2_hash_guid(&guid->guid);
614
615     offset = ctl2_find_guid(This, hash_key, &guid->guid);
616     if (offset != -1) return offset;
617
618     offset = ctl2_alloc_segment(This, MSFT_SEG_GUID, sizeof(MSFT_GuidEntry), 0);
619     if (offset == -1) return -1;
620
621     guid_space = (void *)(This->typelib_segment_data[MSFT_SEG_GUID] + offset);
622     *guid_space = *guid;
623
624     guid_space->next_hash = This->typelib_guidhash_segment[hash_key];
625     This->typelib_guidhash_segment[hash_key] = offset;
626
627     return offset;
628 }
629
630 /****************************************************************************
631  *      ctl2_alloc_name
632  *
633  *  Allocates and initializes a name within a type library. Also updates the
634  *  name hash table as needed.
635  *
636  * RETURNS
637  *
638  *  Success: The offset within the segment of the new name.
639  *  Failure: -1 (this is invariably an out of memory condition).
640  */
641 static int ctl2_alloc_name(
642         ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
643         const WCHAR *name)         /* [I] The name to store. */
644 {
645     int length;
646     int offset;
647     MSFT_NameIntro *name_space;
648     char *encoded_name;
649
650     length = ctl2_encode_name(This, name, &encoded_name);
651
652     offset = ctl2_find_name(This, encoded_name);
653     if (offset != -1) return offset;
654
655     offset = ctl2_alloc_segment(This, MSFT_SEG_NAME, length + 8, 0);
656     if (offset == -1) return -1;
657
658     name_space = (void *)(This->typelib_segment_data[MSFT_SEG_NAME] + offset);
659     name_space->hreftype = -1;
660     name_space->next_hash = -1;
661     memcpy(&name_space->namelen, encoded_name, length);
662
663     if (This->typelib_namehash_segment[encoded_name[2] & 0x7f] != -1)
664         name_space->next_hash = This->typelib_namehash_segment[encoded_name[2] & 0x7f];
665
666     This->typelib_namehash_segment[encoded_name[2] & 0x7f] = offset;
667
668     This->typelib_header.nametablecount += 1;
669     This->typelib_header.nametablechars += *encoded_name;
670
671     return offset;
672 }
673
674 /****************************************************************************
675  *      ctl2_alloc_string
676  *
677  *  Allocates and initializes a string in a type library.
678  *
679  * RETURNS
680  *
681  *  Success: The offset within the segment of the new string.
682  *  Failure: -1 (this is invariably an out of memory condition).
683  */
684 static int ctl2_alloc_string(
685         ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
686         const WCHAR *string)       /* [I] The string to store. */
687 {
688     int length;
689     int offset;
690     char *string_space;
691     char *encoded_string;
692
693     length = ctl2_encode_string(This, string, &encoded_string);
694
695     for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_STRING].length;
696          offset += ((((This->typelib_segment_data[MSFT_SEG_STRING][offset + 1] << 8) & 0xff)
697              | (This->typelib_segment_data[MSFT_SEG_STRING][offset + 0] & 0xff)) + 5) & ~3) {
698         if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_STRING] + offset, length)) return offset;
699     }
700
701     offset = ctl2_alloc_segment(This, MSFT_SEG_STRING, length, 0);
702     if (offset == -1) return -1;
703
704     string_space = This->typelib_segment_data[MSFT_SEG_STRING] + offset;
705     memcpy(string_space, encoded_string, length);
706
707     return offset;
708 }
709
710 /****************************************************************************
711  *      ctl2_alloc_importinfo
712  *
713  *  Allocates and initializes an import information structure in a type library.
714  *
715  * RETURNS
716  *
717  *  Success: The offset of the new importinfo.
718  *  Failure: -1 (this is invariably an out of memory condition).
719  */
720 static int ctl2_alloc_importinfo(
721         ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
722         MSFT_ImpInfo *impinfo)     /* [I] The import information to store. */
723 {
724     int offset;
725     MSFT_ImpInfo *impinfo_space;
726
727     impinfo_space = (MSFT_ImpInfo*)&This->typelib_segment_data[MSFT_SEG_IMPORTINFO][0];
728     for (offset=0; offset<This->typelib_segdir[MSFT_SEG_IMPORTINFO].length;
729             offset+=sizeof(MSFT_ImpInfo)) {
730         if(impinfo_space->oImpFile == impinfo->oImpFile
731                 && impinfo_space->oGuid == impinfo->oGuid)
732             return offset;
733
734         impinfo_space += 1;
735     }
736
737     impinfo->flags |= This->typelib_header.nimpinfos++;
738
739     offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTINFO, sizeof(MSFT_ImpInfo), 0);
740     if (offset == -1) return -1;
741
742     impinfo_space = (void *)(This->typelib_segment_data[MSFT_SEG_IMPORTINFO] + offset);
743     *impinfo_space = *impinfo;
744
745     return offset;
746 }
747
748 /****************************************************************************
749  *      ctl2_alloc_importfile
750  *
751  *  Allocates and initializes an import file definition in a type library.
752  *
753  * RETURNS
754  *
755  *  Success: The offset of the new importinfo.
756  *  Failure: -1 (this is invariably an out of memory condition).
757  */
758 static int ctl2_alloc_importfile(
759         ICreateTypeLib2Impl *This, /* [I] The type library to allocate in. */
760         int guidoffset,            /* [I] The offset to the GUID for the imported library. */
761         LCID lcid,                 /* [I] The LCID of imported library. */
762         int major_version,         /* [I] The major version number of the imported library. */
763         int minor_version,         /* [I] The minor version number of the imported library. */
764         const WCHAR *filename)     /* [I] The filename of the imported library. */
765 {
766     int length;
767     int offset;
768     MSFT_ImpFile *importfile;
769     char *encoded_string;
770
771     length = ctl2_encode_string(This, filename, &encoded_string);
772
773     encoded_string[0] <<= 2;
774     encoded_string[0] |= 1;
775
776     for (offset = 0; offset < This->typelib_segdir[MSFT_SEG_IMPORTFILES].length;
777          offset += ((((This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xd] << 8) & 0xff)
778              | (This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset + 0xc] & 0xff)) >> 2) + 0xc) {
779         if (!memcmp(encoded_string, This->typelib_segment_data[MSFT_SEG_IMPORTFILES] + offset + 0xc, length)) return offset;
780     }
781
782     offset = ctl2_alloc_segment(This, MSFT_SEG_IMPORTFILES, length + 0xc, 0);
783     if (offset == -1) return -1;
784
785     importfile = (MSFT_ImpFile *)&This->typelib_segment_data[MSFT_SEG_IMPORTFILES][offset];
786     importfile->guid = guidoffset;
787     importfile->lcid = lcid;
788     importfile->version = major_version | (minor_version << 16);
789     memcpy(importfile->filename, encoded_string, length);
790
791     return offset;
792 }
793
794 /****************************************************************************
795  *      ctl2_alloc_custdata
796  *
797  *  Allocates and initializes a "custom data" value in a type library.
798  *
799  * RETURNS
800  *
801  *  Success: The offset of the new custdata.
802  *  Failure:
803  *
804  *    -1: Out of memory.
805  *    -2: Unable to encode VARIANT data (typically a bug).
806  */
807 static int ctl2_alloc_custdata(
808         ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the value. */
809         VARIANT *pVarVal)          /* [I] The value to encode. */
810 {
811     int offset;
812
813     TRACE("(%p,%p(%d))\n",This,pVarVal,V_VT(pVarVal));
814
815     switch (V_VT(pVarVal)) {
816     case VT_UI4:
817     case VT_I4:
818     case VT_R4:
819     case VT_INT:
820     case VT_UINT:
821     case VT_HRESULT:
822         offset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
823         if (offset == -1) return offset;
824
825         *((unsigned short *)&This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = V_VT(pVarVal);
826         *((DWORD *)&This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2]) = V_UI4(pVarVal);
827         break;
828
829     case VT_BSTR: {
830             /* Construct the data */
831             UINT cp = CP_ACP;
832             int stringlen = SysStringLen(V_BSTR(pVarVal));
833             int len = 0;
834             if (stringlen > 0) {
835                 GetLocaleInfoA(This->typelib_header.lcid, LOCALE_IDEFAULTANSICODEPAGE | LOCALE_RETURN_NUMBER,
836                         (LPSTR)&cp, sizeof(cp));
837                 len = WideCharToMultiByte(cp, 0, V_BSTR(pVarVal), SysStringLen(V_BSTR(pVarVal)), NULL, 0, NULL, NULL);
838                 if (!len)
839                     return -1;
840             }
841
842             offset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, (6 + len + 3) & ~0x3, 0);
843             if (offset == -1) return offset;
844
845             *((unsigned short *)&This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset]) = V_VT(pVarVal);
846             *((DWORD *)&This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+2]) = (DWORD)len;
847             if (stringlen > 0) {
848                 WideCharToMultiByte(cp, 0, V_BSTR(pVarVal), SysStringLen(V_BSTR(pVarVal)),
849                         &This->typelib_segment_data[MSFT_SEG_CUSTDATA][offset+6], len, NULL, NULL);
850             }
851         }
852         break;
853
854     default:
855         FIXME("Unknown variable encoding vt %d.\n", V_VT(pVarVal));
856         return -2;
857     }
858
859     return offset;
860 }
861
862 /****************************************************************************
863  *      ctl2_set_custdata
864  *
865  *  Adds a custom data element to an object in a type library.
866  *
867  * RETURNS
868  *
869  *  Success: S_OK.
870  *  Failure: One of E_INVALIDARG or E_OUTOFMEMORY.
871  */
872 static HRESULT ctl2_set_custdata(
873         ICreateTypeLib2Impl *This, /* [I] The type library to store the custom data in. */
874         REFGUID guid,              /* [I] The GUID used as a key to retrieve the custom data. */
875         VARIANT *pVarVal,          /* [I] The custom data itself. */
876         int *offset)               /* [I/O] The list of custom data to prepend to. */
877 {
878     MSFT_GuidEntry guidentry;
879     int dataoffset;
880     int guidoffset;
881     int custoffset;
882     int *custdata;
883
884     guidentry.guid = *guid;
885
886     guidentry.hreftype = -1;
887     guidentry.next_hash = -1;
888
889     guidoffset = ctl2_alloc_guid(This, &guidentry);
890     if (guidoffset == -1) return E_OUTOFMEMORY;
891     dataoffset = ctl2_alloc_custdata(This, pVarVal);
892     if (dataoffset == -1) return E_OUTOFMEMORY;
893     if (dataoffset == -2) return DISP_E_BADVARTYPE;
894
895     custoffset = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATAGUID, 12, 0);
896     if (custoffset == -1) return E_OUTOFMEMORY;
897
898     custdata = (int *)&This->typelib_segment_data[MSFT_SEG_CUSTDATAGUID][custoffset];
899     custdata[0] = guidoffset;
900     custdata[1] = dataoffset;
901     custdata[2] = *offset;
902     *offset = custoffset;
903
904     return S_OK;
905 }
906
907 /****************************************************************************
908  *      ctl2_encode_typedesc
909  *
910  *  Encodes a type description, storing information in the TYPEDESC and ARRAYDESC
911  *  segments as needed.
912  *
913  * RETURNS
914  *
915  *  Success: 0.
916  *  Failure: -1.
917  */
918 static int ctl2_encode_typedesc(
919         ICreateTypeLib2Impl *This, /* [I] The type library in which to encode the TYPEDESC. */
920         const TYPEDESC *tdesc,     /* [I] The type description to encode. */
921         int *encoded_tdesc,        /* [O] The encoded type description. */
922         int *width,                /* [O] The width of the type, or NULL. */
923         int *alignment,            /* [O] The alignment of the type, or NULL. */
924         int *decoded_size)         /* [O] The total size of the unencoded TYPEDESCs, including nested descs. */
925 {
926     int default_tdesc;
927     int scratch;
928     int typeoffset;
929     int arrayoffset;
930     int *typedata;
931     int *arraydata;
932     int target_type;
933     int child_size;
934
935     default_tdesc = 0x80000000 | (tdesc->vt << 16) | tdesc->vt;
936     if (!width) width = &scratch;
937     if (!alignment) alignment = &scratch;
938     if (!decoded_size) decoded_size = &scratch;
939
940     *decoded_size = 0;
941
942     switch (tdesc->vt) {
943     case VT_UI1:
944     case VT_I1:
945         *encoded_tdesc = default_tdesc;
946         *width = 1;
947         *alignment = 1;
948         break;
949
950     case VT_INT:
951         *encoded_tdesc = 0x80000000 | (VT_I4 << 16) | VT_INT;
952         if ((This->typelib_header.varflags & 0x0f) == SYS_WIN16) {
953             *width = 2;
954             *alignment = 2;
955         } else {
956             *width = 4;
957             *alignment = 4;
958         }
959         break;
960
961     case VT_UINT:
962         *encoded_tdesc = 0x80000000 | (VT_UI4 << 16) | VT_UINT;
963         if ((This->typelib_header.varflags & 0x0f) == SYS_WIN16) {
964             *width = 2;
965             *alignment = 2;
966         } else {
967             *width = 4;
968             *alignment = 4;
969         }
970         break;
971
972     case VT_UI2:
973     case VT_I2:
974     case VT_BOOL:
975         *encoded_tdesc = default_tdesc;
976         *width = 2;
977         *alignment = 2;
978         break;
979
980     case VT_I4:
981     case VT_UI4:
982     case VT_R4:
983     case VT_ERROR:
984     case VT_BSTR:
985     case VT_HRESULT:
986         *encoded_tdesc = default_tdesc;
987         *width = 4;
988         *alignment = 4;
989         break;
990
991     case VT_CY:
992         *encoded_tdesc = default_tdesc;
993         *width = 8;
994         *alignment = 4; /* guess? */
995         break;
996
997     case VT_VOID:
998         *encoded_tdesc = 0x80000000 | (VT_EMPTY << 16) | tdesc->vt;
999         *width = 0;
1000         *alignment = 1;
1001         break;
1002
1003     case VT_PTR:
1004         /* FIXME: Make with the error checking. */
1005         FIXME("PTR vartype, may not work correctly.\n");
1006
1007         ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1008
1009         for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1010             typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1011             if (((typedata[0] & 0xffff) == VT_PTR) && (typedata[1] == target_type)) break;
1012         }
1013
1014         if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1015             int mix_field;
1016             
1017             if (target_type & 0x80000000) {
1018                 mix_field = ((target_type >> 16) & 0x3fff) | VT_BYREF;
1019             } else {
1020                 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1021                 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1022             }
1023
1024             typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1025             typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1026
1027             typedata[0] = (mix_field << 16) | VT_PTR;
1028             typedata[1] = target_type;
1029         }
1030
1031         *encoded_tdesc = typeoffset;
1032
1033         *width = 4;
1034         *alignment = 4;
1035         *decoded_size = sizeof(TYPEDESC) + child_size;
1036         break;
1037
1038     case VT_SAFEARRAY:
1039         /* FIXME: Make with the error checking. */
1040         FIXME("SAFEARRAY vartype, may not work correctly.\n");
1041
1042         ctl2_encode_typedesc(This, tdesc->u.lptdesc, &target_type, NULL, NULL, &child_size);
1043
1044         for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1045             typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1046             if (((typedata[0] & 0xffff) == VT_SAFEARRAY) && (typedata[1] == target_type)) break;
1047         }
1048
1049         if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1050             int mix_field;
1051             
1052             if (target_type & 0x80000000) {
1053                 mix_field = ((target_type >> 16) & VT_TYPEMASK) | VT_ARRAY;
1054             } else {
1055                 typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][target_type];
1056                 mix_field = ((typedata[0] >> 16) == 0x7fff)? 0x7fff: 0x7ffe;
1057             }
1058
1059             typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1060             typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1061
1062             typedata[0] = (mix_field << 16) | VT_SAFEARRAY;
1063             typedata[1] = target_type;
1064         }
1065
1066         *encoded_tdesc = typeoffset;
1067
1068         *width = 4;
1069         *alignment = 4;
1070         *decoded_size = sizeof(TYPEDESC) + child_size;
1071         break;
1072
1073     case VT_CARRAY:
1074       {
1075         /* FIXME: Make with the error checking. */
1076         int num_dims = tdesc->u.lpadesc->cDims, elements = 1, dim;
1077
1078         ctl2_encode_typedesc(This, &tdesc->u.lpadesc->tdescElem, &target_type, width, alignment, NULL);
1079         arrayoffset = ctl2_alloc_segment(This, MSFT_SEG_ARRAYDESC, (2 + 2 * num_dims) * sizeof(int), 0);
1080         arraydata = (void *)&This->typelib_segment_data[MSFT_SEG_ARRAYDESC][arrayoffset];
1081
1082         arraydata[0] = target_type;
1083         arraydata[1] = num_dims;
1084         arraydata[1] |= ((num_dims * 2 * sizeof(int)) << 16);
1085         arraydata += 2;
1086
1087         for(dim = 0; dim < num_dims; dim++) {
1088             arraydata[0] = tdesc->u.lpadesc->rgbounds[dim].cElements;
1089             arraydata[1] = tdesc->u.lpadesc->rgbounds[dim].lLbound;
1090             elements *= tdesc->u.lpadesc->rgbounds[dim].cElements;
1091             arraydata += 2;
1092         }
1093         typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1094         typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1095
1096         typedata[0] = (0x7ffe << 16) | VT_CARRAY;
1097         typedata[1] = arrayoffset;
1098
1099         *encoded_tdesc = typeoffset;
1100         *width = *width * elements;
1101         *decoded_size = sizeof(ARRAYDESC) + (num_dims - 1) * sizeof(SAFEARRAYBOUND);
1102
1103         break;
1104       }
1105     case VT_USERDEFINED:
1106         TRACE("USERDEFINED.\n");
1107         for (typeoffset = 0; typeoffset < This->typelib_segdir[MSFT_SEG_TYPEDESC].length; typeoffset += 8) {
1108             typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1109             if ((typedata[0] == ((0x7fff << 16) | VT_USERDEFINED)) && (typedata[1] == tdesc->u.hreftype)) break;
1110         }
1111
1112         if (typeoffset == This->typelib_segdir[MSFT_SEG_TYPEDESC].length) {
1113             typeoffset = ctl2_alloc_segment(This, MSFT_SEG_TYPEDESC, 8, 0);
1114             typedata = (void *)&This->typelib_segment_data[MSFT_SEG_TYPEDESC][typeoffset];
1115
1116             typedata[0] = (0x7fff << 16) | VT_USERDEFINED;
1117             typedata[1] = tdesc->u.hreftype;
1118         }
1119
1120         *encoded_tdesc = typeoffset;
1121         *width = 0;
1122         *alignment = 1;
1123         break;
1124
1125     default:
1126         FIXME("Unrecognized type %d.\n", tdesc->vt);
1127         *encoded_tdesc = default_tdesc;
1128         *width = 0;
1129         *alignment = 1;
1130         break;
1131     }
1132
1133     return 0;
1134 }
1135
1136 /****************************************************************************
1137  *      ctl2_find_nth_reference
1138  *
1139  *  Finds a reference by index into the linked list of reference records.
1140  *
1141  * RETURNS
1142  *
1143  *  Success: Offset of the desired reference record.
1144  *  Failure: -1.
1145  */
1146 static int ctl2_find_nth_reference(
1147         ICreateTypeLib2Impl *This, /* [I] The type library in which to search. */
1148         int offset,                /* [I] The starting offset of the reference list. */
1149         int index)                 /* [I] The index of the reference to find. */
1150 {
1151     MSFT_RefRecord *ref;
1152
1153     for (; index && (offset != -1); index--) {
1154         ref = (MSFT_RefRecord *)&This->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1155         offset = ref->onext;
1156     }
1157
1158     return offset;
1159 }
1160
1161 /****************************************************************************
1162  *      ctl2_find_typeinfo_from_offset
1163  *
1164  *  Finds an ITypeInfo given an offset into the TYPEINFO segment.
1165  *
1166  * RETURNS
1167  *
1168  *  Success: S_OK.
1169  *  Failure: TYPE_E_ELEMENTNOTFOUND.
1170  */
1171 static HRESULT ctl2_find_typeinfo_from_offset(
1172         ICreateTypeLib2Impl *This, /* [I] The typelib to find the typeinfo in. */
1173         int offset,                /* [I] The offset of the desired typeinfo. */
1174         ITypeInfo **ppTinfo)       /* [I] The typeinfo found. */
1175 {
1176     void *typeinfodata;
1177     ICreateTypeInfo2Impl *typeinfo;
1178
1179     typeinfodata = &This->typelib_segment_data[MSFT_SEG_TYPEINFO][offset];
1180
1181     for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
1182         if (typeinfo->typeinfo == typeinfodata) {
1183             *ppTinfo = (ITypeInfo *)&typeinfo->lpVtblTypeInfo2;
1184             ITypeInfo2_AddRef(*ppTinfo);
1185             return S_OK;
1186         }
1187     }
1188
1189     ERR("Failed to find typeinfo, invariant varied.\n");
1190
1191     return TYPE_E_ELEMENTNOTFOUND;
1192 }
1193
1194 /****************************************************************************
1195  *      ctl2_add_default_value
1196  *
1197  *  Adds default value of an argument
1198  *
1199  * RETURNS
1200  *
1201  *  Success: S_OK
1202  *  Failure: Error code from winerror.h
1203  */
1204 static HRESULT ctl2_add_default_value(
1205         ICreateTypeLib2Impl *This, /* [I] The typelib to allocate data in */
1206         int *encoded_value,        /* [O] The encoded default value or data offset */
1207         VARIANT *value,            /* [I] Default value to be encoded */
1208         VARTYPE arg_type)          /* [I] Argument type */
1209 {
1210     VARIANT v;
1211     HRESULT hres;
1212     int mask = 0;
1213
1214     TRACE("%p %d %d\n", This, V_VT(value), arg_type);
1215
1216     if(arg_type == VT_INT)
1217         arg_type = VT_I4;
1218     if(arg_type == VT_UINT)
1219         arg_type = VT_UI4;
1220
1221     v = *value;
1222     if(V_VT(value) != arg_type) {
1223         hres = VariantChangeType(&v, value, 0, arg_type);
1224         if(FAILED(hres))
1225             return hres;
1226     }
1227
1228     /* Check if default value can be stored in encoded_value */
1229     switch(arg_type) {
1230     case VT_I4:
1231     case VT_UI4:
1232         mask = 0x3ffffff;
1233         if(V_UI4(&v)>0x3ffffff)
1234             break;
1235     case VT_I1:
1236     case VT_UI1:
1237     case VT_BOOL:
1238          if(!mask)
1239              mask = 0xff;
1240     case VT_I2:
1241     case VT_UI2:
1242         if(!mask)
1243             mask = 0xffff;
1244         *encoded_value = (V_UI4(&v)&mask) | ((0x80+0x4*arg_type)<<24);
1245         return S_OK;
1246     }
1247
1248     switch(arg_type) {
1249     case VT_I4:
1250     case VT_R4:
1251     case VT_UI4:
1252     case VT_INT:
1253     case VT_UINT:
1254     case VT_HRESULT:
1255     case VT_PTR: {
1256         /* Construct the data to be allocated */
1257         int data[2];
1258         data[0] = arg_type + (V_UI4(&v)<<16);
1259         data[1] = (V_UI4(&v)>>16) + 0x57570000;
1260
1261         /* Check if the data was already allocated */
1262         /* Currently the structures doesn't allow to do it in a nice way */
1263         for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-8; *encoded_value+=4)
1264             if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8))
1265                 return S_OK;
1266
1267         /* Allocate the data */
1268         *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, 8, 0);
1269         if(*encoded_value == -1)
1270             return E_OUTOFMEMORY;
1271
1272         memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, 8);
1273         return S_OK;
1274     }
1275     case VT_BSTR: {
1276         /* Construct the data */
1277         int i, len = (6+SysStringLen(V_BSTR(&v))+3) & ~0x3;
1278         char *data = HeapAlloc(GetProcessHeap(), 0, len);
1279
1280         if(!data)
1281             return E_OUTOFMEMORY;
1282
1283         *((unsigned short*)data) = arg_type;
1284         *((unsigned*)(data+2)) = SysStringLen(V_BSTR(&v));
1285         for(i=0; i<SysStringLen(V_BSTR(&v)); i++) {
1286             if(V_BSTR(&v)[i] <= 0x7f)
1287                 data[i+6] = V_BSTR(&v)[i];
1288             else
1289                 data[i+6] = '?';
1290         }
1291         WideCharToMultiByte(CP_ACP, 0, V_BSTR(&v), SysStringLen(V_BSTR(&v)), &data[6], len-6, NULL, NULL);
1292         for(i=6+SysStringLen(V_BSTR(&v)); i<len; i++)
1293             data[i] = 0x57;
1294
1295         /* Check if the data was already allocated */
1296         for(*encoded_value=0; *encoded_value<=This->typelib_segdir[MSFT_SEG_CUSTDATA].length-len; *encoded_value+=4)
1297             if(!memcmp(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len)) {
1298                 HeapFree(GetProcessHeap(), 0, data);
1299                 return S_OK;
1300             }
1301
1302         /* Allocate the data */
1303         *encoded_value = ctl2_alloc_segment(This, MSFT_SEG_CUSTDATA, len, 0);
1304         if(*encoded_value == -1) {
1305             HeapFree(GetProcessHeap(), 0, data);
1306             return E_OUTOFMEMORY;
1307         }
1308
1309         memcpy(&This->typelib_segment_data[MSFT_SEG_CUSTDATA][*encoded_value], data, len);
1310         HeapFree(GetProcessHeap(), 0, data);
1311         return S_OK;
1312     }
1313     default:
1314         FIXME("Argument type not yet handled\n");
1315         return E_NOTIMPL;
1316     }
1317 }
1318
1319 /*================== ICreateTypeInfo2 Implementation ===================================*/
1320
1321 /******************************************************************************
1322  * ICreateTypeInfo2_QueryInterface {OLEAUT32}
1323  *
1324  *  See IUnknown_QueryInterface.
1325  */
1326 static HRESULT WINAPI ICreateTypeInfo2_fnQueryInterface(
1327         ICreateTypeInfo2 * iface,
1328         REFIID riid,
1329         VOID **ppvObject)
1330 {
1331     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1332
1333     TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
1334
1335     *ppvObject=NULL;
1336     if(IsEqualIID(riid, &IID_IUnknown) ||
1337        IsEqualIID(riid,&IID_ICreateTypeInfo)||
1338        IsEqualIID(riid,&IID_ICreateTypeInfo2))
1339     {
1340         *ppvObject = This;
1341     } else if (IsEqualIID(riid, &IID_ITypeInfo) ||
1342                IsEqualIID(riid, &IID_ITypeInfo2)) {
1343         *ppvObject = &This->lpVtblTypeInfo2;
1344     }
1345
1346     if(*ppvObject)
1347     {
1348         ICreateTypeInfo2_AddRef(iface);
1349         TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
1350         return S_OK;
1351     }
1352     TRACE("-- Interface: E_NOINTERFACE\n");
1353     return E_NOINTERFACE;
1354 }
1355
1356 /******************************************************************************
1357  * ICreateTypeInfo2_AddRef {OLEAUT32}
1358  *
1359  *  See IUnknown_AddRef.
1360  */
1361 static ULONG WINAPI ICreateTypeInfo2_fnAddRef(ICreateTypeInfo2 *iface)
1362 {
1363     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1364     ULONG ref = InterlockedIncrement(&This->ref);
1365
1366     TRACE("(%p)->ref was %u\n",This, ref - 1);
1367
1368     if(ref==1 && This->typelib)
1369         ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This->typelib);
1370
1371     return ref;
1372 }
1373
1374 /******************************************************************************
1375  * ICreateTypeInfo2_Release {OLEAUT32}
1376  *
1377  *  See IUnknown_Release.
1378  */
1379 static ULONG WINAPI ICreateTypeInfo2_fnRelease(ICreateTypeInfo2 *iface)
1380 {
1381     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1382     ULONG ref = InterlockedDecrement(&This->ref);
1383
1384     TRACE("(%p)->(%u)\n",This, ref);
1385
1386     if (!ref) {
1387         if (This->typelib) {
1388             ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)This->typelib);
1389             /* Keep This->typelib reference to make stored ICreateTypeInfo structure valid */
1390             /* This->typelib = NULL; */
1391         }
1392
1393         /* ICreateTypeLib2 frees all ICreateTypeInfos when it releases. */
1394         /* HeapFree(GetProcessHeap(),0,This); */
1395         return 0;
1396     }
1397
1398     return ref;
1399 }
1400
1401
1402 /******************************************************************************
1403  * ICreateTypeInfo2_SetGuid {OLEAUT32}
1404  *
1405  *  See ICreateTypeInfo_SetGuid.
1406  */
1407 static HRESULT WINAPI ICreateTypeInfo2_fnSetGuid(ICreateTypeInfo2 *iface, REFGUID guid)
1408 {
1409     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1410
1411     MSFT_GuidEntry guidentry;
1412     int offset;
1413
1414     TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
1415
1416     guidentry.guid = *guid;
1417     guidentry.hreftype = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1418     guidentry.next_hash = -1;
1419
1420     offset = ctl2_alloc_guid(This->typelib, &guidentry);
1421     
1422     if (offset == -1) return E_OUTOFMEMORY;
1423
1424     This->typeinfo->posguid = offset;
1425
1426     if (IsEqualIID(guid, &IID_IDispatch)) {
1427         This->typelib->typelib_header.dispatchpos = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
1428     }
1429
1430     return S_OK;
1431 }
1432
1433 /******************************************************************************
1434  * ICreateTypeInfo2_SetTypeFlags {OLEAUT32}
1435  *
1436  *  See ICreateTypeInfo_SetTypeFlags.
1437  */
1438 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeFlags(ICreateTypeInfo2 *iface, UINT uTypeFlags)
1439 {
1440     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1441
1442     TRACE("(%p,0x%x)\n", iface, uTypeFlags);
1443
1444     if(uTypeFlags & TYPEFLAG_FDUAL) {
1445         This->typeinfo->typekind |= 0x10;
1446         This->typeinfo->typekind &= ~0x0f;
1447         This->typeinfo->typekind |= TKIND_DISPATCH;
1448
1449         if(!This->dual) {
1450             This->dual = HeapAlloc(GetProcessHeap(), 0, sizeof(ICreateTypeInfo2Impl));
1451             if(!This->dual)
1452                 return E_OUTOFMEMORY;
1453
1454             memcpy(This->dual, This, sizeof(ICreateTypeInfo2Impl));
1455             This->dual->ref = 0;
1456             This->dual->typekind = This->typekind==TKIND_DISPATCH ?
1457                 TKIND_INTERFACE : TKIND_DISPATCH;
1458             This->dual->dual = This;
1459         }
1460
1461         /* Make sure dispatch is in typeinfos queue */
1462         if(This->typekind != TKIND_DISPATCH) {
1463             if(This->typelib->last_typeinfo == This)
1464                 This->typelib->last_typeinfo = This->dual;
1465
1466             if(This->typelib->typeinfos == This)
1467                 This->typelib->typeinfos = This->dual;
1468             else {
1469                 ICreateTypeInfo2Impl *iter;
1470
1471                 for(iter=This->typelib->typeinfos; iter->next_typeinfo!=This; iter=iter->next_typeinfo);
1472                 iter->next_typeinfo = This->dual;
1473             }
1474         } else
1475             iface = (ICreateTypeInfo2*)&This->dual->lpVtbl;
1476     }
1477
1478     if (uTypeFlags & (TYPEFLAG_FDISPATCHABLE|TYPEFLAG_FDUAL)) {
1479         static const WCHAR stdole2tlb[] = { 's','t','d','o','l','e','2','.','t','l','b',0 };
1480         ITypeLib *stdole;
1481         ITypeInfo *dispatch;
1482         HREFTYPE hreftype;
1483         HRESULT hres;
1484
1485         hres = LoadTypeLib(stdole2tlb, &stdole);
1486         if(FAILED(hres))
1487             return hres;
1488
1489         hres = ITypeLib_GetTypeInfoOfGuid(stdole, &IID_IDispatch, &dispatch);
1490         ITypeLib_Release(stdole);
1491         if(FAILED(hres))
1492             return hres;
1493
1494         hres = ICreateTypeInfo2_AddRefTypeInfo(iface, dispatch, &hreftype);
1495         ITypeInfo_Release(dispatch);
1496         if(FAILED(hres))
1497             return hres;
1498     }
1499
1500     This->typeinfo->flags = uTypeFlags;
1501     return S_OK;
1502 }
1503
1504 /******************************************************************************
1505  * ICreateTypeInfo2_SetDocString {OLEAUT32}
1506  *
1507  *  See ICreateTypeInfo_SetDocString.
1508  */
1509 static HRESULT WINAPI ICreateTypeInfo2_fnSetDocString(
1510         ICreateTypeInfo2* iface,
1511         LPOLESTR pStrDoc)
1512 {
1513     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1514
1515     int offset;
1516
1517     TRACE("(%p,%s)\n", iface, debugstr_w(pStrDoc));
1518     if (!pStrDoc)
1519         return E_INVALIDARG;
1520
1521     offset = ctl2_alloc_string(This->typelib, pStrDoc);
1522     if (offset == -1) return E_OUTOFMEMORY;
1523     This->typeinfo->docstringoffs = offset;
1524     return S_OK;
1525 }
1526
1527 /******************************************************************************
1528  * ICreateTypeInfo2_SetHelpContext {OLEAUT32}
1529  *
1530  *  See ICreateTypeInfo_SetHelpContext.
1531  */
1532 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpContext(
1533         ICreateTypeInfo2* iface,
1534         DWORD dwHelpContext)
1535 {
1536     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1537
1538     TRACE("(%p,%d)\n", iface, dwHelpContext);
1539
1540     This->typeinfo->helpcontext = dwHelpContext;
1541
1542     return S_OK;
1543 }
1544
1545 /******************************************************************************
1546  * ICreateTypeInfo2_SetVersion {OLEAUT32}
1547  *
1548  *  See ICreateTypeInfo_SetVersion.
1549  */
1550 static HRESULT WINAPI ICreateTypeInfo2_fnSetVersion(
1551         ICreateTypeInfo2* iface,
1552         WORD wMajorVerNum,
1553         WORD wMinorVerNum)
1554 {
1555     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1556
1557     TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
1558
1559     This->typeinfo->version = wMajorVerNum | (wMinorVerNum << 16);
1560     return S_OK;
1561 }
1562
1563 /******************************************************************************
1564  * ICreateTypeInfo2_AddRefTypeInfo {OLEAUT32}
1565  *
1566  *  See ICreateTypeInfo_AddRefTypeInfo.
1567  */
1568 static HRESULT WINAPI ICreateTypeInfo2_fnAddRefTypeInfo(
1569         ICreateTypeInfo2* iface,
1570         ITypeInfo* pTInfo,
1571         HREFTYPE* phRefType)
1572 {
1573     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1574
1575     ITypeLib *container;
1576     UINT index;
1577     HRESULT res;
1578
1579     TRACE("(%p,%p,%p)\n", iface, pTInfo, phRefType);
1580
1581     if(!pTInfo || !phRefType)
1582         return E_INVALIDARG;
1583
1584     /*
1585      * Unfortunately, we can't rely on the passed-in TypeInfo even having the
1586      * same internal structure as one of ours. It could be from another
1587      * implementation of ITypeInfo. So we need to do the following...
1588      */
1589     res = ITypeInfo_GetContainingTypeLib(pTInfo, &container, &index);
1590     if (FAILED(res)) {
1591         TRACE("failed to find containing typelib.\n");
1592         return res;
1593     }
1594
1595     if (container == (ITypeLib *)&This->typelib->lpVtblTypeLib2) {
1596         /* Process locally defined TypeInfo */
1597         *phRefType = This->typelib->typelib_typeinfo_offsets[index];
1598     } else {
1599         BSTR name;
1600         TLIBATTR *tlibattr;
1601         TYPEATTR *typeattr;
1602         TYPEKIND typekind;
1603         MSFT_GuidEntry guid, *check_guid;
1604         MSFT_ImpInfo impinfo;
1605         int guid_offset, import_offset;
1606         HRESULT hres;
1607
1608         /* Allocate container GUID */
1609         hres = ITypeLib_GetLibAttr(container, &tlibattr);
1610         if(FAILED(hres)) {
1611             ITypeLib_Release(container);
1612             return hres;
1613         }
1614
1615         guid.guid = tlibattr->guid;
1616         guid.hreftype = This->typelib->typelib_guids*12+2;
1617         guid.next_hash = -1;
1618
1619         guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1620         if(guid_offset == -1) {
1621             ITypeLib_ReleaseTLibAttr(container, tlibattr);
1622             ITypeLib_Release(container);
1623             return E_OUTOFMEMORY;
1624         }
1625
1626         check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1627         if(check_guid->hreftype == guid.hreftype)
1628             This->typelib->typelib_guids++;
1629
1630         /* Get import file name */
1631         hres = QueryPathOfRegTypeLib(&guid.guid, tlibattr->wMajorVerNum,
1632                 tlibattr->wMinorVerNum, tlibattr->lcid, &name);
1633         if(FAILED(hres)) {
1634             ITypeLib_ReleaseTLibAttr(container, tlibattr);
1635             ITypeLib_Release(container);
1636             return hres;
1637         }
1638
1639         /* Import file */
1640         import_offset = ctl2_alloc_importfile(This->typelib, guid_offset, tlibattr->lcid,
1641                 tlibattr->wMajorVerNum, tlibattr->wMinorVerNum, strrchrW(name, '\\')+1);
1642         ITypeLib_ReleaseTLibAttr(container, tlibattr);
1643         SysFreeString(name);
1644
1645         if(import_offset == -1) {
1646             ITypeLib_Release(container);
1647             return E_OUTOFMEMORY;
1648         }
1649
1650         /* Allocate referenced guid */
1651         hres = ITypeInfo_GetTypeAttr(pTInfo, &typeattr);
1652         if(FAILED(hres)) {
1653             ITypeLib_Release(container);
1654             return hres;
1655         }
1656
1657         guid.guid = typeattr->guid;
1658         guid.hreftype = This->typelib->typeinfo_guids*12+1;
1659         guid.next_hash = -1;
1660         typekind = typeattr->typekind;
1661         ITypeInfo_ReleaseTypeAttr(pTInfo, typeattr);
1662
1663         guid_offset = ctl2_alloc_guid(This->typelib, &guid);
1664         if(guid_offset == -1) {
1665             ITypeLib_Release(container);
1666             return E_OUTOFMEMORY;
1667         }
1668
1669         check_guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][guid_offset];
1670         if(check_guid->hreftype == guid.hreftype)
1671             This->typelib->typeinfo_guids++;
1672
1673         /* Allocate importinfo */
1674         impinfo.flags = (typekind<<24) | MSFT_IMPINFO_OFFSET_IS_GUID;
1675         impinfo.oImpFile = import_offset;
1676         impinfo.oGuid = guid_offset;
1677         *phRefType = ctl2_alloc_importinfo(This->typelib, &impinfo)+1;
1678
1679         if(!memcmp(&guid.guid, &IID_IDispatch, sizeof(GUID)))
1680             This->typelib->typelib_header.dispatchpos = *phRefType;
1681     }
1682
1683     ITypeLib_Release(container);
1684     return S_OK;
1685 }
1686
1687 /******************************************************************************
1688  * ICreateTypeInfo2_AddFuncDesc {OLEAUT32}
1689  *
1690  *  See ICreateTypeInfo_AddFuncDesc.
1691  */
1692 static HRESULT WINAPI ICreateTypeInfo2_fnAddFuncDesc(
1693         ICreateTypeInfo2* iface,
1694         UINT index,
1695         FUNCDESC* pFuncDesc)
1696 {
1697     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1698
1699     CyclicList *iter, *insert;
1700     int *typedata;
1701     int i, num_defaults = 0;
1702     int decoded_size;
1703     HRESULT hres;
1704
1705     TRACE("(%p,%d,%p)\n", iface, index, pFuncDesc);
1706
1707     if(!pFuncDesc || pFuncDesc->oVft&3)
1708         return E_INVALIDARG;
1709
1710     TRACE("{%d,%p,%p,%d,%d,%d,%d,%d,%d,%d,{%d},%d}\n", pFuncDesc->memid,
1711             pFuncDesc->lprgscode, pFuncDesc->lprgelemdescParam, pFuncDesc->funckind,
1712             pFuncDesc->invkind, pFuncDesc->callconv, pFuncDesc->cParams,
1713             pFuncDesc->cParamsOpt, pFuncDesc->oVft, pFuncDesc->cScodes,
1714             pFuncDesc->elemdescFunc.tdesc.vt, pFuncDesc->wFuncFlags);
1715
1716     if(pFuncDesc->cParamsOpt || pFuncDesc->cScodes)
1717         FIXME("Unimplemented parameter - created typelib will be incorrect\n");
1718
1719     switch(This->typekind) {
1720     case TKIND_MODULE:
1721         if(pFuncDesc->funckind != FUNC_STATIC)
1722             return TYPE_E_BADMODULEKIND;
1723         break;
1724     case TKIND_DISPATCH:
1725         if(pFuncDesc->funckind != FUNC_DISPATCH)
1726             return TYPE_E_BADMODULEKIND;
1727         break;
1728     default:
1729         if(pFuncDesc->funckind != FUNC_PUREVIRTUAL)
1730             return TYPE_E_BADMODULEKIND;
1731     }
1732
1733     if(This->typeinfo->cElement<index)
1734         return TYPE_E_ELEMENTNOTFOUND;
1735
1736     if((pFuncDesc->invkind&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF)) &&
1737         !pFuncDesc->cParams)
1738         return TYPE_E_INCONSISTENTPROPFUNCS;
1739
1740     /* get number of arguments with default values specified */
1741     for (i = 0; i < pFuncDesc->cParams; i++)
1742         if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT)
1743             num_defaults++;
1744
1745     if (!This->typedata) {
1746         This->typedata = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList));
1747         if(!This->typedata)
1748             return E_OUTOFMEMORY;
1749
1750         This->typedata->next = This->typedata;
1751         This->typedata->u.val = 0;
1752
1753         if(This->dual)
1754             This->dual->typedata = This->typedata;
1755     }
1756
1757     /* allocate type data space for us */
1758     insert = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList));
1759     if(!insert)
1760         return E_OUTOFMEMORY;
1761     insert->u.data = HeapAlloc(GetProcessHeap(), 0, sizeof(int[6])+sizeof(int[(num_defaults?4:3)])*pFuncDesc->cParams);
1762     if(!insert->u.data) {
1763         HeapFree(GetProcessHeap(), 0, insert);
1764         return E_OUTOFMEMORY;
1765     }
1766
1767     /* fill out the basic type information */
1768     typedata = insert->u.data;
1769     typedata[0] = 0x18 + pFuncDesc->cParams*(num_defaults?16:12);
1770     ctl2_encode_typedesc(This->typelib, &pFuncDesc->elemdescFunc.tdesc, &typedata[1], NULL, NULL, &decoded_size);
1771     typedata[2] = pFuncDesc->wFuncFlags;
1772     typedata[3] = ((sizeof(FUNCDESC) + decoded_size) << 16) | (unsigned short)(pFuncDesc->oVft?pFuncDesc->oVft+1:0);
1773     typedata[4] = (pFuncDesc->callconv << 8) | (pFuncDesc->invkind << 3) | pFuncDesc->funckind;
1774     if(num_defaults) typedata[4] |= 0x1000;
1775     typedata[5] = pFuncDesc->cParams;
1776
1777     /* NOTE: High word of typedata[3] is total size of FUNCDESC + size of all ELEMDESCs for params + TYPEDESCs for pointer params and return types. */
1778     /* That is, total memory allocation required to reconstitute the FUNCDESC in its entirety. */
1779     typedata[3] += (sizeof(ELEMDESC) * pFuncDesc->cParams) << 16;
1780     typedata[3] += (sizeof(PARAMDESCEX) * num_defaults) << 16;
1781
1782     /* add default values */
1783     if(num_defaults) {
1784         for (i = 0; i < pFuncDesc->cParams; i++)
1785             if(pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags & PARAMFLAG_FHASDEFAULT) {
1786                 hres = ctl2_add_default_value(This->typelib, typedata+6+i,
1787                         &pFuncDesc->lprgelemdescParam[i].u.paramdesc.pparamdescex->varDefaultValue,
1788                         pFuncDesc->lprgelemdescParam[i].tdesc.vt);
1789
1790                 if(FAILED(hres)) {
1791                     HeapFree(GetProcessHeap(), 0, insert->u.data);
1792                     HeapFree(GetProcessHeap(), 0, insert);
1793                     return hres;
1794                 }
1795             } else
1796                 typedata[6+i] = 0xffffffff;
1797
1798         num_defaults = pFuncDesc->cParams;
1799     }
1800
1801     /* add arguments */
1802     for (i = 0; i < pFuncDesc->cParams; i++) {
1803         ctl2_encode_typedesc(This->typelib, &pFuncDesc->lprgelemdescParam[i].tdesc,
1804                 &typedata[6+num_defaults+(i*3)], NULL, NULL, &decoded_size);
1805         typedata[7+num_defaults+(i*3)] = -1;
1806         typedata[8+num_defaults+(i*3)] = pFuncDesc->lprgelemdescParam[i].u.paramdesc.wParamFlags;
1807         typedata[3] += decoded_size << 16;
1808     }
1809
1810     /* update the index data */
1811     insert->indice = pFuncDesc->memid;
1812     insert->name = -1;
1813
1814     /* insert type data to list */
1815     if(index == This->typeinfo->cElement) {
1816         insert->next = This->typedata->next;
1817         This->typedata->next = insert;
1818         This->typedata = insert;
1819
1820         if(This->dual)
1821             This->dual->typedata = This->typedata;
1822     } else {
1823         iter = This->typedata->next;
1824         for(i=0; i<index; i++)
1825             iter = iter->next;
1826
1827         insert->next = iter->next;
1828         iter->next = insert;
1829     }
1830
1831     /* update type data size */
1832     This->typedata->next->u.val += 0x18 + pFuncDesc->cParams*(num_defaults?16:12);
1833
1834     /* Increment the number of function elements */
1835     This->typeinfo->cElement += 1;
1836
1837     return S_OK;
1838 }
1839
1840 /******************************************************************************
1841  * ICreateTypeInfo2_AddImplType {OLEAUT32}
1842  *
1843  *  See ICreateTypeInfo_AddImplType.
1844  */
1845 static HRESULT WINAPI ICreateTypeInfo2_fnAddImplType(
1846         ICreateTypeInfo2* iface,
1847         UINT index,
1848         HREFTYPE hRefType)
1849 {
1850     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1851
1852     TRACE("(%p,%d,%d)\n", iface, index, hRefType);
1853
1854     if (This->typekind == TKIND_COCLASS) {
1855         int offset;
1856         MSFT_RefRecord *ref;
1857
1858         if (index == 0) {
1859             if (This->typeinfo->datatype1 != -1) return TYPE_E_ELEMENTNOTFOUND;
1860
1861             offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
1862             if (offset == -1) return E_OUTOFMEMORY;
1863
1864             This->typeinfo->datatype1 = offset;
1865         } else {
1866             int lastoffset;
1867
1868             lastoffset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index - 1);
1869             if (lastoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
1870
1871             ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][lastoffset];
1872             if (ref->onext != -1) return TYPE_E_ELEMENTNOTFOUND;
1873
1874             offset = ctl2_alloc_segment(This->typelib, MSFT_SEG_REFERENCES, sizeof(MSFT_RefRecord), 0);
1875             if (offset == -1) return E_OUTOFMEMORY;
1876
1877             ref->onext = offset;
1878         }
1879
1880         ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1881
1882         ref->reftype = hRefType;
1883         ref->flags = 0;
1884         ref->oCustData = -1;
1885         ref->onext = -1;
1886         This->typeinfo->cImplTypes++;
1887     } else if (This->typekind == TKIND_INTERFACE) {
1888         if (This->typeinfo->cImplTypes && index==1)
1889             return TYPE_E_BADMODULEKIND;
1890
1891         if( index != 0)  return TYPE_E_ELEMENTNOTFOUND;
1892
1893         This->typeinfo->datatype1 = hRefType;
1894         This->typeinfo->cImplTypes = 1;
1895     } else if (This->typekind == TKIND_DISPATCH) {
1896         if(index != 0) return TYPE_E_ELEMENTNOTFOUND;
1897
1898         /* FIXME: Check if referenced typeinfo is IDispatch */
1899         This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
1900         This->typeinfo->cImplTypes = 1;
1901     } else {
1902         FIXME("AddImplType unsupported on typekind %d\n", This->typekind);
1903         return E_OUTOFMEMORY;
1904     }
1905
1906     return S_OK;
1907 }
1908
1909 /******************************************************************************
1910  * ICreateTypeInfo2_SetImplTypeFlags {OLEAUT32}
1911  *
1912  *  See ICreateTypeInfo_SetImplTypeFlags.
1913  */
1914 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeFlags(
1915         ICreateTypeInfo2* iface,
1916         UINT index,
1917         INT implTypeFlags)
1918 {
1919     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1920     int offset;
1921     MSFT_RefRecord *ref;
1922
1923     TRACE("(%p,%d,0x%x)\n", iface, index, implTypeFlags);
1924
1925     if (This->typekind != TKIND_COCLASS) {
1926         return TYPE_E_BADMODULEKIND;
1927     }
1928
1929     offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
1930     if (offset == -1) return TYPE_E_ELEMENTNOTFOUND;
1931
1932     ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
1933     ref->flags = implTypeFlags;
1934
1935     return S_OK;
1936 }
1937
1938 /******************************************************************************
1939  * ICreateTypeInfo2_SetAlignment {OLEAUT32}
1940  *
1941  *  See ICreateTypeInfo_SetAlignment.
1942  */
1943 static HRESULT WINAPI ICreateTypeInfo2_fnSetAlignment(
1944         ICreateTypeInfo2* iface,
1945         WORD cbAlignment)
1946 {
1947     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
1948
1949     TRACE("(%p,%d)\n", iface, cbAlignment);
1950
1951     if (!cbAlignment) return E_INVALIDARG;
1952     if (cbAlignment > 16) return E_INVALIDARG;
1953
1954     This->typeinfo->typekind &= ~0xffc0;
1955     This->typeinfo->typekind |= cbAlignment << 6;
1956
1957     /* FIXME: There's probably some way to simplify this. */
1958     switch (This->typekind) {
1959     case TKIND_ALIAS:
1960     default:
1961         break;
1962
1963     case TKIND_ENUM:
1964     case TKIND_INTERFACE:
1965     case TKIND_DISPATCH:
1966     case TKIND_COCLASS:
1967         if (cbAlignment > 4) cbAlignment = 4;
1968         break;
1969
1970     case TKIND_RECORD:
1971     case TKIND_MODULE:
1972     case TKIND_UNION:
1973         cbAlignment = 1;
1974         break;
1975     }
1976
1977     This->typeinfo->typekind |= cbAlignment << 11;
1978
1979     return S_OK;
1980 }
1981
1982 /******************************************************************************
1983  * ICreateTypeInfo2_SetSchema {OLEAUT32}
1984  *
1985  *  See ICreateTypeInfo_SetSchema.
1986  */
1987 static HRESULT WINAPI ICreateTypeInfo2_fnSetSchema(
1988         ICreateTypeInfo2* iface,
1989         LPOLESTR pStrSchema)
1990 {
1991     FIXME("(%p,%s), stub!\n", iface, debugstr_w(pStrSchema));
1992     return E_OUTOFMEMORY;
1993 }
1994
1995 /******************************************************************************
1996  * ICreateTypeInfo2_AddVarDesc {OLEAUT32}
1997  *
1998  *  See ICreateTypeInfo_AddVarDesc.
1999  */
2000 static HRESULT WINAPI ICreateTypeInfo2_fnAddVarDesc(
2001         ICreateTypeInfo2* iface,
2002         UINT index,
2003         VARDESC* pVarDesc)
2004 {
2005     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2006
2007     CyclicList *insert;
2008     INT *typedata;
2009     int var_datawidth;
2010     int var_alignment;
2011     int var_type_size;
2012     int alignment;
2013
2014     TRACE("(%p,%d,%p), stub!\n", iface, index, pVarDesc);
2015     TRACE("%d, %p, %d, {{%x, %d}, {%p, %x}}, 0x%x, %d\n", pVarDesc->memid, pVarDesc->lpstrSchema, pVarDesc->u.oInst,
2016           pVarDesc->elemdescVar.tdesc.u.hreftype, pVarDesc->elemdescVar.tdesc.vt,
2017           pVarDesc->elemdescVar.u.paramdesc.pparamdescex, pVarDesc->elemdescVar.u.paramdesc.wParamFlags,
2018           pVarDesc->wVarFlags, pVarDesc->varkind);
2019
2020     if ((This->typeinfo->cElement >> 16) != index) {
2021         TRACE("Out-of-order element.\n");
2022         return TYPE_E_ELEMENTNOTFOUND;
2023     }
2024
2025     if (!This->typedata) {
2026         This->typedata = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList));
2027         if(!This->typedata)
2028             return E_OUTOFMEMORY;
2029
2030         This->typedata->next = This->typedata;
2031         This->typedata->u.val = 0;
2032
2033         if(This->dual)
2034             This->dual->typedata = This->typedata;
2035     }
2036
2037     /* allocate type data space for us */
2038     insert = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList));
2039     if(!insert)
2040         return E_OUTOFMEMORY;
2041     insert->u.data = HeapAlloc(GetProcessHeap(), 0, sizeof(int[5]));
2042     if(!insert->u.data) {
2043         HeapFree(GetProcessHeap(), 0, insert);
2044         return E_OUTOFMEMORY;
2045     }
2046
2047     insert->next = This->typedata->next;
2048     This->typedata->next = insert;
2049     This->typedata = insert;
2050
2051     if(This->dual)
2052         This->dual->typedata = This->typedata;
2053
2054     This->typedata->next->u.val += 0x14;
2055     typedata = This->typedata->u.data;
2056
2057     /* fill out the basic type information */
2058     typedata[0] = 0x14 | (index << 16);
2059     typedata[2] = pVarDesc->wVarFlags;
2060     typedata[3] = (sizeof(VARDESC) << 16) | 0;
2061
2062     /* update the index data */
2063     insert->indice = 0x40000000 + index;
2064     insert->name = -1;
2065
2066     /* figure out type widths and whatnot */
2067     ctl2_encode_typedesc(This->typelib, &pVarDesc->elemdescVar.tdesc,
2068                          &typedata[1], &var_datawidth, &var_alignment,
2069                          &var_type_size);
2070
2071     /* pad out starting position to data width */
2072     This->datawidth += var_alignment - 1;
2073     This->datawidth &= ~(var_alignment - 1);
2074     typedata[4] = This->datawidth;
2075     
2076     /* add the new variable to the total data width */
2077     This->datawidth += var_datawidth;
2078     if(This->dual)
2079         This->dual->datawidth = This->datawidth;
2080
2081     /* add type description size to total required allocation */
2082     typedata[3] += var_type_size << 16;
2083
2084     /* fix type alignment */
2085     alignment = (This->typeinfo->typekind >> 11) & 0x1f;
2086     if (alignment < var_alignment) {
2087         alignment = var_alignment;
2088         This->typeinfo->typekind &= ~0xf800;
2089         This->typeinfo->typekind |= alignment << 11;
2090     }
2091
2092     /* ??? */
2093     if (!This->typeinfo->res2) This->typeinfo->res2 = 0x1a;
2094     if ((index == 0) || (index == 1) || (index == 2) || (index == 4) || (index == 9)) {
2095         This->typeinfo->res2 <<= 1;
2096     }
2097
2098     /* ??? */
2099     if (This->typeinfo->res3 == -1) This->typeinfo->res3 = 0;
2100     This->typeinfo->res3 += 0x2c;
2101
2102     /* increment the number of variable elements */
2103     This->typeinfo->cElement += 0x10000;
2104
2105     /* pad data width to alignment */
2106     This->typeinfo->size = (This->datawidth + (alignment - 1)) & ~(alignment - 1);
2107
2108     return S_OK;
2109 }
2110
2111 /******************************************************************************
2112  * ICreateTypeInfo2_SetFuncAndParamNames {OLEAUT32}
2113  *
2114  *  See ICreateTypeInfo_SetFuncAndParamNames.
2115  */
2116 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncAndParamNames(
2117         ICreateTypeInfo2* iface,
2118         UINT index,
2119         LPOLESTR* rgszNames,
2120         UINT cNames)
2121 {
2122     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2123     CyclicList *iter = NULL, *iter2;
2124     int offset, len, i=0;
2125     char *namedata;
2126
2127     TRACE("(%p %d %p %d)\n", iface, index, rgszNames, cNames);
2128
2129     if(!rgszNames)
2130         return E_INVALIDARG;
2131
2132     if(index >= This->typeinfo->cElement || !cNames)
2133         return TYPE_E_ELEMENTNOTFOUND;
2134
2135     len = ctl2_encode_name(This->typelib, rgszNames[0], &namedata);
2136     for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2137         if(i == index)
2138             iter = iter2;
2139         else if(iter2->name!=-1 && !memcmp(namedata,
2140                     This->typelib->typelib_segment_data[MSFT_SEG_NAME]+iter2->name+8, len))
2141             return TYPE_E_AMBIGUOUSNAME;
2142
2143         i++;
2144     }
2145
2146     /* cNames == cParams for put or putref accessor, cParams+1 otherwise */
2147     if(cNames != iter->u.data[5] + ((iter->u.data[4]>>3)&(INVOKE_PROPERTYPUT|INVOKE_PROPERTYPUTREF) ? 0 : 1))
2148         return TYPE_E_ELEMENTNOTFOUND;
2149
2150     offset = ctl2_alloc_name(This->typelib, rgszNames[0]);
2151     if(offset == -1)
2152         return E_OUTOFMEMORY;
2153
2154     iter->name = offset;
2155
2156     namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2157     *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2158
2159     if(iter->u.data[4]&0x1000)
2160         len = iter->u.data[5];
2161     else
2162         len = 0;
2163
2164     for (i = 1; i < cNames; i++) {
2165         offset = ctl2_alloc_name(This->typelib, rgszNames[i]);
2166         iter->u.data[(i*3) + 4 + len] = offset;
2167     }
2168
2169     return S_OK;
2170 }
2171
2172 /******************************************************************************
2173  * ICreateTypeInfo2_SetVarName {OLEAUT32}
2174  *
2175  *  See ICreateTypeInfo_SetVarName.
2176  */
2177 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarName(
2178         ICreateTypeInfo2* iface,
2179         UINT index,
2180         LPOLESTR szName)
2181 {
2182     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2183     CyclicList *iter;
2184     int offset, i;
2185     char *namedata;
2186
2187     TRACE("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szName));
2188
2189     if ((This->typeinfo->cElement >> 16) <= index) {
2190         TRACE("Out-of-order element.\n");
2191         return TYPE_E_ELEMENTNOTFOUND;
2192     }
2193
2194     offset = ctl2_alloc_name(This->typelib, szName);
2195     if (offset == -1) return E_OUTOFMEMORY;
2196
2197     namedata = This->typelib->typelib_segment_data[MSFT_SEG_NAME] + offset;
2198     if (*((INT *)namedata) == -1) {
2199         *((INT *)namedata) = This->typelib->typelib_typeinfo_offsets[This->typeinfo->typekind >> 16];
2200         namedata[9] |= 0x10;
2201     }
2202     if (This->typekind == TKIND_ENUM) {
2203         namedata[9] |= 0x20;
2204     }
2205
2206     iter = This->typedata->next->next;
2207     for(i=0; i<index; i++)
2208         iter = iter->next;
2209
2210     iter->name = offset;
2211     return S_OK;
2212 }
2213
2214 /******************************************************************************
2215  * ICreateTypeInfo2_SetTypeDescAlias {OLEAUT32}
2216  *
2217  *  See ICreateTypeInfo_SetTypeDescAlias.
2218  */
2219 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeDescAlias(
2220         ICreateTypeInfo2* iface,
2221         TYPEDESC* pTDescAlias)
2222 {
2223     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2224
2225     int encoded_typedesc;
2226     int width;
2227
2228     if (This->typekind != TKIND_ALIAS) {
2229         return TYPE_E_WRONGTYPEKIND;
2230     }
2231
2232     FIXME("(%p,%p), hack!\n", iface, pTDescAlias);
2233
2234     if (ctl2_encode_typedesc(This->typelib, pTDescAlias, &encoded_typedesc, &width, NULL, NULL) == -1) {
2235         return E_OUTOFMEMORY;
2236     }
2237
2238     This->typeinfo->size = width;
2239     This->typeinfo->datatype1 = encoded_typedesc;
2240
2241     return S_OK;
2242 }
2243
2244 /******************************************************************************
2245  * ICreateTypeInfo2_DefineFuncAsDllEntry {OLEAUT32}
2246  *
2247  *  See ICreateTypeInfo_DefineFuncAsDllEntry.
2248  */
2249 static HRESULT WINAPI ICreateTypeInfo2_fnDefineFuncAsDllEntry(
2250         ICreateTypeInfo2* iface,
2251         UINT index,
2252         LPOLESTR szDllName,
2253         LPOLESTR szProcName)
2254 {
2255     FIXME("(%p,%d,%s,%s), stub!\n", iface, index, debugstr_w(szDllName), debugstr_w(szProcName));
2256     return E_OUTOFMEMORY;
2257 }
2258
2259 /******************************************************************************
2260  * ICreateTypeInfo2_SetFuncDocString {OLEAUT32}
2261  *
2262  *  See ICreateTypeInfo_SetFuncDocString.
2263  */
2264 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncDocString(
2265         ICreateTypeInfo2* iface,
2266         UINT index,
2267         LPOLESTR szDocString)
2268 {
2269     FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2270     return E_OUTOFMEMORY;
2271 }
2272
2273 /******************************************************************************
2274  * ICreateTypeInfo2_SetVarDocString {OLEAUT32}
2275  *
2276  *  See ICreateTypeInfo_SetVarDocString.
2277  */
2278 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarDocString(
2279         ICreateTypeInfo2* iface,
2280         UINT index,
2281         LPOLESTR szDocString)
2282 {
2283     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2284
2285     FIXME("(%p,%d,%s), stub!\n", iface, index, debugstr_w(szDocString));
2286
2287     ctl2_alloc_string(This->typelib, szDocString);
2288
2289     return E_OUTOFMEMORY;
2290 }
2291
2292 /******************************************************************************
2293  * ICreateTypeInfo2_SetFuncHelpContext {OLEAUT32}
2294  *
2295  *  See ICreateTypeInfo_SetFuncHelpContext.
2296  */
2297 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpContext(
2298         ICreateTypeInfo2* iface,
2299         UINT index,
2300         DWORD dwHelpContext)
2301 {
2302     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2303     CyclicList *func;
2304     int *typedata;
2305     int size;
2306
2307     TRACE("(%p,%d,%d)\n", iface, index, dwHelpContext);
2308
2309     if(This->typeinfo->cElement<index)
2310         return TYPE_E_ELEMENTNOTFOUND;
2311
2312     if(This->typeinfo->cElement == index)
2313         func = This->typedata;
2314     else
2315         for(func=This->typedata->next->next; func!=This->typedata; func=func->next)
2316             if(index-- == 0)
2317                 break;
2318
2319     typedata = func->u.data;
2320
2321     /* Compute func size without arguments */
2322     size = typedata[0] - typedata[5]*(typedata[4]&0x1000?16:12);
2323
2324     /* Allocate memory for HelpContext if needed */
2325     if(size < 7*sizeof(int)) {
2326         typedata = HeapReAlloc(GetProcessHeap(), 0, typedata, typedata[0]+sizeof(int));
2327         if(!typedata)
2328             return E_OUTOFMEMORY;
2329
2330         memmove(&typedata[7], &typedata[6], typedata[0]-sizeof(int)*6);
2331         typedata[0] += sizeof(int);
2332         This->typedata->next->u.val += sizeof(int);
2333         func->u.data = typedata;
2334     }
2335
2336     typedata[6] = dwHelpContext;
2337     return S_OK;
2338 }
2339
2340 /******************************************************************************
2341  * ICreateTypeInfo2_SetVarHelpContext {OLEAUT32}
2342  *
2343  *  See ICreateTypeInfo_SetVarHelpContext.
2344  */
2345 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpContext(
2346         ICreateTypeInfo2* iface,
2347         UINT index,
2348         DWORD dwHelpContext)
2349 {
2350     FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpContext);
2351     return E_OUTOFMEMORY;
2352 }
2353
2354 /******************************************************************************
2355  * ICreateTypeInfo2_SetMops {OLEAUT32}
2356  *
2357  *  See ICreateTypeInfo_SetMops.
2358  */
2359 static HRESULT WINAPI ICreateTypeInfo2_fnSetMops(
2360         ICreateTypeInfo2* iface,
2361         UINT index,
2362         BSTR bstrMops)
2363 {
2364     FIXME("(%p,%d,%p), stub!\n", iface, index, bstrMops);
2365     return E_OUTOFMEMORY;
2366 }
2367
2368 /******************************************************************************
2369  * ICreateTypeInfo2_SetTypeIdldesc {OLEAUT32}
2370  *
2371  *  See ICreateTypeInfo_SetTypeIdldesc.
2372  */
2373 static HRESULT WINAPI ICreateTypeInfo2_fnSetTypeIdldesc(
2374         ICreateTypeInfo2* iface,
2375         IDLDESC* pIdlDesc)
2376 {
2377     FIXME("(%p,%p), stub!\n", iface, pIdlDesc);
2378     return E_OUTOFMEMORY;
2379 }
2380
2381 /******************************************************************************
2382  * ICreateTypeInfo2_LayOut {OLEAUT32}
2383  *
2384  *  See ICreateTypeInfo_LayOut.
2385  */
2386 static HRESULT WINAPI ICreateTypeInfo2_fnLayOut(
2387         ICreateTypeInfo2* iface)
2388 {
2389     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2390     CyclicList *iter, *iter2, **typedata;
2391     HREFTYPE hreftype;
2392     HRESULT hres;
2393     unsigned user_vft = 0;
2394     int i;
2395
2396     TRACE("(%p)\n", iface);
2397
2398     /* FIXME: LayOut should be run on all ImplTypes */
2399     if(This->typekind == TKIND_COCLASS)
2400         return S_OK;
2401
2402     /* Validate inheritance */
2403     This->typeinfo->datatype2 = 0;
2404     hreftype = This->typeinfo->datatype1;
2405
2406     /* Process internally defined interfaces */
2407     for(i=0; i<This->typelib->typelib_header.nrtypeinfos; i++) {
2408         MSFT_TypeInfoBase *header;
2409
2410         if(hreftype&1)
2411             break;
2412
2413         header = (MSFT_TypeInfoBase*)&(This->typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][hreftype]);
2414         This->typeinfo->datatype2 += (header->cElement<<16) + 1;
2415         hreftype = header->datatype1;
2416     }
2417     if(i == This->typelib->typelib_header.nrtypeinfos)
2418         return TYPE_E_CIRCULARTYPE;
2419
2420     /* Process externally defined interfaces */
2421     if(hreftype != -1) {
2422         ITypeInfo *cur, *next;
2423         TYPEATTR *typeattr;
2424
2425         hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&next);
2426         if(FAILED(hres))
2427             return hres;
2428
2429         hres = ITypeInfo_GetRefTypeInfo(next, hreftype, &cur);
2430         ITypeInfo_Release(next);
2431         if(FAILED(hres))
2432             return hres;
2433
2434
2435         while(1) {
2436             hres = ITypeInfo_GetTypeAttr(cur, &typeattr);
2437             if(FAILED(hres)) {
2438                 ITypeInfo_Release(cur);
2439                 return hres;
2440             }
2441
2442             if(!memcmp(&typeattr->guid, &IID_IDispatch, sizeof(IDispatch)))
2443                 This->typeinfo->flags |= TYPEFLAG_FDISPATCHABLE;
2444
2445             This->typeinfo->datatype2 += (typeattr->cFuncs<<16) + 1;
2446             ITypeInfo_ReleaseTypeAttr(cur, typeattr);
2447
2448             hres = ITypeInfo_GetRefTypeOfImplType(cur, 0, &hreftype);
2449             if(hres == TYPE_E_ELEMENTNOTFOUND)
2450                 break;
2451             if(FAILED(hres)) {
2452                 ITypeInfo_Release(cur);
2453                 return hres;
2454             }
2455
2456             hres = ITypeInfo_GetRefTypeInfo(cur, hreftype, &next);
2457             if(FAILED(hres)) {
2458                 ITypeInfo_Release(cur);
2459                 return hres;
2460             }
2461
2462             ITypeInfo_Release(cur);
2463             cur = next;
2464         }
2465         ITypeInfo_Release(cur);
2466     }
2467
2468     /* Get cbSizeVft of inherited interface */
2469     /* Makes LayOut running recursively */
2470     if(This->typeinfo->datatype1 != -1) {
2471         ITypeInfo *cur, *inherited;
2472         TYPEATTR *typeattr;
2473
2474         hres = ICreateTypeInfo_QueryInterface(iface, &IID_ITypeInfo, (void**)&cur);
2475         if(FAILED(hres))
2476             return hres;
2477
2478         hres = ITypeInfo_GetRefTypeInfo(cur, This->typeinfo->datatype1, &inherited);
2479         ITypeInfo_Release(cur);
2480         if(FAILED(hres))
2481             return hres;
2482
2483         hres = ITypeInfo_GetTypeAttr(inherited, &typeattr);
2484         if(FAILED(hres)) {
2485             ITypeInfo_Release(inherited);
2486             return hres;
2487         }
2488
2489         This->typeinfo->cbSizeVft = typeattr->cbSizeVft * 4 / sizeof(void *);
2490
2491         ITypeInfo_ReleaseTypeAttr(inherited, typeattr);
2492         ITypeInfo_Release(inherited);
2493     } else
2494         This->typeinfo->cbSizeVft = 0;
2495
2496     if(!This->typedata)
2497         return S_OK;
2498
2499     typedata = HeapAlloc(GetProcessHeap(), 0, sizeof(CyclicList*)*(This->typeinfo->cElement&0xffff));
2500     if(!typedata)
2501         return E_OUTOFMEMORY;
2502
2503     /* Assign IDs and VTBL entries */
2504     i = 0;
2505     if(This->typedata->u.data[3]&1)
2506         user_vft = This->typedata->u.data[3]&0xffff;
2507
2508     for(iter=This->typedata->next->next; iter!=This->typedata->next; iter=iter->next) {
2509         /* Assign MEMBERID if MEMBERID_NIL was specified */
2510         if(iter->indice == MEMBERID_NIL) {
2511             iter->indice = 0x60000000 + i + (This->typeinfo->datatype2<<16);
2512
2513             for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2514                 if(iter == iter2) continue;
2515                 if(iter2->indice == iter->indice) {
2516                     iter->indice = 0x5fffffff + This->typeinfo->cElement + i + (This->typeinfo->datatype2<<16);
2517
2518                     for(iter2=This->typedata->next->next; iter2!=This->typedata->next; iter2=iter2->next) {
2519                         if(iter == iter2) continue;
2520                         if(iter2->indice == iter->indice) {
2521                             HeapFree(GetProcessHeap(), 0, typedata);
2522                             return E_ACCESSDENIED;
2523                         }
2524                     }
2525
2526                     break;
2527                 }
2528             }
2529         }
2530
2531         typedata[i] = iter;
2532
2533         iter->u.data[0] = (iter->u.data[0]&0xffff) | (i<<16);
2534
2535         if((iter->u.data[3]&1) != (user_vft&1)) {
2536             HeapFree(GetProcessHeap(), 0, typedata);
2537             return TYPE_E_INVALIDID;
2538         }
2539
2540         if(user_vft&1) {
2541             if(user_vft < (iter->u.data[3]&0xffff))
2542                 user_vft = (iter->u.data[3]&0xffff);
2543
2544             if((iter->u.data[3]&0xffff) < This->typeinfo->cbSizeVft) {
2545                 HeapFree(GetProcessHeap(), 0, typedata);
2546                 return TYPE_E_INVALIDID;
2547             }
2548         } else if(This->typekind != TKIND_MODULE) {
2549             iter->u.data[3] = (iter->u.data[3]&0xffff0000) | This->typeinfo->cbSizeVft;
2550             This->typeinfo->cbSizeVft += 4;
2551         }
2552
2553         /* Construct a list of elements with the same memberid */
2554         iter->u.data[4] = (iter->u.data[4]&0xffff) | (i<<16);
2555         for(iter2=This->typedata->next->next; iter2!=iter; iter2=iter2->next) {
2556             if(iter->indice == iter2->indice) {
2557                 int v1, v2;
2558
2559                 v1 = iter->u.data[4] >> 16;
2560                 v2 = iter2->u.data[4] >> 16;
2561
2562                 iter->u.data[4] = (iter->u.data[4]&0xffff) | (v2<<16);
2563                 iter2->u.data[4] = (iter2->u.data[4]&0xffff) | (v1<<16);
2564                 break;
2565             }
2566         }
2567
2568         i++;
2569     }
2570
2571     if(user_vft)
2572         This->typeinfo->cbSizeVft = user_vft+3;
2573
2574     for(i=0; i<(This->typeinfo->cElement&0xffff); i++) {
2575         if(typedata[i]->u.data[4]>>16 > i) {
2576             int inv;
2577
2578             inv = (typedata[i]->u.data[4]>>3) & 0xf;
2579             i = typedata[i]->u.data[4] >> 16;
2580
2581             while(i > typedata[i]->u.data[4]>>16) {
2582                 int invkind = (typedata[i]->u.data[4]>>3) & 0xf;
2583
2584                 if(inv & invkind) {
2585                     HeapFree(GetProcessHeap(), 0, typedata);
2586                     return TYPE_E_DUPLICATEID;
2587                 }
2588
2589                 i = typedata[i]->u.data[4] >> 16;
2590                 inv |= invkind;
2591             }
2592
2593             if(inv & INVOKE_FUNC) {
2594                 HeapFree(GetProcessHeap(), 0, typedata);
2595                 return TYPE_E_INCONSISTENTPROPFUNCS;
2596             }
2597         }
2598     }
2599
2600     HeapFree(GetProcessHeap(), 0, typedata);
2601     return S_OK;
2602 }
2603
2604 /******************************************************************************
2605  * ICreateTypeInfo2_DeleteFuncDesc {OLEAUT32}
2606  *
2607  *  Delete a function description from a type.
2608  *
2609  * RETURNS
2610  *
2611  *  Success: S_OK.
2612  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2613  */
2614 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDesc(
2615         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2616         UINT index)              /* [I] The index of the function to delete. */
2617 {
2618     FIXME("(%p,%d), stub!\n", iface, index);
2619     return E_OUTOFMEMORY;
2620 }
2621
2622 /******************************************************************************
2623  * ICreateTypeInfo2_DeleteFuncDescByMemId {OLEAUT32}
2624  *
2625  *  Delete a function description from a type.
2626  *
2627  * RETURNS
2628  *
2629  *  Success: S_OK.
2630  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2631  */
2632 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteFuncDescByMemId(
2633         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete a function. */
2634         MEMBERID memid,          /* [I] The member id of the function to delete. */
2635         INVOKEKIND invKind)      /* [I] The invocation type of the function to delete. (?) */
2636 {
2637     FIXME("(%p,%d,%d), stub!\n", iface, memid, invKind);
2638     return E_OUTOFMEMORY;
2639 }
2640
2641 /******************************************************************************
2642  * ICreateTypeInfo2_DeleteVarDesc {OLEAUT32}
2643  *
2644  *  Delete a variable description from a type.
2645  *
2646  * RETURNS
2647  *
2648  *  Success: S_OK.
2649  *  Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2650  *  TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2651  */
2652 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDesc(
2653         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2654         UINT index)              /* [I] The index of the variable description to delete. */
2655 {
2656     FIXME("(%p,%d), stub!\n", iface, index);
2657     return E_OUTOFMEMORY;
2658 }
2659
2660 /******************************************************************************
2661  * ICreateTypeInfo2_DeleteVarDescByMemId {OLEAUT32}
2662  *
2663  *  Delete a variable description from a type.
2664  *
2665  * RETURNS
2666  *
2667  *  Success: S_OK.
2668  *  Failure: One of E_OUTOFMEMORY, E_INVALIDARG, TYPE_E_IOERROR,
2669  *  TYPE_E_INVDATAREAD, TYPE_E_UNSUPFORMAT or TYPE_E_INVALIDSTATE.
2670  */
2671 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteVarDescByMemId(
2672         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete the variable description. */
2673         MEMBERID memid)          /* [I] The member id of the variable description to delete. */
2674 {
2675     FIXME("(%p,%d), stub!\n", iface, memid);
2676     return E_OUTOFMEMORY;
2677 }
2678
2679 /******************************************************************************
2680  * ICreateTypeInfo2_DeleteImplType {OLEAUT32}
2681  *
2682  *  Delete an interface implementation from a type. (?)
2683  *
2684  * RETURNS
2685  *
2686  *  Success: S_OK.
2687  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2688  */
2689 static HRESULT WINAPI ICreateTypeInfo2_fnDeleteImplType(
2690         ICreateTypeInfo2* iface, /* [I] The typeinfo from which to delete. */
2691         UINT index)              /* [I] The index of the interface to delete. */
2692 {
2693     FIXME("(%p,%d), stub!\n", iface, index);
2694     return E_OUTOFMEMORY;
2695 }
2696
2697 /******************************************************************************
2698  * ICreateTypeInfo2_SetCustData {OLEAUT32}
2699  *
2700  *  Set the custom data for a type.
2701  *
2702  * RETURNS
2703  *
2704  *  Success: S_OK.
2705  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2706  */
2707 static HRESULT WINAPI ICreateTypeInfo2_fnSetCustData(
2708         ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2709         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2710         VARIANT* pVarVal)        /* [I] The custom data. */
2711 {
2712     ICreateTypeInfo2Impl *This = (ICreateTypeInfo2Impl *)iface;
2713
2714     TRACE("(%p,%s,%p)!\n", iface, debugstr_guid(guid), pVarVal);
2715
2716     if (!pVarVal)
2717             return E_INVALIDARG;
2718
2719     return ctl2_set_custdata(This->typelib, guid, pVarVal, &This->typeinfo->oCustData);
2720 }
2721
2722 /******************************************************************************
2723  * ICreateTypeInfo2_SetFuncCustData {OLEAUT32}
2724  *
2725  *  Set the custom data for a function.
2726  *
2727  * RETURNS
2728  *
2729  *  Success: S_OK.
2730  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2731  */
2732 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncCustData(
2733         ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2734         UINT index,              /* [I] The index of the function for which to set the custom data. */
2735         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2736         VARIANT* pVarVal)        /* [I] The custom data. */
2737 {
2738     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
2739     return E_OUTOFMEMORY;
2740 }
2741
2742 /******************************************************************************
2743  * ICreateTypeInfo2_SetParamCustData {OLEAUT32}
2744  *
2745  *  Set the custom data for a function parameter.
2746  *
2747  * RETURNS
2748  *
2749  *  Success: S_OK.
2750  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2751  */
2752 static HRESULT WINAPI ICreateTypeInfo2_fnSetParamCustData(
2753         ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2754         UINT indexFunc,          /* [I] The index of the function on which the parameter resides. */
2755         UINT indexParam,         /* [I] The index of the parameter on which to set the custom data. */
2756         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2757         VARIANT* pVarVal)        /* [I] The custom data. */
2758 {
2759     FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
2760     return E_OUTOFMEMORY;
2761 }
2762
2763 /******************************************************************************
2764  * ICreateTypeInfo2_SetVarCustData {OLEAUT32}
2765  *
2766  *  Set the custom data for a variable.
2767  *
2768  * RETURNS
2769  *
2770  *  Success: S_OK.
2771  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2772  */
2773 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarCustData(
2774         ICreateTypeInfo2* iface, /* [I] The typeinfo in which to set the custom data. */
2775         UINT index,              /* [I] The index of the variable on which to set the custom data. */
2776         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2777         VARIANT* pVarVal)        /* [I] The custom data. */
2778 {
2779     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
2780     return E_OUTOFMEMORY;
2781 }
2782
2783 /******************************************************************************
2784  * ICreateTypeInfo2_SetImplTypeCustData {OLEAUT32}
2785  *
2786  *  Set the custom data for an implemented interface.
2787  *
2788  * RETURNS
2789  *
2790  *  Success: S_OK.
2791  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2792  */
2793 static HRESULT WINAPI ICreateTypeInfo2_fnSetImplTypeCustData(
2794         ICreateTypeInfo2* iface, /* [I] The typeinfo on which to set the custom data. */
2795         UINT index,              /* [I] The index of the implemented interface on which to set the custom data. */
2796         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
2797         VARIANT* pVarVal)        /* [I] The custom data. */
2798 {
2799     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
2800     return E_OUTOFMEMORY;
2801 }
2802
2803 /******************************************************************************
2804  * ICreateTypeInfo2_SetHelpStringContext {OLEAUT32}
2805  *
2806  *  Set the help string context for the typeinfo.
2807  *
2808  * RETURNS
2809  *
2810  *  Success: S_OK.
2811  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2812  */
2813 static HRESULT WINAPI ICreateTypeInfo2_fnSetHelpStringContext(
2814         ICreateTypeInfo2* iface,   /* [I] The typeinfo on which to set the help string context. */
2815         ULONG dwHelpStringContext) /* [I] The help string context. */
2816 {
2817     FIXME("(%p,%d), stub!\n", iface, dwHelpStringContext);
2818     return E_OUTOFMEMORY;
2819 }
2820
2821 /******************************************************************************
2822  * ICreateTypeInfo2_SetFuncHelpStringContext {OLEAUT32}
2823  *
2824  *  Set the help string context for a function.
2825  *
2826  * RETURNS
2827  *
2828  *  Success: S_OK.
2829  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2830  */
2831 static HRESULT WINAPI ICreateTypeInfo2_fnSetFuncHelpStringContext(
2832         ICreateTypeInfo2* iface,   /* [I] The typeinfo on which to set the help string context. */
2833         UINT index,                /* [I] The index for the function on which to set the help string context. */
2834         ULONG dwHelpStringContext) /* [I] The help string context. */
2835 {
2836     FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
2837     return E_OUTOFMEMORY;
2838 }
2839
2840 /******************************************************************************
2841  * ICreateTypeInfo2_SetVarHelpStringContext {OLEAUT32}
2842  *
2843  *  Set the help string context for a variable.
2844  *
2845  * RETURNS
2846  *
2847  *  Success: S_OK.
2848  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
2849  */
2850 static HRESULT WINAPI ICreateTypeInfo2_fnSetVarHelpStringContext(
2851         ICreateTypeInfo2* iface,   /* [I] The typeinfo on which to set the help string context. */
2852         UINT index,                /* [I] The index of the variable on which to set the help string context. */
2853         ULONG dwHelpStringContext) /* [I] The help string context */
2854 {
2855     FIXME("(%p,%d,%d), stub!\n", iface, index, dwHelpStringContext);
2856     return E_OUTOFMEMORY;
2857 }
2858
2859 /******************************************************************************
2860  * ICreateTypeInfo2_Invalidate {OLEAUT32}
2861  *
2862  *  Undocumented function. (!)
2863  */
2864 static HRESULT WINAPI ICreateTypeInfo2_fnInvalidate(
2865         ICreateTypeInfo2* iface)
2866 {
2867     FIXME("(%p), stub!\n", iface);
2868     return E_OUTOFMEMORY;
2869 }
2870
2871 /******************************************************************************
2872  * ICreateTypeInfo2_SetName {OLEAUT32}
2873  *
2874  *  Set the name for a typeinfo.
2875  *
2876  * RETURNS
2877  *
2878  *  Success: S_OK.
2879  *  Failure: One of STG_E_INSUFFICIENTMEMORY, E_OUTOFMEMORY, E_INVALIDARG or TYPE_E_INVALIDSTATE.
2880  */
2881 static HRESULT WINAPI ICreateTypeInfo2_fnSetName(
2882         ICreateTypeInfo2* iface,
2883         LPOLESTR szName)
2884 {
2885     FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
2886     return E_OUTOFMEMORY;
2887 }
2888
2889 /*================== ITypeInfo2 Implementation ===================================*/
2890
2891 /******************************************************************************
2892  * ITypeInfo2_QueryInterface {OLEAUT32}
2893  *
2894  *  See IUnknown_QueryInterface.
2895  */
2896 static HRESULT WINAPI ITypeInfo2_fnQueryInterface(ITypeInfo2 * iface, REFIID riid, LPVOID * ppv)
2897 {
2898     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2899
2900     return ICreateTypeInfo2_QueryInterface((ICreateTypeInfo2 *)This, riid, ppv);
2901 }
2902
2903 /******************************************************************************
2904  * ITypeInfo2_AddRef {OLEAUT32}
2905  *
2906  *  See IUnknown_AddRef.
2907  */
2908 static ULONG WINAPI ITypeInfo2_fnAddRef(ITypeInfo2 * iface)
2909 {
2910     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2911
2912     return ICreateTypeInfo2_AddRef((ICreateTypeInfo2 *)This);
2913 }
2914
2915 /******************************************************************************
2916  * ITypeInfo2_Release {OLEAUT32}
2917  *
2918  *  See IUnknown_Release.
2919  */
2920 static ULONG WINAPI ITypeInfo2_fnRelease(ITypeInfo2 * iface)
2921 {
2922     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2923
2924     return ICreateTypeInfo2_Release((ICreateTypeInfo2 *)This);
2925 }
2926
2927 /******************************************************************************
2928  * ITypeInfo2_GetTypeAttr {OLEAUT32}
2929  *
2930  *  See ITypeInfo_GetTypeAttr.
2931  */
2932 static HRESULT WINAPI ITypeInfo2_fnGetTypeAttr(
2933         ITypeInfo2* iface,
2934         TYPEATTR** ppTypeAttr)
2935 {
2936     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
2937     HRESULT hres;
2938
2939     TRACE("(%p,%p)\n", iface, ppTypeAttr);
2940
2941     if(!ppTypeAttr)
2942         return E_INVALIDARG;
2943
2944     hres = ICreateTypeInfo_LayOut((ICreateTypeInfo*)This);
2945     if(FAILED(hres))
2946         return hres;
2947
2948     *ppTypeAttr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TYPEATTR));
2949     if(!*ppTypeAttr)
2950         return E_OUTOFMEMORY;
2951
2952     if(This->typeinfo->posguid != -1) {
2953         MSFT_GuidEntry *guid;
2954
2955         guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][This->typeinfo->posguid];
2956         (*ppTypeAttr)->guid = guid->guid;
2957     }
2958
2959     (*ppTypeAttr)->lcid = This->typelib->typelib_header.lcid;
2960     (*ppTypeAttr)->cbSizeInstance = This->typeinfo->size;
2961     (*ppTypeAttr)->typekind = This->typekind;
2962     (*ppTypeAttr)->cFuncs = This->typeinfo->cElement&0xffff;
2963     if(This->typeinfo->flags&TYPEFLAG_FDUAL && This->typekind==TKIND_DISPATCH)
2964         (*ppTypeAttr)->cFuncs += 7;
2965     (*ppTypeAttr)->cVars = This->typeinfo->cElement>>16;
2966     (*ppTypeAttr)->cImplTypes = This->typeinfo->cImplTypes;
2967     (*ppTypeAttr)->cbSizeVft = This->typekind==TKIND_DISPATCH ? 7 * sizeof(void*) : This->typeinfo->cbSizeVft;
2968     (*ppTypeAttr)->cbAlignment = (This->typeinfo->typekind>>11) & 0x1f;
2969     (*ppTypeAttr)->wTypeFlags = This->typeinfo->flags;
2970     (*ppTypeAttr)->wMajorVerNum = This->typeinfo->version&0xffff;
2971     (*ppTypeAttr)->wMinorVerNum = This->typeinfo->version>>16;
2972
2973     if((*ppTypeAttr)->typekind == TKIND_ALIAS)
2974         FIXME("TKIND_ALIAS handling not implemented\n");
2975
2976     return S_OK;
2977 }
2978
2979 /******************************************************************************
2980  * ITypeInfo2_GetTypeComp {OLEAUT32}
2981  *
2982  *  See ITypeInfo_GetTypeComp.
2983  */
2984 static HRESULT WINAPI ITypeInfo2_fnGetTypeComp(
2985         ITypeInfo2* iface,
2986         ITypeComp** ppTComp)
2987 {
2988     FIXME("(%p,%p), stub!\n", iface, ppTComp);
2989     return E_OUTOFMEMORY;
2990 }
2991
2992 /******************************************************************************
2993  * ITypeInfo2_GetFuncDesc {OLEAUT32}
2994  *
2995  *  See ITypeInfo_GetFuncDesc.
2996  */
2997 static HRESULT WINAPI ITypeInfo2_fnGetFuncDesc(
2998         ITypeInfo2* iface,
2999         UINT index,
3000         FUNCDESC** ppFuncDesc)
3001 {
3002     FIXME("(%p,%d,%p), stub!\n", iface, index, ppFuncDesc);
3003     return E_OUTOFMEMORY;
3004 }
3005
3006 /******************************************************************************
3007  * ITypeInfo2_GetVarDesc {OLEAUT32}
3008  *
3009  *  See ITypeInfo_GetVarDesc.
3010  */
3011 static HRESULT WINAPI ITypeInfo2_fnGetVarDesc(
3012         ITypeInfo2* iface,
3013         UINT index,
3014         VARDESC** ppVarDesc)
3015 {
3016     FIXME("(%p,%d,%p), stub!\n", iface, index, ppVarDesc);
3017     return E_OUTOFMEMORY;
3018 }
3019
3020 /******************************************************************************
3021  * ITypeInfo2_GetNames {OLEAUT32}
3022  *
3023  *  See ITypeInfo_GetNames.
3024  */
3025 static HRESULT WINAPI ITypeInfo2_fnGetNames(
3026         ITypeInfo2* iface,
3027         MEMBERID memid,
3028         BSTR* rgBstrNames,
3029         UINT cMaxNames,
3030         UINT* pcNames)
3031 {
3032     FIXME("(%p,%d,%p,%d,%p), stub!\n", iface, memid, rgBstrNames, cMaxNames, pcNames);
3033     return E_OUTOFMEMORY;
3034 }
3035
3036 /******************************************************************************
3037  * ITypeInfo2_GetRefTypeOfImplType {OLEAUT32}
3038  *
3039  *  See ITypeInfo_GetRefTypeOfImplType.
3040  */
3041 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeOfImplType(
3042         ITypeInfo2* iface,
3043         UINT index,
3044         HREFTYPE* pRefType)
3045 {
3046     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3047     MSFT_RefRecord *ref;
3048     int offset;
3049
3050     TRACE("(%p,%d,%p)\n", iface, index, pRefType);
3051
3052     if(!pRefType)
3053         return E_INVALIDARG;
3054
3055     if(This->typeinfo->flags&TYPEFLAG_FDUAL) {
3056         if(index == -1) {
3057             *pRefType = -2;
3058             return S_OK;
3059         }
3060
3061         if(This->typekind == TKIND_DISPATCH)
3062             return ITypeInfo2_GetRefTypeOfImplType((ITypeInfo2*)&This->dual->lpVtblTypeInfo2,
3063                     index, pRefType);
3064     }
3065
3066     if(index>=This->typeinfo->cImplTypes)
3067         return TYPE_E_ELEMENTNOTFOUND;
3068
3069     if(This->typekind == TKIND_INTERFACE) {
3070         *pRefType = This->typeinfo->datatype1 + 2;
3071         return S_OK;
3072     }
3073
3074     offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3075     if(offset == -1)
3076         return TYPE_E_ELEMENTNOTFOUND;
3077
3078     ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3079     *pRefType = ref->reftype;
3080     return S_OK;
3081 }
3082
3083 /******************************************************************************
3084  * ITypeInfo2_GetImplTypeFlags {OLEAUT32}
3085  *
3086  *  See ITypeInfo_GetImplTypeFlags.
3087  */
3088 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeFlags(
3089         ITypeInfo2* iface,
3090         UINT index,
3091         INT* pImplTypeFlags)
3092 {
3093     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3094     int offset;
3095     MSFT_RefRecord *ref;
3096
3097     TRACE("(%p,%d,%p)\n", iface, index, pImplTypeFlags);
3098
3099     if(!pImplTypeFlags)
3100         return E_INVALIDARG;
3101
3102     if(index >= This->typeinfo->cImplTypes)
3103         return TYPE_E_ELEMENTNOTFOUND;
3104
3105     if(This->typekind != TKIND_COCLASS) {
3106         *pImplTypeFlags = 0;
3107         return S_OK;
3108     }
3109
3110     offset = ctl2_find_nth_reference(This->typelib, This->typeinfo->datatype1, index);
3111     if(offset == -1)
3112         return TYPE_E_ELEMENTNOTFOUND;
3113
3114     ref = (MSFT_RefRecord *)&This->typelib->typelib_segment_data[MSFT_SEG_REFERENCES][offset];
3115     *pImplTypeFlags = ref->flags;
3116     return S_OK;
3117 }
3118
3119 /******************************************************************************
3120  * ITypeInfo2_GetIDsOfNames {OLEAUT32}
3121  *
3122  *  See ITypeInfo_GetIDsOfNames.
3123  */
3124 static HRESULT WINAPI ITypeInfo2_fnGetIDsOfNames(
3125         ITypeInfo2* iface,
3126         LPOLESTR* rgszNames,
3127         UINT cNames,
3128         MEMBERID* pMemId)
3129 {
3130     FIXME("(%p,%p,%d,%p), stub!\n", iface, rgszNames, cNames, pMemId);
3131     return E_OUTOFMEMORY;
3132 }
3133
3134 /******************************************************************************
3135  * ITypeInfo2_Invoke {OLEAUT32}
3136  *
3137  *  See ITypeInfo_Invoke.
3138  */
3139 static HRESULT WINAPI ITypeInfo2_fnInvoke(
3140         ITypeInfo2* iface,
3141         PVOID pvInstance,
3142         MEMBERID memid,
3143         WORD wFlags,
3144         DISPPARAMS* pDispParams,
3145         VARIANT* pVarResult,
3146         EXCEPINFO* pExcepInfo,
3147         UINT* puArgErr)
3148 {
3149     FIXME("(%p,%p,%d,%x,%p,%p,%p,%p), stub!\n", iface, pvInstance, memid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
3150     return E_OUTOFMEMORY;
3151 }
3152
3153 /******************************************************************************
3154  * ITypeInfo2_GetDocumentation {OLEAUT32}
3155  *
3156  *  See ITypeInfo_GetDocumentation.
3157  */
3158 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation(
3159         ITypeInfo2* iface,
3160         MEMBERID memid,
3161         BSTR* pBstrName,
3162         BSTR* pBstrDocString,
3163         DWORD* pdwHelpContext,
3164         BSTR* pBstrHelpFile)
3165 {
3166     FIXME("(%p,%d,%p,%p,%p,%p), stub!\n", iface, memid, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
3167     return E_OUTOFMEMORY;
3168 }
3169
3170 /******************************************************************************
3171  * ITypeInfo2_GetDllEntry {OLEAUT32}
3172  *
3173  *  See ITypeInfo_GetDllEntry.
3174  */
3175 static HRESULT WINAPI ITypeInfo2_fnGetDllEntry(
3176         ITypeInfo2* iface,
3177         MEMBERID memid,
3178         INVOKEKIND invKind,
3179         BSTR* pBstrDllName,
3180         BSTR* pBstrName,
3181         WORD* pwOrdinal)
3182 {
3183     FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, invKind, pBstrDllName, pBstrName, pwOrdinal);
3184     return E_OUTOFMEMORY;
3185 }
3186
3187 /******************************************************************************
3188  * ITypeInfo2_GetRefTypeInfo {OLEAUT32}
3189  *
3190  *  See ITypeInfo_GetRefTypeInfo.
3191  */
3192 static HRESULT WINAPI ITypeInfo2_fnGetRefTypeInfo(
3193         ITypeInfo2* iface,
3194         HREFTYPE hRefType,
3195         ITypeInfo** ppTInfo)
3196 {
3197     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3198
3199     TRACE("(%p,%d,%p)\n", iface, hRefType, ppTInfo);
3200
3201     if(!ppTInfo)
3202         return E_INVALIDARG;
3203
3204     if(hRefType==-2 && This->dual) {
3205         *ppTInfo = (ITypeInfo*)&This->dual->lpVtblTypeInfo2;
3206         ITypeInfo_AddRef(*ppTInfo);
3207         return S_OK;
3208     }
3209
3210     if(hRefType&1) {
3211         ITypeLib *tl;
3212         MSFT_ImpInfo *impinfo;
3213         MSFT_ImpFile *impfile;
3214         MSFT_GuidEntry *guid;
3215         WCHAR *filename;
3216         HRESULT hres;
3217
3218         if((hRefType&(~0x3)) >= This->typelib->typelib_segdir[MSFT_SEG_IMPORTINFO].length)
3219             return E_FAIL;
3220
3221         impinfo = (MSFT_ImpInfo*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTINFO][hRefType&(~0x3)];
3222         impfile = (MSFT_ImpFile*)&This->typelib->typelib_segment_data[MSFT_SEG_IMPORTFILES][impinfo->oImpFile];
3223         guid = (MSFT_GuidEntry*)&This->typelib->typelib_segment_data[MSFT_SEG_GUID][impinfo->oGuid];
3224
3225         ctl2_decode_string(impfile->filename, &filename);
3226
3227         hres = LoadTypeLib(filename, &tl);
3228         if(FAILED(hres))
3229             return hres;
3230
3231         hres = ITypeLib_GetTypeInfoOfGuid(tl, &guid->guid, ppTInfo);
3232
3233         ITypeLib_Release(tl);
3234         return hres;
3235     } else {
3236         ICreateTypeInfo2Impl *iter;
3237         int i = 0;
3238
3239         for(iter=This->typelib->typeinfos; iter; iter=iter->next_typeinfo) {
3240             if(This->typelib->typelib_typeinfo_offsets[i] == (hRefType&(~0x3))) {
3241                 *ppTInfo = (ITypeInfo*)&iter->lpVtblTypeInfo2;
3242
3243                 ITypeLib_AddRef(*ppTInfo);
3244                 return S_OK;
3245             }
3246             i++;
3247         }
3248     }
3249
3250     return E_FAIL;
3251 }
3252
3253 /******************************************************************************
3254  * ITypeInfo2_AddressOfMember {OLEAUT32}
3255  *
3256  *  See ITypeInfo_AddressOfMember.
3257  */
3258 static HRESULT WINAPI ITypeInfo2_fnAddressOfMember(
3259         ITypeInfo2* iface,
3260         MEMBERID memid,
3261         INVOKEKIND invKind,
3262         PVOID* ppv)
3263 {
3264     FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, ppv);
3265     return E_OUTOFMEMORY;
3266 }
3267
3268 /******************************************************************************
3269  * ITypeInfo2_CreateInstance {OLEAUT32}
3270  *
3271  *  See ITypeInfo_CreateInstance.
3272  */
3273 static HRESULT WINAPI ITypeInfo2_fnCreateInstance(
3274         ITypeInfo2* iface,
3275         IUnknown* pUnkOuter,
3276         REFIID riid,
3277         PVOID* ppvObj)
3278 {
3279     FIXME("(%p,%p,%s,%p), stub!\n", iface, pUnkOuter, debugstr_guid(riid), ppvObj);
3280     return E_OUTOFMEMORY;
3281 }
3282
3283 /******************************************************************************
3284  * ITypeInfo2_GetMops {OLEAUT32}
3285  *
3286  *  See ITypeInfo_GetMops.
3287  */
3288 static HRESULT WINAPI ITypeInfo2_fnGetMops(
3289         ITypeInfo2* iface,
3290         MEMBERID memid,
3291         BSTR* pBstrMops)
3292 {
3293     FIXME("(%p,%d,%p), stub!\n", iface, memid, pBstrMops);
3294     return E_OUTOFMEMORY;
3295 }
3296
3297 /******************************************************************************
3298  * ITypeInfo2_GetContainingTypeLib {OLEAUT32}
3299  *
3300  *  See ITypeInfo_GetContainingTypeLib.
3301  */
3302 static HRESULT WINAPI ITypeInfo2_fnGetContainingTypeLib(
3303         ITypeInfo2* iface,
3304         ITypeLib** ppTLib,
3305         UINT* pIndex)
3306 {
3307     ICreateTypeInfo2Impl *This = impl_from_ITypeInfo2(iface);
3308
3309     TRACE("(%p,%p,%p)\n", iface, ppTLib, pIndex);
3310     
3311     *ppTLib = (ITypeLib *)&This->typelib->lpVtblTypeLib2;
3312     ICreateTypeLib_AddRef((ICreateTypeLib*)This->typelib);
3313     *pIndex = This->typeinfo->typekind >> 16;
3314
3315     return S_OK;
3316 }
3317
3318 /******************************************************************************
3319  * ITypeInfo2_ReleaseTypeAttr {OLEAUT32}
3320  *
3321  *  See ITypeInfo_ReleaseTypeAttr.
3322  */
3323 static void WINAPI ITypeInfo2_fnReleaseTypeAttr(
3324         ITypeInfo2* iface,
3325         TYPEATTR* pTypeAttr)
3326 {
3327     TRACE("(%p,%p)\n", iface, pTypeAttr);
3328
3329     HeapFree(GetProcessHeap(), 0, pTypeAttr);
3330 }
3331
3332 /******************************************************************************
3333  * ITypeInfo2_ReleaseFuncDesc {OLEAUT32}
3334  *
3335  *  See ITypeInfo_ReleaseFuncDesc.
3336  */
3337 static void WINAPI ITypeInfo2_fnReleaseFuncDesc(
3338         ITypeInfo2* iface,
3339         FUNCDESC* pFuncDesc)
3340 {
3341     FIXME("(%p,%p), stub!\n", iface, pFuncDesc);
3342 }
3343
3344 /******************************************************************************
3345  * ITypeInfo2_ReleaseVarDesc {OLEAUT32}
3346  *
3347  *  See ITypeInfo_ReleaseVarDesc.
3348  */
3349 static void WINAPI ITypeInfo2_fnReleaseVarDesc(
3350         ITypeInfo2* iface,
3351         VARDESC* pVarDesc)
3352 {
3353     FIXME("(%p,%p), stub!\n", iface, pVarDesc);
3354 }
3355
3356 /******************************************************************************
3357  * ITypeInfo2_GetTypeKind {OLEAUT32}
3358  *
3359  *  Get the TYPEKIND value for a TypeInfo.
3360  *
3361  * RETURNS
3362  *
3363  *  Success: S_OK.
3364  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3365  */
3366 static HRESULT WINAPI ITypeInfo2_fnGetTypeKind(
3367         ITypeInfo2* iface,   /* [I] The TypeInfo to obtain the typekind for. */
3368         TYPEKIND* pTypeKind) /* [O] The typekind for this TypeInfo. */
3369 {
3370     FIXME("(%p,%p), stub!\n", iface, pTypeKind);
3371     return E_OUTOFMEMORY;
3372 }
3373
3374 /******************************************************************************
3375  * ITypeInfo2_GetTypeFlags {OLEAUT32}
3376  *
3377  *  Get the Type Flags for a TypeInfo.
3378  *
3379  * RETURNS
3380  *
3381  *  Success: S_OK.
3382  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3383  */
3384 static HRESULT WINAPI ITypeInfo2_fnGetTypeFlags(
3385         ITypeInfo2* iface, /* [I] The TypeInfo to obtain the typeflags for. */
3386         ULONG* pTypeFlags) /* [O] The type flags for this TypeInfo. */
3387 {
3388     FIXME("(%p,%p), stub!\n", iface, pTypeFlags);
3389     return E_OUTOFMEMORY;
3390 }
3391
3392 /******************************************************************************
3393  * ITypeInfo2_GetFuncIndexOfMemId {OLEAUT32}
3394  *
3395  *  Gets the index of a function given its member id.
3396  *
3397  * RETURNS
3398  *
3399  *  Success: S_OK.
3400  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3401  */
3402 static HRESULT WINAPI ITypeInfo2_fnGetFuncIndexOfMemId(
3403         ITypeInfo2* iface,  /* [I] The TypeInfo in which to find the function. */
3404         MEMBERID memid,     /* [I] The member id for the function. */
3405         INVOKEKIND invKind, /* [I] The invocation kind for the function. */
3406         UINT* pFuncIndex)   /* [O] The index of the function. */
3407 {
3408     FIXME("(%p,%d,%d,%p), stub!\n", iface, memid, invKind, pFuncIndex);
3409     return E_OUTOFMEMORY;
3410 }
3411
3412 /******************************************************************************
3413  * ITypeInfo2_GetVarIndexOfMemId {OLEAUT32}
3414  *
3415  *  Gets the index of a variable given its member id.
3416  *
3417  * RETURNS
3418  *
3419  *  Success: S_OK.
3420  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3421  */
3422 static HRESULT WINAPI ITypeInfo2_fnGetVarIndexOfMemId(
3423         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the variable. */
3424         MEMBERID memid,    /* [I] The member id for the variable. */
3425         UINT* pVarIndex)   /* [O] The index of the variable. */
3426 {
3427     FIXME("(%p,%d,%p), stub!\n", iface, memid, pVarIndex);
3428     return E_OUTOFMEMORY;
3429 }
3430
3431 /******************************************************************************
3432  * ITypeInfo2_GetCustData {OLEAUT32}
3433  *
3434  *  Gets a custom data element from a TypeInfo.
3435  *
3436  * RETURNS
3437  *
3438  *  Success: S_OK.
3439  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3440  */
3441 static HRESULT WINAPI ITypeInfo2_fnGetCustData(
3442         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3443         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3444         VARIANT* pVarVal)  /* [O] The custom data. */
3445 {
3446     FIXME("(%p,%s,%p), stub!\n", iface, debugstr_guid(guid), pVarVal);
3447     return E_OUTOFMEMORY;
3448 }
3449
3450 /******************************************************************************
3451  * ITypeInfo2_GetFuncCustData {OLEAUT32}
3452  *
3453  *  Gets a custom data element from a function.
3454  *
3455  * RETURNS
3456  *
3457  *  Success: S_OK.
3458  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3459  */
3460 static HRESULT WINAPI ITypeInfo2_fnGetFuncCustData(
3461         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3462         UINT index,        /* [I] The index of the function for which to retrieve the custom data. */
3463         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3464         VARIANT* pVarVal)  /* [O] The custom data. */
3465 {
3466     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3467     return E_OUTOFMEMORY;
3468 }
3469
3470 /******************************************************************************
3471  * ITypeInfo2_GetParamCustData {OLEAUT32}
3472  *
3473  *  Gets a custom data element from a parameter.
3474  *
3475  * RETURNS
3476  *
3477  *  Success: S_OK.
3478  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3479  */
3480 static HRESULT WINAPI ITypeInfo2_fnGetParamCustData(
3481         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3482         UINT indexFunc,    /* [I] The index of the function for which to retrieve the custom data. */
3483         UINT indexParam,   /* [I] The index of the parameter for which to retrieve the custom data. */
3484         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3485         VARIANT* pVarVal)  /* [O] The custom data. */
3486 {
3487     FIXME("(%p,%d,%d,%s,%p), stub!\n", iface, indexFunc, indexParam, debugstr_guid(guid), pVarVal);
3488     return E_OUTOFMEMORY;
3489 }
3490
3491 /******************************************************************************
3492  * ITypeInfo2_GetVarCustData {OLEAUT32}
3493  *
3494  *  Gets a custom data element from a variable.
3495  *
3496  * RETURNS
3497  *
3498  *  Success: S_OK.
3499  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3500  */
3501 static HRESULT WINAPI ITypeInfo2_fnGetVarCustData(
3502         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3503         UINT index,        /* [I] The index of the variable for which to retrieve the custom data. */
3504         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3505         VARIANT* pVarVal)  /* [O] The custom data. */
3506 {
3507     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3508     return E_OUTOFMEMORY;
3509 }
3510
3511 /******************************************************************************
3512  * ITypeInfo2_GetImplTypeCustData {OLEAUT32}
3513  *
3514  *  Gets a custom data element from an implemented type of a TypeInfo.
3515  *
3516  * RETURNS
3517  *
3518  *  Success: S_OK.
3519  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3520  */
3521 static HRESULT WINAPI ITypeInfo2_fnGetImplTypeCustData(
3522         ITypeInfo2* iface, /* [I] The TypeInfo in which to find the custom data. */
3523         UINT index,        /* [I] The index of the implemented type for which to retrieve the custom data. */
3524         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
3525         VARIANT* pVarVal)  /* [O] The custom data. */
3526 {
3527     FIXME("(%p,%d,%s,%p), stub!\n", iface, index, debugstr_guid(guid), pVarVal);
3528     return E_OUTOFMEMORY;
3529 }
3530
3531 /******************************************************************************
3532  * ITypeInfo2_GetDocumentation2 {OLEAUT32}
3533  *
3534  *  Gets some documentation from a TypeInfo in a locale-aware fashion.
3535  *
3536  * RETURNS
3537  *
3538  *  Success: S_OK.
3539  *  Failure: One of STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
3540  */
3541 static HRESULT WINAPI ITypeInfo2_fnGetDocumentation2(
3542         ITypeInfo2* iface,           /* [I] The TypeInfo to retrieve the documentation from. */
3543         MEMBERID memid,              /* [I] The member id (why?). */
3544         LCID lcid,                   /* [I] The locale (why?). */
3545         BSTR* pbstrHelpString,       /* [O] The help string. */
3546         DWORD* pdwHelpStringContext, /* [O] The help string context. */
3547         BSTR* pbstrHelpStringDll)    /* [O] The help file name. */
3548 {
3549     FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", iface, memid, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
3550     return E_OUTOFMEMORY;
3551 }
3552
3553 /******************************************************************************
3554  * ITypeInfo2_GetAllCustData {OLEAUT32}
3555  *
3556  *  Gets all of the custom data associated with a TypeInfo.
3557  *
3558  * RETURNS
3559  *
3560  *  Success: S_OK.
3561  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3562  */
3563 static HRESULT WINAPI ITypeInfo2_fnGetAllCustData(
3564         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3565         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3566 {
3567     FIXME("(%p,%p), stub!\n", iface, pCustData);
3568     return E_OUTOFMEMORY;
3569 }
3570
3571 /******************************************************************************
3572  * ITypeInfo2_GetAllFuncCustData {OLEAUT32}
3573  *
3574  *  Gets all of the custom data associated with a function.
3575  *
3576  * RETURNS
3577  *
3578  *  Success: S_OK.
3579  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3580  */
3581 static HRESULT WINAPI ITypeInfo2_fnGetAllFuncCustData(
3582         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3583         UINT index,          /* [I] The index of the function for which to retrieve the custom data. */
3584         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3585 {
3586     FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
3587     return E_OUTOFMEMORY;
3588 }
3589
3590 /******************************************************************************
3591  * ITypeInfo2_GetAllParamCustData {OLEAUT32}
3592  *
3593  *  Gets all of the custom data associated with a parameter.
3594  *
3595  * RETURNS
3596  *
3597  *  Success: S_OK.
3598  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3599  */
3600 static HRESULT WINAPI ITypeInfo2_fnGetAllParamCustData(
3601         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3602         UINT indexFunc,      /* [I] The index of the function for which to retrieve the custom data. */
3603         UINT indexParam,     /* [I] The index of the parameter for which to retrieve the custom data. */
3604         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3605 {
3606     FIXME("(%p,%d,%d,%p), stub!\n", iface, indexFunc, indexParam, pCustData);
3607     return E_OUTOFMEMORY;
3608 }
3609
3610 /******************************************************************************
3611  * ITypeInfo2_GetAllVarCustData {OLEAUT32}
3612  *
3613  *  Gets all of the custom data associated with a variable.
3614  *
3615  * RETURNS
3616  *
3617  *  Success: S_OK.
3618  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3619  */
3620 static HRESULT WINAPI ITypeInfo2_fnGetAllVarCustData(
3621         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3622         UINT index,          /* [I] The index of the variable for which to retrieve the custom data. */
3623         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3624 {
3625     FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
3626     return E_OUTOFMEMORY;
3627 }
3628
3629 /******************************************************************************
3630  * ITypeInfo2_GetAllImplTypeCustData {OLEAUT32}
3631  *
3632  *  Gets all of the custom data associated with an implemented type.
3633  *
3634  * RETURNS
3635  *
3636  *  Success: S_OK.
3637  *  Failure: One of E_OUTOFMEMORY or E_INVALIDARG.
3638  */
3639 static HRESULT WINAPI ITypeInfo2_fnGetAllImplTypeCustData(
3640         ITypeInfo2* iface,   /* [I] The TypeInfo in which to find the custom data. */
3641         UINT index,          /* [I] The index of the implemented type for which to retrieve the custom data. */
3642         CUSTDATA* pCustData) /* [O] A pointer to the custom data. */
3643 {
3644     FIXME("(%p,%d,%p), stub!\n", iface, index, pCustData);
3645     return E_OUTOFMEMORY;
3646 }
3647
3648
3649 /*================== ICreateTypeInfo2 & ITypeInfo2 VTABLEs And Creation ===================================*/
3650
3651 static const ICreateTypeInfo2Vtbl ctypeinfo2vt =
3652 {
3653
3654     ICreateTypeInfo2_fnQueryInterface,
3655     ICreateTypeInfo2_fnAddRef,
3656     ICreateTypeInfo2_fnRelease,
3657
3658     ICreateTypeInfo2_fnSetGuid,
3659     ICreateTypeInfo2_fnSetTypeFlags,
3660     ICreateTypeInfo2_fnSetDocString,
3661     ICreateTypeInfo2_fnSetHelpContext,
3662     ICreateTypeInfo2_fnSetVersion,
3663     ICreateTypeInfo2_fnAddRefTypeInfo,
3664     ICreateTypeInfo2_fnAddFuncDesc,
3665     ICreateTypeInfo2_fnAddImplType,
3666     ICreateTypeInfo2_fnSetImplTypeFlags,
3667     ICreateTypeInfo2_fnSetAlignment,
3668     ICreateTypeInfo2_fnSetSchema,
3669     ICreateTypeInfo2_fnAddVarDesc,
3670     ICreateTypeInfo2_fnSetFuncAndParamNames,
3671     ICreateTypeInfo2_fnSetVarName,
3672     ICreateTypeInfo2_fnSetTypeDescAlias,
3673     ICreateTypeInfo2_fnDefineFuncAsDllEntry,
3674     ICreateTypeInfo2_fnSetFuncDocString,
3675     ICreateTypeInfo2_fnSetVarDocString,
3676     ICreateTypeInfo2_fnSetFuncHelpContext,
3677     ICreateTypeInfo2_fnSetVarHelpContext,
3678     ICreateTypeInfo2_fnSetMops,
3679     ICreateTypeInfo2_fnSetTypeIdldesc,
3680     ICreateTypeInfo2_fnLayOut,
3681
3682     ICreateTypeInfo2_fnDeleteFuncDesc,
3683     ICreateTypeInfo2_fnDeleteFuncDescByMemId,
3684     ICreateTypeInfo2_fnDeleteVarDesc,
3685     ICreateTypeInfo2_fnDeleteVarDescByMemId,
3686     ICreateTypeInfo2_fnDeleteImplType,
3687     ICreateTypeInfo2_fnSetCustData,
3688     ICreateTypeInfo2_fnSetFuncCustData,
3689     ICreateTypeInfo2_fnSetParamCustData,
3690     ICreateTypeInfo2_fnSetVarCustData,
3691     ICreateTypeInfo2_fnSetImplTypeCustData,
3692     ICreateTypeInfo2_fnSetHelpStringContext,
3693     ICreateTypeInfo2_fnSetFuncHelpStringContext,
3694     ICreateTypeInfo2_fnSetVarHelpStringContext,
3695     ICreateTypeInfo2_fnInvalidate,
3696     ICreateTypeInfo2_fnSetName
3697 };
3698
3699 static const ITypeInfo2Vtbl typeinfo2vt =
3700 {
3701
3702     ITypeInfo2_fnQueryInterface,
3703     ITypeInfo2_fnAddRef,
3704     ITypeInfo2_fnRelease,
3705
3706     ITypeInfo2_fnGetTypeAttr,
3707     ITypeInfo2_fnGetTypeComp,
3708     ITypeInfo2_fnGetFuncDesc,
3709     ITypeInfo2_fnGetVarDesc,
3710     ITypeInfo2_fnGetNames,
3711     ITypeInfo2_fnGetRefTypeOfImplType,
3712     ITypeInfo2_fnGetImplTypeFlags,
3713     ITypeInfo2_fnGetIDsOfNames,
3714     ITypeInfo2_fnInvoke,
3715     ITypeInfo2_fnGetDocumentation,
3716     ITypeInfo2_fnGetDllEntry,
3717     ITypeInfo2_fnGetRefTypeInfo,
3718     ITypeInfo2_fnAddressOfMember,
3719     ITypeInfo2_fnCreateInstance,
3720     ITypeInfo2_fnGetMops,
3721     ITypeInfo2_fnGetContainingTypeLib,
3722     ITypeInfo2_fnReleaseTypeAttr,
3723     ITypeInfo2_fnReleaseFuncDesc,
3724     ITypeInfo2_fnReleaseVarDesc,
3725
3726     ITypeInfo2_fnGetTypeKind,
3727     ITypeInfo2_fnGetTypeFlags,
3728     ITypeInfo2_fnGetFuncIndexOfMemId,
3729     ITypeInfo2_fnGetVarIndexOfMemId,
3730     ITypeInfo2_fnGetCustData,
3731     ITypeInfo2_fnGetFuncCustData,
3732     ITypeInfo2_fnGetParamCustData,
3733     ITypeInfo2_fnGetVarCustData,
3734     ITypeInfo2_fnGetImplTypeCustData,
3735     ITypeInfo2_fnGetDocumentation2,
3736     ITypeInfo2_fnGetAllCustData,
3737     ITypeInfo2_fnGetAllFuncCustData,
3738     ITypeInfo2_fnGetAllParamCustData,
3739     ITypeInfo2_fnGetAllVarCustData,
3740     ITypeInfo2_fnGetAllImplTypeCustData,
3741 };
3742
3743 static ICreateTypeInfo2 *ICreateTypeInfo2_Constructor(ICreateTypeLib2Impl *typelib, WCHAR *szName, TYPEKIND tkind)
3744 {
3745     ICreateTypeInfo2Impl *pCreateTypeInfo2Impl;
3746
3747     int nameoffset;
3748     int typeinfo_offset;
3749     MSFT_TypeInfoBase *typeinfo;
3750
3751     TRACE("Constructing ICreateTypeInfo2 for %s with tkind %d\n", debugstr_w(szName), tkind);
3752
3753     pCreateTypeInfo2Impl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ICreateTypeInfo2Impl));
3754     if (!pCreateTypeInfo2Impl) return NULL;
3755
3756     pCreateTypeInfo2Impl->lpVtbl = &ctypeinfo2vt;
3757     pCreateTypeInfo2Impl->lpVtblTypeInfo2 = &typeinfo2vt;
3758     pCreateTypeInfo2Impl->ref = 1;
3759
3760     pCreateTypeInfo2Impl->typelib = typelib;
3761     ICreateTypeLib_AddRef((ICreateTypeLib*)typelib);
3762
3763     nameoffset = ctl2_alloc_name(typelib, szName);
3764     typeinfo_offset = ctl2_alloc_typeinfo(typelib, nameoffset);
3765     typeinfo = (MSFT_TypeInfoBase *)&typelib->typelib_segment_data[MSFT_SEG_TYPEINFO][typeinfo_offset];
3766
3767     typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset + 9] = 0x38;
3768     *((int *)&typelib->typelib_segment_data[MSFT_SEG_NAME][nameoffset]) = typeinfo_offset;
3769
3770     pCreateTypeInfo2Impl->typeinfo = typeinfo;
3771
3772     pCreateTypeInfo2Impl->typekind = tkind;
3773     typeinfo->typekind |= tkind | 0x20;
3774     ICreateTypeInfo2_SetAlignment((ICreateTypeInfo2 *)pCreateTypeInfo2Impl, 4);
3775
3776     switch (tkind) {
3777     case TKIND_ENUM:
3778     case TKIND_INTERFACE:
3779     case TKIND_DISPATCH:
3780     case TKIND_COCLASS:
3781         typeinfo->size = 4;
3782         break;
3783
3784     case TKIND_RECORD:
3785     case TKIND_UNION:
3786         typeinfo->size = 0;
3787         break;
3788
3789     case TKIND_MODULE:
3790         typeinfo->size = 2;
3791         break;
3792
3793     case TKIND_ALIAS:
3794         typeinfo->size = -0x75;
3795         break;
3796
3797     default:
3798         FIXME("(%s,%d), unrecognized typekind %d\n", debugstr_w(szName), tkind, tkind);
3799         typeinfo->size = 0xdeadbeef;
3800         break;
3801     }
3802
3803     if (typelib->last_typeinfo) typelib->last_typeinfo->next_typeinfo = pCreateTypeInfo2Impl;
3804     typelib->last_typeinfo = pCreateTypeInfo2Impl;
3805     if (!typelib->typeinfos) typelib->typeinfos = pCreateTypeInfo2Impl;
3806
3807     TRACE(" -- %p\n", pCreateTypeInfo2Impl);
3808
3809     return (ICreateTypeInfo2 *)pCreateTypeInfo2Impl;
3810 }
3811
3812
3813 /*================== ICreateTypeLib2 Implementation ===================================*/
3814
3815 /******************************************************************************
3816  * ICreateTypeLib2_QueryInterface {OLEAUT32}
3817  *
3818  *  See IUnknown_QueryInterface.
3819  */
3820 static HRESULT WINAPI ICreateTypeLib2_fnQueryInterface(
3821         ICreateTypeLib2 * iface,
3822         REFIID riid,
3823         VOID **ppvObject)
3824 {
3825     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3826
3827     TRACE("(%p)->(IID: %s)\n",This,debugstr_guid(riid));
3828
3829     *ppvObject=NULL;
3830     if(IsEqualIID(riid, &IID_IUnknown) ||
3831        IsEqualIID(riid,&IID_ICreateTypeLib)||
3832        IsEqualIID(riid,&IID_ICreateTypeLib2))
3833     {
3834         *ppvObject = This;
3835     } else if (IsEqualIID(riid, &IID_ITypeLib) ||
3836                IsEqualIID(riid, &IID_ITypeLib2)) {
3837         *ppvObject = &This->lpVtblTypeLib2;
3838     }
3839
3840     if(*ppvObject)
3841     {
3842         ICreateTypeLib2_AddRef(iface);
3843         TRACE("-- Interface: (%p)->(%p)\n",ppvObject,*ppvObject);
3844         return S_OK;
3845     }
3846     TRACE("-- Interface: E_NOINTERFACE\n");
3847     return E_NOINTERFACE;
3848 }
3849
3850 /******************************************************************************
3851  * ICreateTypeLib2_AddRef {OLEAUT32}
3852  *
3853  *  See IUnknown_AddRef.
3854  */
3855 static ULONG WINAPI ICreateTypeLib2_fnAddRef(ICreateTypeLib2 *iface)
3856 {
3857     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3858     ULONG ref = InterlockedIncrement(&This->ref);
3859
3860     TRACE("(%p)->ref was %u\n",This, ref - 1);
3861
3862     return ref;
3863 }
3864
3865 /******************************************************************************
3866  * ICreateTypeLib2_Release {OLEAUT32}
3867  *
3868  *  See IUnknown_Release.
3869  */
3870 static ULONG WINAPI ICreateTypeLib2_fnRelease(ICreateTypeLib2 *iface)
3871 {
3872     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3873     ULONG ref = InterlockedDecrement(&This->ref);
3874
3875     TRACE("(%p)->(%u)\n",This, ref);
3876
3877     if (!ref) {
3878         int i;
3879
3880         for (i = 0; i < MSFT_SEG_MAX; i++) {
3881             HeapFree(GetProcessHeap(), 0, This->typelib_segment_data[i]);
3882             This->typelib_segment_data[i] = NULL;
3883         }
3884
3885         HeapFree(GetProcessHeap(), 0, This->filename);
3886         This->filename = NULL;
3887
3888         while (This->typeinfos) {
3889             ICreateTypeInfo2Impl *typeinfo = This->typeinfos;
3890             This->typeinfos = typeinfo->next_typeinfo;
3891             if(typeinfo->typedata) {
3892                 CyclicList *iter, *rem;
3893
3894                 rem = typeinfo->typedata->next;
3895                 typeinfo->typedata->next = NULL;
3896                 iter = rem->next;
3897                 HeapFree(GetProcessHeap(), 0, rem);
3898
3899                 while(iter) {
3900                     rem = iter;
3901                     iter = iter->next;
3902                     HeapFree(GetProcessHeap(), 0, rem->u.data);
3903                     HeapFree(GetProcessHeap(), 0, rem);
3904                 }
3905             }
3906
3907             HeapFree(GetProcessHeap(), 0, typeinfo->dual);
3908             HeapFree(GetProcessHeap(), 0, typeinfo);
3909         }
3910
3911         HeapFree(GetProcessHeap(),0,This);
3912         return 0;
3913     }
3914
3915     return ref;
3916 }
3917
3918
3919 /******************************************************************************
3920  * ICreateTypeLib2_CreateTypeInfo {OLEAUT32}
3921  *
3922  *  See ICreateTypeLib_CreateTypeInfo.
3923  */
3924 static HRESULT WINAPI ICreateTypeLib2_fnCreateTypeInfo(
3925         ICreateTypeLib2 * iface,
3926         LPOLESTR szName,
3927         TYPEKIND tkind,
3928         ICreateTypeInfo **ppCTInfo)
3929 {
3930     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3931     char *name;
3932
3933     TRACE("(%p,%s,%d,%p)\n", iface, debugstr_w(szName), tkind, ppCTInfo);
3934
3935     ctl2_encode_name(This, szName, &name);
3936     if(ctl2_find_name(This, name) != -1)
3937         return TYPE_E_NAMECONFLICT;
3938
3939     *ppCTInfo = (ICreateTypeInfo *)ICreateTypeInfo2_Constructor(This, szName, tkind);
3940
3941     if (!*ppCTInfo) return E_OUTOFMEMORY;
3942
3943     return S_OK;
3944 }
3945
3946 /******************************************************************************
3947  * ICreateTypeLib2_SetName {OLEAUT32}
3948  *
3949  *  See ICreateTypeLib_SetName.
3950  */
3951 static HRESULT WINAPI ICreateTypeLib2_fnSetName(
3952         ICreateTypeLib2 * iface,
3953         LPOLESTR szName)
3954 {
3955     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3956
3957     int offset;
3958
3959     TRACE("(%p,%s)\n", iface, debugstr_w(szName));
3960
3961     offset = ctl2_alloc_name(This, szName);
3962     if (offset == -1) return E_OUTOFMEMORY;
3963     This->typelib_header.NameOffset = offset;
3964     return S_OK;
3965 }
3966
3967 /******************************************************************************
3968  * ICreateTypeLib2_SetVersion {OLEAUT32}
3969  *
3970  *  See ICreateTypeLib_SetVersion.
3971  */
3972 static HRESULT WINAPI ICreateTypeLib2_fnSetVersion(ICreateTypeLib2 * iface, WORD wMajorVerNum, WORD wMinorVerNum)
3973 {
3974     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3975
3976     TRACE("(%p,%d,%d)\n", iface, wMajorVerNum, wMinorVerNum);
3977
3978     This->typelib_header.version = wMajorVerNum | (wMinorVerNum << 16);
3979     return S_OK;
3980 }
3981
3982 /******************************************************************************
3983  * ICreateTypeLib2_SetGuid {OLEAUT32}
3984  *
3985  *  See ICreateTypeLib_SetGuid.
3986  */
3987 static HRESULT WINAPI ICreateTypeLib2_fnSetGuid(ICreateTypeLib2 * iface, REFGUID guid)
3988 {
3989     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
3990
3991     MSFT_GuidEntry guidentry;
3992     int offset;
3993
3994     TRACE("(%p,%s)\n", iface, debugstr_guid(guid));
3995
3996     guidentry.guid = *guid;
3997     guidentry.hreftype = -2;
3998     guidentry.next_hash = -1;
3999
4000     offset = ctl2_alloc_guid(This, &guidentry);
4001     
4002     if (offset == -1) return E_OUTOFMEMORY;
4003
4004     This->typelib_header.posguid = offset;
4005
4006     return S_OK;
4007 }
4008
4009 /******************************************************************************
4010  * ICreateTypeLib2_SetDocString {OLEAUT32}
4011  *
4012  *  See ICreateTypeLib_SetDocString.
4013  */
4014 static HRESULT WINAPI ICreateTypeLib2_fnSetDocString(ICreateTypeLib2 * iface, LPOLESTR szDoc)
4015 {
4016     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4017
4018     int offset;
4019
4020     TRACE("(%p,%s)\n", iface, debugstr_w(szDoc));
4021     if (!szDoc)
4022         return E_INVALIDARG;
4023
4024     offset = ctl2_alloc_string(This, szDoc);
4025     if (offset == -1) return E_OUTOFMEMORY;
4026     This->typelib_header.helpstring = offset;
4027     return S_OK;
4028 }
4029
4030 /******************************************************************************
4031  * ICreateTypeLib2_SetHelpFileName {OLEAUT32}
4032  *
4033  *  See ICreateTypeLib_SetHelpFileName.
4034  */
4035 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpFileName(ICreateTypeLib2 * iface, LPOLESTR szHelpFileName)
4036 {
4037     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4038
4039     int offset;
4040
4041     TRACE("(%p,%s)\n", iface, debugstr_w(szHelpFileName));
4042
4043     offset = ctl2_alloc_string(This, szHelpFileName);
4044     if (offset == -1) return E_OUTOFMEMORY;
4045     This->typelib_header.helpfile = offset;
4046     This->typelib_header.varflags |= 0x10;
4047     return S_OK;
4048 }
4049
4050 /******************************************************************************
4051  * ICreateTypeLib2_SetHelpContext {OLEAUT32}
4052  *
4053  *  See ICreateTypeLib_SetHelpContext.
4054  */
4055 static HRESULT WINAPI ICreateTypeLib2_fnSetHelpContext(ICreateTypeLib2 * iface, DWORD dwHelpContext)
4056 {
4057     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4058
4059     TRACE("(%p,%d)\n", iface, dwHelpContext);
4060     This->typelib_header.helpcontext = dwHelpContext;
4061     return S_OK;
4062 }
4063
4064 /******************************************************************************
4065  * ICreateTypeLib2_SetLcid {OLEAUT32}
4066  *
4067  * Sets both the lcid and lcid2 members in the header to lcid.
4068  *
4069  * As a special case if lcid == LOCALE_NEUTRAL (0), then the first header lcid
4070  * is set to US English while the second one is set to 0.
4071  */
4072 static HRESULT WINAPI ICreateTypeLib2_fnSetLcid(ICreateTypeLib2 * iface, LCID lcid)
4073 {
4074     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4075
4076     TRACE("(%p,%d)\n", iface, lcid);
4077
4078     This->typelib_header.lcid = This->typelib_header.lcid2 = lcid;
4079
4080     if(lcid == LOCALE_NEUTRAL) This->typelib_header.lcid = MAKELANGID(LANG_ENGLISH, SUBLANG_ENGLISH_US);
4081
4082     return S_OK;
4083 }
4084
4085 /******************************************************************************
4086  * ICreateTypeLib2_SetLibFlags {OLEAUT32}
4087  *
4088  *  See ICreateTypeLib_SetLibFlags.
4089  */
4090 static HRESULT WINAPI ICreateTypeLib2_fnSetLibFlags(ICreateTypeLib2 * iface, UINT uLibFlags)
4091 {
4092     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4093
4094     TRACE("(%p,0x%x)\n", iface, uLibFlags);
4095
4096     This->typelib_header.flags = uLibFlags;
4097
4098     return S_OK;
4099 }
4100
4101 static int ctl2_write_chunk(HANDLE hFile, const void *segment, int length)
4102 {
4103     DWORD dwWritten;
4104     if (!WriteFile(hFile, segment, length, &dwWritten, 0)) {
4105         CloseHandle(hFile);
4106         return 0;
4107     }
4108     return -1;
4109 }
4110
4111 static int ctl2_write_segment(ICreateTypeLib2Impl *This, HANDLE hFile, int segment)
4112 {
4113     DWORD dwWritten;
4114     if (!WriteFile(hFile, This->typelib_segment_data[segment],
4115                    This->typelib_segdir[segment].length, &dwWritten, 0)) {
4116         CloseHandle(hFile);
4117         return 0;
4118     }
4119
4120     return -1;
4121 }
4122
4123 static HRESULT ctl2_finalize_typeinfos(ICreateTypeLib2Impl *This, int filesize)
4124 {
4125     ICreateTypeInfo2Impl *typeinfo;
4126     HRESULT hres;
4127
4128     for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4129         typeinfo->typeinfo->memoffset = filesize;
4130
4131         hres = ICreateTypeInfo2_fnLayOut((ICreateTypeInfo2 *)typeinfo);
4132         if(FAILED(hres))
4133             return hres;
4134
4135         if (typeinfo->typedata)
4136             filesize += typeinfo->typedata->next->u.val
4137                 + ((typeinfo->typeinfo->cElement >> 16) * 12)
4138                 + ((typeinfo->typeinfo->cElement & 0xffff) * 12) + 4;
4139     }
4140
4141     return S_OK;
4142 }
4143
4144 static int ctl2_finalize_segment(ICreateTypeLib2Impl *This, int filepos, int segment)
4145 {
4146     if (This->typelib_segdir[segment].length) {
4147         This->typelib_segdir[segment].offset = filepos;
4148     } else {
4149         This->typelib_segdir[segment].offset = -1;
4150     }
4151
4152     return This->typelib_segdir[segment].length;
4153 }
4154
4155 static void ctl2_write_typeinfos(ICreateTypeLib2Impl *This, HANDLE hFile)
4156 {
4157     ICreateTypeInfo2Impl *typeinfo;
4158
4159     for (typeinfo = This->typeinfos; typeinfo; typeinfo = typeinfo->next_typeinfo) {
4160         CyclicList *iter;
4161         int offset = 0;
4162
4163         if (!typeinfo->typedata) continue;
4164
4165         iter = typeinfo->typedata->next;
4166         ctl2_write_chunk(hFile, &iter->u.val, sizeof(int));
4167         for(iter=iter->next; iter!=typeinfo->typedata->next; iter=iter->next)
4168             ctl2_write_chunk(hFile, iter->u.data, iter->u.data[0] & 0xffff);
4169
4170         for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4171             ctl2_write_chunk(hFile, &iter->indice, sizeof(int));
4172
4173         for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next)
4174             ctl2_write_chunk(hFile, &iter->name, sizeof(int));
4175
4176         for(iter=typeinfo->typedata->next->next; iter!=typeinfo->typedata->next; iter=iter->next) {
4177             ctl2_write_chunk(hFile, &offset, sizeof(int));
4178             offset += iter->u.data[0] & 0xffff;
4179         }
4180     }
4181 }
4182
4183 /******************************************************************************
4184  * ICreateTypeLib2_SaveAllChanges {OLEAUT32}
4185  *
4186  *  See ICreateTypeLib_SaveAllChanges.
4187  */
4188 static HRESULT WINAPI ICreateTypeLib2_fnSaveAllChanges(ICreateTypeLib2 * iface)
4189 {
4190     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4191
4192     int retval;
4193     int filepos;
4194     HANDLE hFile;
4195     HRESULT hres;
4196
4197     TRACE("(%p)\n", iface);
4198
4199     retval = TYPE_E_IOERROR;
4200
4201     hFile = CreateFileW(This->filename, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, 0);
4202     if (hFile == INVALID_HANDLE_VALUE) return retval;
4203
4204     filepos = sizeof(MSFT_Header) + sizeof(MSFT_SegDir);
4205     filepos += This->typelib_header.nrtypeinfos * 4;
4206
4207     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEINFO);
4208     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUIDHASH);
4209     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_GUID);
4210     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_REFERENCES);
4211     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTINFO);
4212     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_IMPORTFILES);
4213     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAMEHASH);
4214     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_NAME);
4215     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_STRING);
4216     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_TYPEDESC);
4217     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_ARRAYDESC);
4218     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATA);
4219     filepos += ctl2_finalize_segment(This, filepos, MSFT_SEG_CUSTDATAGUID);
4220
4221     hres = ctl2_finalize_typeinfos(This, filepos);
4222     if(FAILED(hres)) {
4223         CloseHandle(hFile);
4224         return hres;
4225     }
4226
4227     if (!ctl2_write_chunk(hFile, &This->typelib_header, sizeof(This->typelib_header))) return retval;
4228     if (This->typelib_header.varflags & HELPDLLFLAG)
4229         if (!ctl2_write_chunk(hFile, &This->helpStringDll, sizeof(This->helpStringDll))) return retval;
4230     if (!ctl2_write_chunk(hFile, This->typelib_typeinfo_offsets, This->typelib_header.nrtypeinfos * 4)) return retval;
4231     if (!ctl2_write_chunk(hFile, This->typelib_segdir, sizeof(This->typelib_segdir))) return retval;
4232     if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEINFO    )) return retval;
4233     if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUIDHASH    )) return retval;
4234     if (!ctl2_write_segment(This, hFile, MSFT_SEG_GUID        )) return retval;
4235     if (!ctl2_write_segment(This, hFile, MSFT_SEG_REFERENCES  )) return retval;
4236     if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTINFO  )) return retval;
4237     if (!ctl2_write_segment(This, hFile, MSFT_SEG_IMPORTFILES )) return retval;
4238     if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAMEHASH    )) return retval;
4239     if (!ctl2_write_segment(This, hFile, MSFT_SEG_NAME        )) return retval;
4240     if (!ctl2_write_segment(This, hFile, MSFT_SEG_STRING      )) return retval;
4241     if (!ctl2_write_segment(This, hFile, MSFT_SEG_TYPEDESC    )) return retval;
4242     if (!ctl2_write_segment(This, hFile, MSFT_SEG_ARRAYDESC   )) return retval;
4243     if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATA    )) return retval;
4244     if (!ctl2_write_segment(This, hFile, MSFT_SEG_CUSTDATAGUID)) return retval;
4245
4246     ctl2_write_typeinfos(This, hFile);
4247
4248     if (!CloseHandle(hFile)) return retval;
4249
4250     return S_OK;
4251 }
4252
4253
4254 /******************************************************************************
4255  * ICreateTypeLib2_DeleteTypeInfo {OLEAUT32}
4256  *
4257  *  Deletes a named TypeInfo from a type library.
4258  *
4259  * RETURNS
4260  *
4261  *  Success: S_OK
4262  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4263  */
4264 static HRESULT WINAPI ICreateTypeLib2_fnDeleteTypeInfo(
4265         ICreateTypeLib2 * iface, /* [I] The type library to delete from. */
4266         LPOLESTR szName)         /* [I] The name of the typeinfo to delete. */
4267 {
4268     FIXME("(%p,%s), stub!\n", iface, debugstr_w(szName));
4269     return E_OUTOFMEMORY;
4270 }
4271
4272 /******************************************************************************
4273  * ICreateTypeLib2_SetCustData {OLEAUT32}
4274  *
4275  *  Sets custom data for a type library.
4276  *
4277  * RETURNS
4278  *
4279  *  Success: S_OK
4280  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4281  */
4282 static HRESULT WINAPI ICreateTypeLib2_fnSetCustData(
4283         ICreateTypeLib2 * iface, /* [I] The type library to store the custom data in. */
4284         REFGUID guid,            /* [I] The GUID used as a key to retrieve the custom data. */
4285         VARIANT *pVarVal)        /* [I] The custom data itself. */
4286 {
4287     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4288
4289     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), pVarVal);
4290
4291     return ctl2_set_custdata(This, guid, pVarVal, &This->typelib_header.CustomDataOffset);
4292 }
4293
4294 /******************************************************************************
4295  * ICreateTypeLib2_SetHelpStringContext {OLEAUT32}
4296  *
4297  *  Sets a context number for the library help string.
4298  *
4299  * PARAMS
4300  *  iface     [I] The type library to set the help string context for.
4301  *  dwContext [I] The help string context.
4302  *
4303  * RETURNS
4304  *  Success: S_OK
4305  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4306  */
4307 static
4308 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringContext(ICreateTypeLib2 * iface,
4309                                                       ULONG dwContext)
4310 {
4311     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4312
4313     TRACE("(%p,%d)\n", iface, dwContext);
4314
4315     This->typelib_header.helpstringcontext = dwContext;
4316     return S_OK;
4317 }
4318
4319 /******************************************************************************
4320  * ICreateTypeLib2_SetHelpStringDll {OLEAUT32}
4321  *
4322  *  Set the DLL used to look up localized help strings.
4323  *
4324  * PARAMS
4325  *  iface     [I] The type library to set the help DLL for.
4326  *  szDllName [I] The name of the help DLL.
4327  *
4328  * RETURNS
4329  *  Success: S_OK
4330  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4331  */
4332 static
4333 HRESULT WINAPI ICreateTypeLib2_fnSetHelpStringDll(ICreateTypeLib2 * iface,
4334                                                   LPOLESTR szDllName)
4335 {
4336     ICreateTypeLib2Impl *This = (ICreateTypeLib2Impl *)iface;
4337     int offset;
4338
4339     TRACE("(%p,%s)\n", iface, debugstr_w(szDllName));
4340     if (!szDllName)
4341         return E_INVALIDARG;
4342
4343     offset = ctl2_alloc_string(This, szDllName);
4344     if (offset == -1)
4345         return E_OUTOFMEMORY;
4346     This->typelib_header.varflags |= HELPDLLFLAG;
4347     This->helpStringDll = offset;
4348     return S_OK;
4349 }
4350
4351 /*================== ITypeLib2 Implementation ===================================*/
4352
4353 /******************************************************************************
4354  * ITypeLib2_QueryInterface {OLEAUT32}
4355  *
4356  *  See IUnknown_QueryInterface.
4357  */
4358 static HRESULT WINAPI ITypeLib2_fnQueryInterface(ITypeLib2 * iface, REFIID riid, LPVOID * ppv)
4359 {
4360     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4361
4362     return ICreateTypeLib2_QueryInterface((ICreateTypeLib2 *)This, riid, ppv);
4363 }
4364
4365 /******************************************************************************
4366  * ITypeLib2_AddRef {OLEAUT32}
4367  *
4368  *  See IUnknown_AddRef.
4369  */
4370 static ULONG WINAPI ITypeLib2_fnAddRef(ITypeLib2 * iface)
4371 {
4372     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4373
4374     return ICreateTypeLib2_AddRef((ICreateTypeLib2 *)This);
4375 }
4376
4377 /******************************************************************************
4378  * ITypeLib2_Release {OLEAUT32}
4379  *
4380  *  See IUnknown_Release.
4381  */
4382 static ULONG WINAPI ITypeLib2_fnRelease(ITypeLib2 * iface)
4383 {
4384     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4385
4386     return ICreateTypeLib2_Release((ICreateTypeLib2 *)This);
4387 }
4388
4389 /******************************************************************************
4390  * ITypeLib2_GetTypeInfoCount {OLEAUT32}
4391  *
4392  *  See ITypeLib_GetTypeInfoCount.
4393  */
4394 static UINT WINAPI ITypeLib2_fnGetTypeInfoCount(
4395         ITypeLib2 * iface)
4396 {
4397     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4398
4399     TRACE("(%p)\n", iface);
4400
4401     return This->typelib_header.nrtypeinfos;
4402 }
4403
4404 /******************************************************************************
4405  * ITypeLib2_GetTypeInfo {OLEAUT32}
4406  *
4407  *  See ITypeLib_GetTypeInfo.
4408  */
4409 static HRESULT WINAPI ITypeLib2_fnGetTypeInfo(
4410         ITypeLib2 * iface,
4411         UINT index,
4412         ITypeInfo** ppTInfo)
4413 {
4414     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4415
4416     TRACE("(%p,%d,%p)\n", iface, index, ppTInfo);
4417
4418     if (index >= This->typelib_header.nrtypeinfos) {
4419         return TYPE_E_ELEMENTNOTFOUND;
4420     }
4421
4422     return ctl2_find_typeinfo_from_offset(This, This->typelib_typeinfo_offsets[index], ppTInfo);
4423 }
4424
4425 /******************************************************************************
4426  * ITypeLib2_GetTypeInfoType {OLEAUT32}
4427  *
4428  *  See ITypeLib_GetTypeInfoType.
4429  */
4430 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoType(
4431         ITypeLib2 * iface,
4432         UINT index,
4433         TYPEKIND* pTKind)
4434 {
4435     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4436
4437     TRACE("(%p,%d,%p)\n", iface, index, pTKind);
4438
4439     if (index >= This->typelib_header.nrtypeinfos) {
4440         return TYPE_E_ELEMENTNOTFOUND;
4441     }
4442
4443     *pTKind = (This->typelib_segment_data[MSFT_SEG_TYPEINFO][This->typelib_typeinfo_offsets[index]]) & 15;
4444
4445     return S_OK;
4446 }
4447
4448 /******************************************************************************
4449  * ITypeLib2_GetTypeInfoOfGuid {OLEAUT32}
4450  *
4451  *  See ITypeLib_GetTypeInfoOfGuid.
4452  */
4453 static HRESULT WINAPI ITypeLib2_fnGetTypeInfoOfGuid(
4454         ITypeLib2 * iface,
4455         REFGUID guid,
4456         ITypeInfo** ppTinfo)
4457 {
4458     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4459
4460     int guidoffset;
4461     int typeinfo;
4462
4463     TRACE("(%p,%s,%p)\n", iface, debugstr_guid(guid), ppTinfo);
4464
4465     guidoffset = ctl2_find_guid(This, ctl2_hash_guid(guid), guid);
4466     if (guidoffset == -1) return TYPE_E_ELEMENTNOTFOUND;
4467
4468     typeinfo = ((MSFT_GuidEntry *)&This->typelib_segment_data[MSFT_SEG_GUID][guidoffset])->hreftype;
4469     if (typeinfo < 0) return TYPE_E_ELEMENTNOTFOUND;
4470
4471     return ctl2_find_typeinfo_from_offset(This, typeinfo, ppTinfo);
4472 }
4473
4474 /******************************************************************************
4475  * ITypeLib2_GetLibAttr {OLEAUT32}
4476  *
4477  *  See ITypeLib_GetLibAttr.
4478  */
4479 static HRESULT WINAPI ITypeLib2_fnGetLibAttr(
4480         ITypeLib2 * iface,
4481         TLIBATTR** ppTLibAttr)
4482 {
4483     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4484
4485     TRACE("(%p,%p)\n", This, ppTLibAttr);
4486
4487     if(!ppTLibAttr)
4488         return E_INVALIDARG;
4489
4490     *ppTLibAttr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(TLIBATTR));
4491     if(!*ppTLibAttr)
4492         return E_OUTOFMEMORY;
4493
4494     if(This->typelib_header.posguid != -1) {
4495         MSFT_GuidEntry *guid;
4496
4497         guid = (MSFT_GuidEntry*)&This->typelib_segment_data[MSFT_SEG_GUID][This->typelib_header.posguid];
4498         (*ppTLibAttr)->guid = guid->guid;
4499     }
4500
4501     (*ppTLibAttr)->lcid = This->typelib_header.lcid;
4502     (*ppTLibAttr)->syskind = This->typelib_header.varflags&0x3;
4503     (*ppTLibAttr)->wMajorVerNum = This->typelib_header.version&0xffff;
4504     (*ppTLibAttr)->wMinorVerNum = This->typelib_header.version>>16;
4505     (*ppTLibAttr)->wLibFlags = This->typelib_header.flags;
4506     return S_OK;
4507 }
4508
4509 /******************************************************************************
4510  * ITypeLib2_GetTypeComp {OLEAUT32}
4511  *
4512  *  See ITypeLib_GetTypeComp.
4513  */
4514 static HRESULT WINAPI ITypeLib2_fnGetTypeComp(
4515         ITypeLib2 * iface,
4516         ITypeComp** ppTComp)
4517 {
4518     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4519
4520     FIXME("(%p,%p), stub!\n", This, ppTComp);
4521
4522     return E_OUTOFMEMORY;
4523 }
4524
4525 /******************************************************************************
4526  * ITypeLib2_GetDocumentation {OLEAUT32}
4527  *
4528  *  See ITypeLib_GetDocumentation.
4529  */
4530 static HRESULT WINAPI ITypeLib2_fnGetDocumentation(
4531         ITypeLib2 * iface,
4532         INT index,
4533         BSTR* pBstrName,
4534         BSTR* pBstrDocString,
4535         DWORD* pdwHelpContext,
4536         BSTR* pBstrHelpFile)
4537 {
4538     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4539     WCHAR *string;
4540
4541     TRACE("(%p,%d,%p,%p,%p,%p)\n", This, index, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4542
4543     if(index != -1) {
4544         ICreateTypeInfo2Impl *iter;
4545
4546         for(iter=This->typeinfos; iter!=NULL && index!=0; iter=iter->next_typeinfo)
4547             index--;
4548
4549         if(!iter)
4550             return TYPE_E_ELEMENTNOTFOUND;
4551
4552         return ITypeInfo_GetDocumentation((ITypeInfo*)iter->lpVtblTypeInfo2,
4553                 -1, pBstrName, pBstrDocString, pdwHelpContext, pBstrHelpFile);
4554     }
4555
4556     if(pBstrName) {
4557         if(This->typelib_header.NameOffset == -1)
4558             *pBstrName = NULL;
4559         else {
4560             MSFT_NameIntro *name = (MSFT_NameIntro*)&This->
4561                 typelib_segment_data[MSFT_SEG_NAME][This->typelib_header.NameOffset];
4562
4563             ctl2_decode_name((char*)&name->namelen, &string);
4564
4565             *pBstrName = SysAllocString(string);
4566             if(!*pBstrName)
4567                 return E_OUTOFMEMORY;
4568         }
4569     }
4570
4571     if(pBstrDocString) {
4572         if(This->typelib_header.helpstring == -1)
4573             *pBstrDocString = NULL;
4574         else {
4575             ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpstring], &string);
4576
4577             *pBstrDocString = SysAllocString(string);
4578             if(!*pBstrDocString) {
4579                 if(pBstrName) SysFreeString(*pBstrName);
4580                 return E_OUTOFMEMORY;
4581             }
4582         }
4583     }
4584
4585     if(pdwHelpContext)
4586         *pdwHelpContext = This->typelib_header.helpcontext;
4587
4588     if(pBstrHelpFile) {
4589         if(This->typelib_header.helpfile == -1)
4590             *pBstrHelpFile = NULL;
4591         else {
4592             ctl2_decode_string(&This->typelib_segment_data[MSFT_SEG_STRING][This->typelib_header.helpfile], &string);
4593
4594             *pBstrHelpFile = SysAllocString(string);
4595             if(!*pBstrHelpFile) {
4596                 if(pBstrName) SysFreeString(*pBstrName);
4597                 if(pBstrDocString) SysFreeString(*pBstrDocString);
4598                 return E_OUTOFMEMORY;
4599             }
4600         }
4601     }
4602
4603     return S_OK;
4604 }
4605
4606 /******************************************************************************
4607  * ITypeLib2_IsName {OLEAUT32}
4608  *
4609  *  See ITypeLib_IsName.
4610  */
4611 static HRESULT WINAPI ITypeLib2_fnIsName(
4612         ITypeLib2 * iface,
4613         LPOLESTR szNameBuf,
4614         ULONG lHashVal,
4615         BOOL* pfName)
4616 {
4617     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4618
4619     char *encoded_name;
4620     int nameoffset;
4621     MSFT_NameIntro *nameintro;
4622
4623     TRACE("(%p,%s,%x,%p)\n", iface, debugstr_w(szNameBuf), lHashVal, pfName);
4624
4625     ctl2_encode_name(This, szNameBuf, &encoded_name);
4626     nameoffset = ctl2_find_name(This, encoded_name);
4627
4628     *pfName = 0;
4629
4630     if (nameoffset == -1) return S_OK;
4631
4632     nameintro = (MSFT_NameIntro *)(&This->typelib_segment_data[MSFT_SEG_NAME][nameoffset]);
4633     if (nameintro->hreftype == -1) return S_OK;
4634
4635     *pfName = 1;
4636
4637     FIXME("Should be decoding our copy of the name over szNameBuf.\n");
4638
4639     return S_OK;
4640 }
4641
4642 /******************************************************************************
4643  * ITypeLib2_FindName {OLEAUT32}
4644  *
4645  *  See ITypeLib_FindName.
4646  */
4647 static HRESULT WINAPI ITypeLib2_fnFindName(
4648         ITypeLib2 * iface,
4649         LPOLESTR szNameBuf,
4650         ULONG lHashVal,
4651         ITypeInfo** ppTInfo,
4652         MEMBERID* rgMemId,
4653         USHORT* pcFound)
4654 {
4655     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4656
4657     FIXME("(%p,%s,%x,%p,%p,%p), stub!\n", This, debugstr_w(szNameBuf), lHashVal, ppTInfo, rgMemId, pcFound);
4658
4659     return E_OUTOFMEMORY;
4660 }
4661
4662 /******************************************************************************
4663  * ITypeLib2_ReleaseTLibAttr {OLEAUT32}
4664  *
4665  *  See ITypeLib_ReleaseTLibAttr.
4666  */
4667 static void WINAPI ITypeLib2_fnReleaseTLibAttr(
4668         ITypeLib2 * iface,
4669         TLIBATTR* pTLibAttr)
4670 {
4671     TRACE("(%p,%p)\n", iface, pTLibAttr);
4672
4673     HeapFree(GetProcessHeap(), 0, pTLibAttr);
4674 }
4675
4676 /******************************************************************************
4677  * ICreateTypeLib2_GetCustData {OLEAUT32}
4678  *
4679  *  Retrieves a custom data value stored on a type library.
4680  *
4681  * RETURNS
4682  *
4683  *  Success: S_OK
4684  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4685  */
4686 static HRESULT WINAPI ITypeLib2_fnGetCustData(
4687         ITypeLib2 * iface, /* [I] The type library in which to find the custom data. */
4688         REFGUID guid,      /* [I] The GUID under which the custom data is stored. */
4689         VARIANT* pVarVal)  /* [O] The custom data. */
4690 {
4691     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4692
4693     FIXME("(%p,%s,%p), stub!\n", This, debugstr_guid(guid), pVarVal);
4694
4695     return E_OUTOFMEMORY;
4696 }
4697
4698 /******************************************************************************
4699  * ICreateTypeLib2_GetLibStatistics {OLEAUT32}
4700  *
4701  *  Retrieves some statistics about names in a type library, supposedly for
4702  *  hash table optimization purposes.
4703  *
4704  * RETURNS
4705  *
4706  *  Success: S_OK
4707  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4708  */
4709 static HRESULT WINAPI ITypeLib2_fnGetLibStatistics(
4710         ITypeLib2 * iface,      /* [I] The type library to get statistics about. */
4711         ULONG* pcUniqueNames,   /* [O] The number of unique names in the type library. */
4712         ULONG* pcchUniqueNames) /* [O] The number of changed (?) characters in names in the type library. */
4713 {
4714     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4715
4716     FIXME("(%p,%p,%p), stub!\n", This, pcUniqueNames, pcchUniqueNames);
4717
4718     return E_OUTOFMEMORY;
4719 }
4720
4721 /******************************************************************************
4722  * ICreateTypeLib2_GetDocumentation2 {OLEAUT32}
4723  *
4724  *  Obtain locale-aware help string information.
4725  *
4726  * RETURNS
4727  *
4728  *  Success: S_OK
4729  *  Failure: STG_E_INSUFFICIENTMEMORY or E_INVALIDARG.
4730  */
4731 static HRESULT WINAPI ITypeLib2_fnGetDocumentation2(
4732         ITypeLib2 * iface,
4733         INT index,
4734         LCID lcid,
4735         BSTR* pbstrHelpString,
4736         DWORD* pdwHelpStringContext,
4737         BSTR* pbstrHelpStringDll)
4738 {
4739     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4740
4741     FIXME("(%p,%d,%d,%p,%p,%p), stub!\n", This, index, lcid, pbstrHelpString, pdwHelpStringContext, pbstrHelpStringDll);
4742
4743     return E_OUTOFMEMORY;
4744 }
4745
4746 /******************************************************************************
4747  * ICreateTypeLib2_GetAllCustData {OLEAUT32}
4748  *
4749  *  Retrieve all of the custom data for a type library.
4750  *
4751  * RETURNS
4752  *
4753  *  Success: S_OK
4754  *  Failure: E_OUTOFMEMORY or E_INVALIDARG.
4755  */
4756 static HRESULT WINAPI ITypeLib2_fnGetAllCustData(
4757         ITypeLib2 * iface,   /* [I] The type library in which to find the custom data. */
4758         CUSTDATA* pCustData) /* [O] The structure in which to place the custom data. */
4759 {
4760     ICreateTypeLib2Impl *This = impl_from_ITypeLib2(iface);
4761
4762     FIXME("(%p,%p), stub!\n", This, pCustData);
4763
4764     return E_OUTOFMEMORY;
4765 }
4766
4767
4768 /*================== ICreateTypeLib2 & ITypeLib2 VTABLEs And Creation ===================================*/
4769
4770 static const ICreateTypeLib2Vtbl ctypelib2vt =
4771 {
4772
4773     ICreateTypeLib2_fnQueryInterface,
4774     ICreateTypeLib2_fnAddRef,
4775     ICreateTypeLib2_fnRelease,
4776
4777     ICreateTypeLib2_fnCreateTypeInfo,
4778     ICreateTypeLib2_fnSetName,
4779     ICreateTypeLib2_fnSetVersion,
4780     ICreateTypeLib2_fnSetGuid,
4781     ICreateTypeLib2_fnSetDocString,
4782     ICreateTypeLib2_fnSetHelpFileName,
4783     ICreateTypeLib2_fnSetHelpContext,
4784     ICreateTypeLib2_fnSetLcid,
4785     ICreateTypeLib2_fnSetLibFlags,
4786     ICreateTypeLib2_fnSaveAllChanges,
4787
4788     ICreateTypeLib2_fnDeleteTypeInfo,
4789     ICreateTypeLib2_fnSetCustData,
4790     ICreateTypeLib2_fnSetHelpStringContext,
4791     ICreateTypeLib2_fnSetHelpStringDll
4792 };
4793
4794 static const ITypeLib2Vtbl typelib2vt =
4795 {
4796
4797     ITypeLib2_fnQueryInterface,
4798     ITypeLib2_fnAddRef,
4799     ITypeLib2_fnRelease,
4800
4801     ITypeLib2_fnGetTypeInfoCount,
4802     ITypeLib2_fnGetTypeInfo,
4803     ITypeLib2_fnGetTypeInfoType,
4804     ITypeLib2_fnGetTypeInfoOfGuid,
4805     ITypeLib2_fnGetLibAttr,
4806     ITypeLib2_fnGetTypeComp,
4807     ITypeLib2_fnGetDocumentation,
4808     ITypeLib2_fnIsName,
4809     ITypeLib2_fnFindName,
4810     ITypeLib2_fnReleaseTLibAttr,
4811
4812     ITypeLib2_fnGetCustData,
4813     ITypeLib2_fnGetLibStatistics,
4814     ITypeLib2_fnGetDocumentation2,
4815     ITypeLib2_fnGetAllCustData,
4816 };
4817
4818 static ICreateTypeLib2 *ICreateTypeLib2_Constructor(SYSKIND syskind, LPCOLESTR szFile)
4819 {
4820     ICreateTypeLib2Impl *pCreateTypeLib2Impl;
4821     int failed = 0;
4822
4823     TRACE("Constructing ICreateTypeLib2 (%d, %s)\n", syskind, debugstr_w(szFile));
4824
4825     pCreateTypeLib2Impl = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ICreateTypeLib2Impl));
4826     if (!pCreateTypeLib2Impl) return NULL;
4827
4828     pCreateTypeLib2Impl->filename = HeapAlloc(GetProcessHeap(), 0, (strlenW(szFile) + 1) * sizeof(WCHAR));
4829     if (!pCreateTypeLib2Impl->filename) {
4830         HeapFree(GetProcessHeap(), 0, pCreateTypeLib2Impl);
4831         return NULL;
4832     }
4833     strcpyW(pCreateTypeLib2Impl->filename, szFile);
4834
4835     ctl2_init_header(pCreateTypeLib2Impl);
4836     ctl2_init_segdir(pCreateTypeLib2Impl);
4837
4838     pCreateTypeLib2Impl->typelib_header.varflags |= syskind;
4839
4840     /*
4841      * The following two calls return an offset or -1 if out of memory. We
4842      * specifically need an offset of 0, however, so...
4843      */
4844     if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_GUIDHASH, 0x80, 0x80)) { failed = 1; }
4845     if (ctl2_alloc_segment(pCreateTypeLib2Impl, MSFT_SEG_NAMEHASH, 0x200, 0x200)) { failed = 1; }
4846
4847     pCreateTypeLib2Impl->typelib_guidhash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_GUIDHASH];
4848     pCreateTypeLib2Impl->typelib_namehash_segment = (int *)pCreateTypeLib2Impl->typelib_segment_data[MSFT_SEG_NAMEHASH];
4849
4850     memset(pCreateTypeLib2Impl->typelib_guidhash_segment, 0xff, 0x80);
4851     memset(pCreateTypeLib2Impl->typelib_namehash_segment, 0xff, 0x200);
4852
4853     pCreateTypeLib2Impl->lpVtbl = &ctypelib2vt;
4854     pCreateTypeLib2Impl->lpVtblTypeLib2 = &typelib2vt;
4855     pCreateTypeLib2Impl->ref = 1;
4856
4857     if (failed) {
4858         ICreateTypeLib2_fnRelease((ICreateTypeLib2 *)pCreateTypeLib2Impl);
4859         return 0;
4860     }
4861
4862     return (ICreateTypeLib2 *)pCreateTypeLib2Impl;
4863 }
4864
4865 /******************************************************************************
4866  * CreateTypeLib2 [OLEAUT32.180]
4867  *
4868  *  Obtains an ICreateTypeLib2 object for creating a new-style (MSFT) type
4869  *  library.
4870  *
4871  * NOTES
4872  *
4873  *  See also CreateTypeLib.
4874  *
4875  * RETURNS
4876  *    Success: S_OK
4877  *    Failure: Status
4878  */
4879 HRESULT WINAPI CreateTypeLib2(
4880         SYSKIND syskind,           /* [I] System type library is for */
4881         LPCOLESTR szFile,          /* [I] Type library file name */
4882         ICreateTypeLib2** ppctlib) /* [O] Storage for object returned */
4883 {
4884     TRACE("(%d,%s,%p)\n", syskind, debugstr_w(szFile), ppctlib);
4885
4886     if (!szFile) return E_INVALIDARG;
4887     *ppctlib = ICreateTypeLib2_Constructor(syskind, szFile);
4888     return (*ppctlib)? S_OK: E_OUTOFMEMORY;
4889 }
4890
4891 /******************************************************************************
4892  * ClearCustData (OLEAUT32.171)
4893  *
4894  * Clear a custom data types' data.
4895  *
4896  * PARAMS
4897  *  lpCust [I] The custom data type instance
4898  *
4899  * RETURNS
4900  *  Nothing.
4901  */
4902 void WINAPI ClearCustData(LPCUSTDATA lpCust)
4903 {
4904     if (lpCust && lpCust->cCustData)
4905     {
4906         if (lpCust->prgCustData)
4907         {
4908             DWORD i;
4909
4910             for (i = 0; i < lpCust->cCustData; i++)
4911                 VariantClear(&lpCust->prgCustData[i].varValue);
4912
4913             /* FIXME - Should be using a per-thread IMalloc */
4914             HeapFree(GetProcessHeap(), 0, lpCust->prgCustData);
4915             lpCust->prgCustData = NULL;
4916         }
4917         lpCust->cCustData = 0;
4918     }
4919 }